c-08

36
Lecture 8 Lecture 8 Version 1.0 Version 1.0 Data Types in C Data Types in C

Upload: rushdi-shams

Post on 19-Jan-2015

64 views

Category:

Education


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C-08

Lecture 8Lecture 8Version 1.0Version 1.0

Data Types in CData Types in C

Page 2: C-08

2Rushdi Shams, Dept of CSE, KUET, Bangladesh

Primary Data Types in CPrimary Data Types in C

We are known of three basic data We are known of three basic data types in C-types in C-

1.1. intint

2.2. floatfloat

3.3. CharChar Are they all?Are they all? Do the programmers write code Do the programmers write code

with only these three data types?with only these three data types?

Page 3: C-08

3Rushdi Shams, Dept of CSE, KUET, Bangladesh

Additional Data Types in Additional Data Types in CC

Answer is NO!!Answer is NO!! A A char char could be an could be an unsigned char unsigned char

or a or a signed charsigned char int int could be a could be a short int short int or a or a long long

intint Let’s take a deeper look into themLet’s take a deeper look into them

Page 4: C-08

4Rushdi Shams, Dept of CSE, KUET, Bangladesh

Integers: short and longIntegers: short and long

The range of integer depends on the The range of integer depends on the compiler compiler

Turb C/C++ is a 16 bit compiler Turb C/C++ is a 16 bit compiler For such compiler the range of an For such compiler the range of an

integer isinteger is

-32768 to 32767 -32768 to 32767 Why such range? Why such range?

Page 5: C-08

5Rushdi Shams, Dept of CSE, KUET, Bangladesh

Integers: short and longIntegers: short and long

Compiler allocates maximum 16 bits to Compiler allocates maximum 16 bits to hold an integerhold an integer

Then, in that allocated location in Then, in that allocated location in memory, values up to 2memory, values up to 21515 = 32,768 can = 32,768 can be stored be stored

If you take both sides (positive and If you take both sides (positive and negative) then the range will be -32,768 negative) then the range will be -32,768 to 32,767 to 32,767

221515 if 16 bit is allocated, then it should be if 16 bit is allocated, then it should be 221616, no? , no?

Page 6: C-08

6Rushdi Shams, Dept of CSE, KUET, Bangladesh

Integers: short and longIntegers: short and long

No! Just one bit is preserved to No! Just one bit is preserved to indicate “Signs”- 0 for positive and 1 indicate “Signs”- 0 for positive and 1 for negative for negative

Page 7: C-08

7Rushdi Shams, Dept of CSE, KUET, Bangladesh

Integers: short and longIntegers: short and long

C offers a variation of the integer data C offers a variation of the integer data type that provides what are called type that provides what are called short short and and long long integer values integer values

short short and and long long integers would usually integers would usually occupy two and four bytes respectively occupy two and four bytes respectively

Page 8: C-08

8Rushdi Shams, Dept of CSE, KUET, Bangladesh

Integers: longIntegers: long

long long variables which hold variables which hold long long integers integers are declared using the keyword are declared using the keyword longlong – –

long int i ; long int i ;

long int abc ; long int abc ;

They can also be written as – They can also be written as – long i ; long i ;

long abc ;long abc ;

Page 9: C-08

9Rushdi Shams, Dept of CSE, KUET, Bangladesh

Integers: longIntegers: long

long long integers cause the program to run a integers cause the program to run a bit slower, but the range of values that bit slower, but the range of values that we can use is expanded tremendously. we can use is expanded tremendously.

The value of a The value of a long long integer typically can integer typically can vary from -2147483648 to +2147483647 vary from -2147483648 to +2147483647

Page 10: C-08

10Rushdi Shams, Dept of CSE, KUET, Bangladesh

Integers: shortIntegers: short

integers that need less space in memory integers that need less space in memory and thus help speed up program and thus help speed up program execution – execution –

short int j ; short int j ;

short int height ; short int height ;

They can also be written as – They can also be written as – short j ; short j ;

short height ;short height ; The range covered by short integers is The range covered by short integers is

similar to that of integers.similar to that of integers.

Page 11: C-08

11Rushdi Shams, Dept of CSE, KUET, Bangladesh

Integers: unsignedIntegers: unsigned

