SlideShare a Scribd company logo
1 of 36
Lecture 8Lecture 8
Version 1.0Version 1.0
Data Types in CData Types in C
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?
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
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?
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?
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
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
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 ;
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
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.
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 ;
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 ;
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
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.
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
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?
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.
18Rushdi Shams, Dept of CSE, KUET, Bangladesh
Brainstorming!Brainstorming!
19Rushdi Shams, Dept of CSE, KUET, Bangladesh
The SolutionThe Solution
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
21Rushdi Shams, Dept of CSE, KUET, Bangladesh
The EssenceThe Essence
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
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
24Rushdi Shams, Dept of CSE, KUET, Bangladesh
ConstantsConstants
25Rushdi Shams, Dept of CSE, KUET, Bangladesh
Type Conversions in ExpressionsType Conversions in Expressions
26Rushdi Shams, Dept of CSE, KUET, Bangladesh
Type Conversions in ExpressionsType Conversions in Expressions
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)
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)
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
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.
31Rushdi Shams, Dept of CSE, KUET, Bangladesh
Type Conversion in AssignmentType Conversion in Assignment
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.
33Rushdi Shams, Dept of CSE, KUET, Bangladesh
Type Conversion in AssignmentType Conversion in Assignment
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
35Rushdi Shams, Dept of CSE, KUET, Bangladesh
Type CastType Cast
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.

More Related Content

What's hot

Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffmanchidabdu
 
Data Types in C
Data Types in CData Types in C
Data Types in Cyarkhosh
 
Pseudocode.docx angel
Pseudocode.docx angelPseudocode.docx angel
Pseudocode.docx angelangelevonne
 
Recommender systems
Recommender systemsRecommender systems
Recommender systemsVenkat Raman
 
Artificial intelligence and first order logic
Artificial intelligence and first order logicArtificial intelligence and first order logic
Artificial intelligence and first order logicparsa rafiq
 
Neural machine translation of rare words with subword units
Neural machine translation of rare words with subword unitsNeural machine translation of rare words with subword units
Neural machine translation of rare words with subword unitsTae Hwan Jung
 

What's hot (11)

NLP
NLPNLP
NLP
 
deep learning
deep learningdeep learning
deep learning
 
Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffman
 
ma92008id393
ma92008id393ma92008id393
ma92008id393
 
Data Types in C
Data Types in CData Types in C
Data Types in C
 
NLP_KASHK:Text Normalization
NLP_KASHK:Text NormalizationNLP_KASHK:Text Normalization
NLP_KASHK:Text Normalization
 
Pseudocode.docx angel
Pseudocode.docx angelPseudocode.docx angel
Pseudocode.docx angel
 
Recommender systems
Recommender systemsRecommender systems
Recommender systems
 
Artificial intelligence and first order logic
Artificial intelligence and first order logicArtificial intelligence and first order logic
Artificial intelligence and first order logic
 
NLP and Deep Learning
NLP and Deep LearningNLP and Deep Learning
NLP and Deep Learning
 
Neural machine translation of rare words with subword units
Neural machine translation of rare words with subword unitsNeural machine translation of rare words with subword units
Neural machine translation of rare words with subword units
 

Similar to Lec 08. C Data Types

Literals, primitive datatypes, variables, expressions, identifiers
Literals, primitive datatypes, variables, expressions, identifiersLiterals, primitive datatypes, variables, expressions, identifiers
Literals, primitive datatypes, variables, expressions, identifiersTanishq Soni
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageRakesh Roshan
 
Data types slides by Faixan
Data types slides by FaixanData types slides by Faixan
Data types slides by FaixanٖFaiXy :)
 
C language
C languageC language
C languageSMS2007
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2rohassanie
 
OPTIMIZATION OF LZ77 DATA COMPRESSION ALGORITHM
OPTIMIZATION OF LZ77 DATA COMPRESSION ALGORITHMOPTIMIZATION OF LZ77 DATA COMPRESSION ALGORITHM
OPTIMIZATION OF LZ77 DATA COMPRESSION ALGORITHMJitendra Choudhary
 
