C Lecture-3-DataTypes
C Lecture-3-DataTypes
Data Types
Data Types
*some compiler use 10 bytes for long double : the range is +/- 3.4E-4932 and
+/- 1.1E4832
• Floating-point literals can be represented in
Note : there are no unsigned floating point data types. On all machines, variables of
the float , double, and long double data types can store positive or negative numbers.
int i; float f;
f = 8.9;
i = 8.9; // stores 8 in i ( truncates, does not round )
i = 8;
f = 8; // stores 8.0 in f
f = 7.9;
i = f; // stores 7 in i
• Defined as bool
• Literals: the values are true or false
bool boolValue;
boolValue = true;
cout << boolValue << endl;
boolValue = false;
cout << boolValue << endl;
Output:
1
0
• char
• Literals: All the keyboard and printable symbols such as 'A' '3' '!' '\n' 'n'.
• Numeric value of character from the ASCII character set is stored in memory:
char letter;
letter = 'A'; // 65 is stored in memory
cout << letter << endl;
letter = '!';
cout << letter << endl;
Output:
A
!
char letter;
letter = 65;
cout << letter << endl;
letter = 66;
cout << letter << endl;
Output:
A
B
firstName = "George";
lastName = "Washington";
sizeof
• sizeof function returns size of a data type in bytes in any system.
• The result is system-dependent.
• The argument may be a data type:
sizeof(int) // result is 4 on most systems
• The argument may be a variable:
double salary;
cout << sizeof(salary); // result is 8 on most systems
cout << "The size of an integer is " << sizeof(int) << " bytes.\n";
Scopes of a Variable
Example :-
#include <iostream>
using namespace std;
int main () {
value = 150; //error, use of value before it is defined
int value;
cout << value; }
Named Constants
Same literal may be used throughout a program, but may want to change it later.
General Form:
For Example
• Initialization required