Sometimes, we know in advance that Sometimes, we know in advance that the value stored in a given integer the value stored in a given integer variable will always be positive variable will always be positive

In such a case we can declare the In such a case we can declare the variable to be variable to be unsignedunsigned, as in, , as in,

unsigned int num_students ; unsigned int num_students ;

Page 12: C-08

12Rushdi Shams, Dept of CSE, KUET, Bangladesh

Integers: unsignedIntegers: unsigned

The range will shift from -32768 to The range will shift from -32768 to +32767 to the range 0 to 65535+32767 to the range 0 to 65535

declaring an integer as declaring an integer as unsigned unsigned almost doubles the size of the almost doubles the size of the largest possible value that it can largest possible value that it can otherwise take otherwise take

Both are valid syntaxes-Both are valid syntaxes-unsigned int i ; unsigned int i ;

unsigned i ;unsigned i ;

Page 13: C-08

13Rushdi Shams, Dept of CSE, KUET, Bangladesh

Integers: unsigned and Integers: unsigned and signedsigned

Like an Like an unsigned intunsigned int, there also , there also exists a exists a short unsigned int short unsigned int and a and a long unsigned intlong unsigned int. .

By default a By default a short int short int is a is a signed signed short int short int and a and a long int long int is a is a signed signed long intlong int

Page 14: C-08

14Rushdi Shams, Dept of CSE, KUET, Bangladesh

Characters: signed and Characters: signed and unsignedunsigned

Parallel to Parallel to signed signed and and unsigned unsigned intints (either s (either short short or or longlong), similarly ), similarly there also exist there also exist signed signed and and unsigned charunsigned chars s

occupying one byte each, but having occupying one byte each, but having different ranges different ranges

How can a character have signed How can a character have signed and unsigned range?and unsigned range? We will see We will see that now.that now.

Page 15: C-08

15Rushdi Shams, Dept of CSE, KUET, Bangladesh

Character: signed and Character: signed and unsignedunsigned

A A signed char signed char is same as an is same as an ordinary ordinary char char and has a range from and has a range from -128 to +127 -128 to +127

an an unsigned char unsigned char has a range from has a range from 0 to 255 0 to 255

Page 16: C-08

16Rushdi Shams, Dept of CSE, KUET, Bangladesh

Character: signed and Character: signed and unsignedunsigned

Should this program print the Should this program print the character with corresponding ASCII character with corresponding ASCII value of 291?value of 291?

Page 17: C-08

17Rushdi Shams, Dept of CSE, KUET, Bangladesh

Characters: signed and Characters: signed and unsignedunsigned

value in our case happens to be 35, value in our case happens to be 35, hence 35 and its corresponding hence 35 and its corresponding character #, gets printed out. character #, gets printed out.

Page 18: C-08

18Rushdi Shams, Dept of CSE, KUET, Bangladesh

Brainstorming!Brainstorming!

Page 19: C-08

19Rushdi Shams, Dept of CSE, KUET, Bangladesh

The SolutionThe Solution

Page 20: C-08

20Rushdi Shams, Dept of CSE, KUET, Bangladesh

Floats and DoublesFloats and Doubles

A A float float occupies four bytes in memory and occupies four bytes in memory and can range from -3.4e38 to +3.4e38. can range from -3.4e38 to +3.4e38.

If this is insufficient then C offers a If this is insufficient then C offers a double double data type that occupies 8 bytes in memory data type that occupies 8 bytes in memory and has a range from -1.7e308 to +1.7e308and has a range from -1.7e308 to +1.7e308

If the situation demands usage of real If the situation demands usage of real numbers that lie even beyond the range numbers that lie even beyond the range offered by offered by double double data type, then there data type, then there exists a exists a long double long double that can range from -that can range from -1.7e4932 to +1.7e4932 1.7e4932 to +1.7e4932

Page 21: C-08

21Rushdi Shams, Dept of CSE, KUET, Bangladesh

The EssenceThe Essence

Page 22: C-08

22Rushdi Shams, Dept of CSE, KUET, Bangladesh

ConstantsConstants

C also allows you to use constants with C also allows you to use constants with scientific notation. The general form of such scientific notation. The general form of such notation is-notation is-