Stringing Things Along
Stringing Things AlongStringing Things Along
Stringing Things AlongKevlin Henney
 
Lec 23. Files (Part II)
Lec 23. Files (Part II)Lec 23. Files (Part II)
Lec 23. Files (Part II)Rushdi Shams
 
About size_t and ptrdiff_t
About size_t and ptrdiff_tAbout size_t and ptrdiff_t
About size_t and ptrdiff_tPVS-Studio
 
Tokens In C.pdf
Tokens In C.pdfTokens In C.pdf
Tokens In C.pdfGimnoy
 

Similar to Lec 08. C Data Types (20)

[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes
 
Literals, primitive datatypes, variables, expressions, identifiers
Literals, primitive datatypes, variables, expressions, identifiersLiterals, primitive datatypes, variables, expressions, identifiers
Literals, primitive datatypes, variables, expressions, identifiers
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
 
Data types slides by Faixan
Data types slides by FaixanData types slides by Faixan
Data types slides by Faixan
 
C language
C languageC language
C language
 
Data types ,variables,array
Data types ,variables,arrayData types ,variables,array
Data types ,variables,array
 
C language basics
C language basicsC language basics
C language basics
 
Data type
Data typeData type
Data type
 
Datatypes
DatatypesDatatypes
Datatypes
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Lec 16. Strings
Lec 16. StringsLec 16. Strings
Lec 16. Strings
 
fundamentals of c
fundamentals of cfundamentals of c
fundamentals of c
 
OPTIMIZATION OF LZ77 DATA COMPRESSION ALGORITHM
OPTIMIZATION OF LZ77 DATA COMPRESSION ALGORITHMOPTIMIZATION OF LZ77 DATA COMPRESSION ALGORITHM
OPTIMIZATION OF LZ77 DATA COMPRESSION ALGORITHM
 
50120130405006
5012013040500650120130405006
50120130405006
 
Stringing Things Along
Stringing Things AlongStringing Things Along
Stringing Things Along
 
Lec 23. Files (Part II)
Lec 23. Files (Part II)Lec 23. Files (Part II)
Lec 23. Files (Part II)
 
About size_t and ptrdiff_t
About size_t and ptrdiff_tAbout size_t and ptrdiff_t
About size_t and ptrdiff_t
 
Ansi
AnsiAnsi
Ansi
 
Tokens In C.pdf
Tokens In C.pdfTokens In C.pdf
Tokens In C.pdf
 
C Programming Assignment
C Programming AssignmentC Programming Assignment
C Programming Assignment
 

More from Rushdi Shams

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchRushdi Shams
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IRRushdi Shams
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101Rushdi Shams
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processingRushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: ParsingRushdi Shams
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translationRushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translationRushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semanticsRushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logicRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)Rushdi Shams
 

More from Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 

Recently uploaded

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 

Recently uploaded (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 

Lec 08. C Data Types

  • 1. Lecture 8Lecture 8 Version 1.0Version 1.0 Data Types in CData Types in C
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh Brainstorming!Brainstorming!
  • 19. 19Rushdi Shams, Dept of CSE, KUET, Bangladesh The SolutionThe Solution
  • 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. 21Rushdi Shams, Dept of CSE, KUET, Bangladesh The EssenceThe Essence
  • 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. 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. 24Rushdi Shams, Dept of CSE, KUET, Bangladesh ConstantsConstants
  • 25. 25Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversions in ExpressionsType Conversions in Expressions
  • 26. 26Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversions in ExpressionsType Conversions in Expressions
  • 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. 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. 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. 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. 31Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversion in AssignmentType Conversion in Assignment
  • 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. 33Rushdi Shams, Dept of CSE, KUET, Bangladesh Type Conversion in AssignmentType Conversion in Assignment
  • 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. 35Rushdi Shams, Dept of CSE, KUET, Bangladesh Type CastType Cast
  • 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.