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 huffman
chidabdu
 

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

FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
rohassanie
 
Lec 23. Files (Part II)
Lec 23. Files (Part II)Lec 23. Files (Part II)
Lec 23. Files (Part II)
Rushdi Shams
 

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

Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
Rushdi 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 translation
Rushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
Rushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
Rushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
Rushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
Rushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
Rushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
Rushdi 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

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 

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.