number E sign exponentnumber E sign exponent

Please note that there is no space in actual C Please note that there is no space in actual C declaration. The spaces are provided to declaration. The spaces are provided to understand the components of such declaration understand the components of such declaration

The sign part is optional The sign part is optional 1234.56 can be written as 123.456E1 (in 1234.56 can be written as 123.456E1 (in

everyday math, it is 123.456X101) which is everyday math, it is 123.456X101) which is equal to 1234.56 equal to 1234.56

Page 23: C-08

23Rushdi Shams, Dept of CSE, KUET, Bangladesh

ConstantsConstants

C supports another type of constant- C supports another type of constant- the string. A string is a set of the string. A string is a set of characters enclosed by double quotes characters enclosed by double quotes

You have worked with strings with You have worked with strings with printf() and scanf(). C allows you to printf() and scanf(). C allows you to define string constants, but it does not define string constants, but it does not formally have a string data type formally have a string data type

We will see later that C supports We will see later that C supports strings with character arrays strings with character arrays

Page 24: C-08

24Rushdi Shams, Dept of CSE, KUET, Bangladesh

ConstantsConstants

Page 25: C-08

25Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type Conversions in Type Conversions in ExpressionsExpressions

Page 26: C-08

26Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type Conversions in Type Conversions in ExpressionsExpressions

Page 27: C-08

27Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type Conversions in Type Conversions in ExpressionsExpressions

What happens here in integral What happens here in integral promotion and type promotion?promotion and type promotion?

100.0/(10/3) 100.0/(10/3)

Page 28: C-08

28Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type Conversions in Type Conversions in ExpressionsExpressions

What happens here in integral What happens here in integral promotion and type promotion?promotion and type promotion?

char ch;char ch;

short i;short i;

unsigned long ul;unsigned long ul;

float f;float f;

f/ch - (i*ul)f/ch - (i*ul)

Page 29: C-08

29Rushdi Shams, Dept of CSE, KUET, Bangladesh

What is the order then?What is the order then?

1.1. Long doubleLong double

2.2. DoubleDouble

3.3. FloatFloat

4.4. Unsigned longUnsigned long

5.5. LongLong

6.6. UnsignedUnsigned

Page 30: C-08

30Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type Conversion in Type Conversion in AssignmentAssignment

1.1. In an assignment statement (a=b), in which In an assignment statement (a=b), in which the type of right side differs with the type the type of right side differs with the type of left side, the type of right side will be of left side, the type of right side will be converted into the type of the left side.converted into the type of the left side.

2.2. When the type of the left side is in a better When the type of the left side is in a better place than the type of the right side, there place than the type of the right side, there is nothing wrong.is nothing wrong.

3.3. But when the type of the right side is in a But when the type of the right side is in a better place than the type of the left side, better place than the type of the left side, then data loss may occur.then data loss may occur.

Page 31: C-08

31Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type Conversion in Type Conversion in AssignmentAssignment

Page 32: C-08

32Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type Conversion in Type Conversion in AssignmentAssignment

When converting from long double When converting from long double to double or double to float, to double or double to float, precision is lost.precision is lost.

When converting from float to When converting from float to integer, fractional part is lost.integer, fractional part is lost.

If the number is too large to fit in If the number is too large to fit in the target type, a garbage value will the target type, a garbage value will result.result.

Page 33: C-08

33Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type Conversion in Type Conversion in AssignmentAssignment

Page 34: C-08

34Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type CastType Cast

Sometimes it is necessary to transform Sometimes it is necessary to transform the type of a variable temporarilythe type of a variable temporarily

you want to use a variable in modulus you want to use a variable in modulus operation, so you have declared it as an operation, so you have declared it as an integer since modulus operation never integer since modulus operation never accepts variables other than integers accepts variables other than integers

But you may require using the variable in But you may require using the variable in division as well which may produce a division as well which may produce a floating point result floating point result

You can use type cast this time You can use type cast this time

Page 35: C-08

35Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type CastType Cast

Page 36: C-08

36Rushdi Shams, Dept of CSE, KUET, Bangladesh

Type CastType Cast

you cannot cast a variable that is on the you cannot cast a variable that is on the left side of an assignment statement. left side of an assignment statement.