C-08

159 views
109 views

Published on

Published in: Education, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
159
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C-08

  1. 1. Lecture 8Lecture 8 Version 1.0Version 1.0 Data Types in CData Types in C
  2. 2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh Primary Data Types in CPrimary Data Types in C  We are known of three basic data types in C-We are known of three basic data types in C- 1.1. intint 2.2. floatfloat 3.3. CharChar  Are they all?Are they all?  Do the programmers write code with onlyDo the programmers write code with only these three data types?these three data types?
  3. 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh Additional Data Types in CAdditional Data Types in C  Answer is NO!!Answer is NO!!  AA charchar could be ancould be an unsigned charunsigned char or aor a signedsigned charchar  intint could be acould be a short intshort int or aor a long intlong int  Let’s take a deeper look into themLet’s take a deeper look into them
  4. 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh Integers: short and longIntegers: short and long  The range of integer depends on the compilerThe range of integer depends on the compiler  Turb C/C++ is a 16 bit compilerTurb C/C++ is a 16 bit compiler  For such compiler the range of an integer isFor such compiler the range of an integer is -32768 to 32767-32768 to 32767  Why such range?Why such range?
  5. 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh Integers: short and longIntegers: short and long  Compiler allocates maximum 16 bits to hold anCompiler allocates maximum 16 bits to hold an integerinteger  Then, in that allocated location in memory,Then, in that allocated location in memory, values up to 2values up to 21515 = 32,768 can be stored= 32,768 can be stored  If you take both sides (positive and negative)If you take both sides (positive and negative) then the range will be -32,768 to 32,767then the range will be -32,768 to 32,767  221515 if 16 bit is allocated, then it should be 2if 16 bit is allocated, then it should be 21616 , no?, no?
  6. 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh Integers: short and longIntegers: short and long  No! Just one bit is preserved to indicate “Signs”-No! Just one bit is preserved to indicate “Signs”- 0 for positive and 1 for negative0 for positive and 1 for negative
  7. 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh Integers: short and longIntegers: short and long  C offers a variation of the integer data type thatC offers a variation of the integer data type that provides what are calledprovides what are called shortshort andand longlong integer valuesinteger values  shortshort andand longlong integers would usually occupy two andintegers would usually occupy two and four bytes respectivelyfour bytes respectively
  8. 8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh Integers: longIntegers: long  longlong variables which holdvariables which hold longlong integers are declaredintegers are declared using the keywordusing 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 ;
  9. 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh Integers: longIntegers: long  longlong integers cause the program to run a bit slower,integers cause the program to run a bit slower, but the range of values that we can use is expandedbut the range of values that we can use is expanded tremendously.tremendously.  The value of aThe value of a longlong integer typically can vary frominteger typically can vary from -2147483648 to +2147483647-2147483648 to +2147483647
  10. 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh Integers: shortIntegers: short  integers that need less space in memory and thus helpintegers that need less space in memory and thus help speed up program execution –speed up program 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 similar to that ofThe range covered by short integers is similar to that of integers.integers.
  11. 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh Integers: unsignedIntegers: unsigned  Sometimes, we know in advance that the valueSometimes, we know in advance that the value stored in a given integer variable will always bestored in a given integer variable will always be positivepositive  In such a case we can declare the variable to beIn such a case we can declare the variable to be unsignedunsigned, as in,, as in, unsigned int num_students ;unsigned int num_students ;
  12. 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh Integers: unsignedIntegers: unsigned  The range will shift from -32768 to +32767 toThe range will shift from -32768 to +32767 to the range 0 to 65535the range 0 to 65535  declaring an integer asdeclaring an integer as unsignedunsigned almost doublesalmost doubles the size of the largest possible value that it canthe size of the largest possible value that it can otherwise takeotherwise take  Both are valid syntaxes-Both are valid syntaxes- unsigned int i ;unsigned int i ; unsigned i ;unsigned i ;
  13. 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh Integers: unsigned and signedIntegers: unsigned and signed  Like anLike an unsigned intunsigned int, there also exists a, there also exists a shortshort unsigned intunsigned int and aand a long unsigned intlong unsigned int..  By default aBy default a short intshort int is ais a signed short intsigned short int andand aa long intlong int is ais a signed long intsigned long int
  14. 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh Characters: signed and unsignedCharacters: signed and unsigned  Parallel toParallel to signedsigned andand unsigned intunsigned ints (eithers (either shortshort oror longlong), similarly there also exist), similarly there also exist signedsigned andand unsigned charunsigned charss  occupying one byte each, but having differentoccupying one byte each, but having different rangesranges  How can a character have signed and unsignedHow can a character have signed and unsigned range?range? We will see that now.We will see that now.
  15. 15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh Character: signed and unsignedCharacter: signed and unsigned  AA signed charsigned char is same as an ordinaryis same as an ordinary charchar andand has a range from -128 to +127has a range from -128 to +127  anan unsigned charunsigned char has a range from 0 to 255has a range from 0 to 255
  16. 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh Character: signed and unsignedCharacter: signed and unsigned  Should this program print the character withShould this program print the character with corresponding ASCII value of 291?corresponding ASCII value of 291?
  17. 17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh Characters: signed and unsignedCharacters: signed and unsigned  value in our case happens to be 35, hence 35 andvalue in our case happens to be 35, hence 35 and its corresponding character #, gets printed out.its corresponding character #, gets printed out.
  18. 18. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh Brainstorming!Brainstorming!
  19. 19. 19Rushdi Shams, Dept of CSE, KUET, Bangladesh The SolutionThe Solution
  20. 20. 20Rushdi Shams, Dept of CSE, KUET, Bangladesh Floats and DoublesFloats and Doubles  AA floatfloat occupies four bytes in memory and can rangeoccupies four bytes in memory and can range from -3.4e38 to +3.4e38.from -3.4e38 to +3.4e38.  If this is insufficient then C offers aIf this is insufficient then C offers a doubledouble data typedata type that occupies 8 bytes in memory and has a range fromthat occupies 8 bytes in memory and has a range from -1.7e308 to +1.7e308-1.7e308 to +1.7e308  If the situation demands usage of real numbers that lieIf the situation demands usage of real numbers that lie even beyond the range offered byeven beyond the range offered by doubledouble data type,data type, then there exists athen there exists a long doublelong double that can range fromthat can range from -1.7e4932 to +1.7e4932-1.7e4932 to +1.7e4932
  21. 21. 21Rushdi Shams, Dept of CSE, KUET, Bangladesh The EssenceThe Essence
  22. 22. 22Rushdi Shams, Dept of CSE, KUET, Bangladesh ConstantsConstants  C also allows you to use constants with scientificC also allows you to use constants with scientific notation. The general form of such notation is-notation. The general form of such notation is- number E sign exponentnumber E sign exponent  Please note that there is no space in actual CPlease note that there is no space in actual C declaration. The spaces are provided to understand thedeclaration. The spaces are provided to understand the components of such declarationcomponents of such declaration  The sign part is optionalThe sign part is optional  1234.56 can be written as 123.456E1 (in everyday math,1234.56 can be written as 123.456E1 (in everyday math, it is 123.456X101) which is equal to 1234.56it is 123.456X101) which is equal to 1234.56
  23. 23. 23Rushdi Shams, Dept of CSE, KUET, Bangladesh ConstantsConstants  C supports another type of constant- the string.C supports another type of constant- the string. A string is a set of characters enclosed by doubleA string is a set of characters enclosed by double quotesquotes  You have worked with strings with printf() andYou have worked with strings with printf() and scanf(). C allows you to define string constants,scanf(). C allows you to define string constants, but it does not formally have a string data typebut it does not formally have a string data type  We will see later that C supports strings withWe will see later that C supports strings with character arrayscharacter arrays
  24. 24. 24Rushdi Shams, Dept of CSE, KUET, Bangladesh ConstantsConstants
  25. 25. 25Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversions in ExpressionsType Conversions in Expressions
  26. 26. 26Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversions in ExpressionsType Conversions in Expressions
  27. 27. 27Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversions in ExpressionsType Conversions in Expressions  What happens here in integral promotion andWhat happens here in integral promotion and type promotion?type promotion? 100.0/(10/3)100.0/(10/3)
  28. 28. 28Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversions in ExpressionsType Conversions in Expressions  What happens here in integral promotion andWhat happens here in integral promotion and type promotion?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)
  29. 29. 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
  30. 30. 30Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversion in AssignmentType Conversion in Assignment 1.1. In an assignment statement (a=b), in which the typeIn an assignment statement (a=b), in which the type of right side differs with the type of left side, the typeof right side differs with the type of left side, the type of right side will be converted into the type of the leftof right side will be converted into the type of the left side.side. 2.2. When the type of the left side is in a better place thanWhen the type of the left side is in a better place than the type of the right side, there is nothing wrong.the type of the right side, there is nothing wrong. 3.3. But when the type of the right side is in a better placeBut when the type of the right side is in a better place than the type of the left side, then data loss may occur.than the type of the left side, then data loss may occur.
  31. 31. 31Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversion in AssignmentType Conversion in Assignment
  32. 32. 32Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversion in AssignmentType Conversion in Assignment  When converting from long double to double orWhen converting from long double to double or double to float, precision is lost.double to float, precision is lost.  When converting from float to integer,When converting from float to integer, fractional part is lost.fractional part is lost.  If the number is too large to fit in the targetIf the number is too large to fit in the target type, a garbage value will result.type, a garbage value will result.
  33. 33. 33Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversion in AssignmentType Conversion in Assignment
  34. 34. 34Rushdi Shams, Dept of CSE, KUET, Bangladesh Type CastType Cast  Sometimes it is necessary to transform the type of aSometimes it is necessary to transform the type of a variable temporarilyvariable temporarily  you want to use a variable in modulus operation, so youyou want to use a variable in modulus operation, so you have declared it as an integer since modulus operationhave declared it as an integer since modulus operation never accepts variables other than integersnever accepts variables other than integers  But you may require using the variable in division asBut you may require using the variable in division as well which may produce a floating point resultwell which may produce a floating point result  You can use type cast this timeYou can use type cast this time
  35. 35. 35Rushdi Shams, Dept of CSE, KUET, Bangladesh Type CastType Cast
  36. 36. 36Rushdi Shams, Dept of CSE, KUET, Bangladesh Type CastType Cast  you cannot cast a variable that is on the left side of anyou cannot cast a variable that is on the left side of an assignment statement.assignment statement.

×