23
Data Types
C++ data types fall
into three categories:
Simple data type
Structured data type
Pointers
24
Simple Data Types
• There are four simple data types in C / C++.
• Countable data types
• Measurable data types
• Character data types
• String data types
Signed vs Unsigned Number 25
Unsigned numbers include 0 or positive numbers
Signed Numbers include 0, positive and negative numbers
Countable Data Types 26
• Integer: Whole Numbers, Can be positive or negative
• unsigned integer
• signed integer
• unsigned short
• signed short
• unsigned long
• signed long
Countable Data Types 27
Countable Data Types 28
Countable Data Types 29
Important Take Away 30
Measurable Data Types 31
• Floating Point Numbers: Numbers with a Decimal, can be positive or
negative
• float
• double
• long double
Measurable Data Types 32
Measurable Data Types 33
34
Boolean Data Type 35
Variable can take any of the following two values:
true (1)
false (0)
Example 36
37
Store single character
In coding we use char for character datatype
The value must be enclosed in single quotes (‘ ’)
Syntax: char variable_name = ‘Value’ ;
Example: char grade = ‘A’ ;
Character Character datatype will take 1 byte of memory
Datatype
Range of character is -128 to 127
Range of unsigned character is 255
CHAR_MIN
CHAR_MAX
UCHAR_MAX
38
39
#include <iostream>
Character
using namespace std ;
Datatype
int main ()
{
cout << CHAR_MIN <<endl ;
cout << CHAR_MAX <<endl ;
cout <<UCHAR_MAX <<endl ;
char sub1_grade = 'A’ , sub2_grade = ‘B+’ ;
cout << sub1_grade <<endl <<sub2_grade <<endl ;
return 0 ;
}
40
#include <iostream>
Character
using namespace std ;
Datatype
int main ()
{
char c = 65 ;
cout <<c <<endl ;
c = ‘65’ ;
cout << c <<endl ;
c = 200 ;
cout << c <<endl ;
return 0 ;
}
Store sequence of characters
In coding we use char [] or string for string
datatype
The value must be enclosed in double quotes (“ “)
Syntax: char variable_name [size in number] =
“Value” ;
String
Example: char std_name = “Ahmad” ;
string variable_name = “value” ;
Datatype string std_name = “Mujtaba” ;
Char [n] datatype will take n number of bytes of
memory.
String datatype will take n number of bytes of
memory where n represents the number of
characters in string.
41
What will be the output of following program?
#include <iostream>
using namespace std ;
int main ()
String {
char name [10] = "Mujtaba" ;
Datatype string name1 = "Mujtaba" ;
cout << name << “\t” << sizeof (name) <<endl;
cout << name1 << “\t” << sizeof (name1) <<endl;
return 0 ;
42
43