2-Data Types C++
2-Data Types C++
Experiment 2
Exploring Variables, Key Words, Identifiers & Various Data Types in C++
Objectives:
Variable:
A variable is a memory location whose contents can be changed. Variables behave as containers to store data
values. There are different types of variables in C++ such as:
int
float
double
char
string
bool
To declare a single variable in C++, following syntax is to be used:
Type variablename = value;
Here the equal sign is used to assign the value to the variable.
The following rules must be followed while naming the variables in C++:
A variable name can only have alphabets, numbers and the underscore.
A variable name cannot begin with a number.
It is preferred practice to begin variable names with lowercase characters.
A variable name cannot have a keyword.
A variable name can start with an underscore. But it should be avoided.
Example Codes:
#include<iostream>
#include<conio.h>
int main(){
int a = 5;
cout << a;
a=15;
cout << a;
getch();
return 0;
}
Here the value 5 is assigned to variable a and afterwards, the value of a is changed to 15.
Consider another example:
#include<iostream>
#include<conio.h>
#include<string>
int main(){
int a=5;
float b=5.3;
double c=9.888;
char d='f';
std::string e="Computer Programming Lab";
bool f=true;
cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e<<endl<<f;
getch();
return 0;
}
To declare multiple variables of same type, use the comma separated syntax as shown:
Type variable_1_name= value, variable_2_name=value, variable_3_name=value;
The following code example illustrates the concept as:
#include<iostream>
#include<conio.h>
#include<string>
int main(){
int x=5,y=6,z=10;
Constants:
When it is required to not change the existing value of a variable, const keyword is used to declare the variable
as constant (unchangeable/read only). The following example code gives idea about constant in C++ as:
#include<iostream>
#include<conio.h>
int main(){
cout<<a;
getch();
return 0;
The program will through error if we try to change the value from 10 to 15 as shown:
#include<iostream>
#include<conio.h>
int main(){
a=15;
cout<<a;
getch();
return 0;
Identifiers:
Identifiers are the names of things that appear in the programs such as variables, constants and functions. All
identifiers must obey C++’s rules. Some identifiers are predefined such as cout and cin while some are user
defined. The following rules must be followed while naming identifiers:
An identifier can consist of letters (A-Z or a-z), digits (0-9), and underscores (_).
Special characters and spaces are not allowed.
An identifier can only begin with a letter or an underscore only.
C++ has reserved keywords that cannot be used as identifiers since they have predefined meanings in
the language. For example, int cannot be used as an identifier as it has already some predefined
meaning in C++. Attempting to use these as identifiers will result in a compilation error.
Identifier must be unique in its namespace.
Key Words:
Keywords (also known as reserved words) have special meanings to C++ compiler and are always written or
typed in short (lower) cases. Keywords are words that the language uses for a special purpose and cannot be
used as identifiers/variable names. The list of keywords in C++ is given as:
asm double new switch char for register typedef delete long
auto else operator template class friend return union int struct
break enum private this const goto short unsigned static while
Data Types:
A data type specifies the size and type of information a variable will store. Basic data types used in C++ are:
Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-
float 4 bytes
7 decimal digits
Stores fractional numbers, containing one or more decimals. Sufficient for storing 15
double 8 bytes
decimal digits
The following code gives idea about finding size of a data type:
#include<iostream>
#include<conio.h>
#include<string>
int main(){
cout<<sizeof(std::string)<<endl;
cout<<sizeof(int)<<endl;
cout<<sizeof(double)<<endl;
cout<<sizeof(float)<<endl;
cout<<sizeof(char)<<endl;
cout<<sizeof(bool)<<endl;
getch();
return 0;}
#include<iostream>
#include<limits.h>
#include<conio.h>
#include<float.h>
int main(){
cout<<INT_MAX<<endl;
cout<<INT_MIN<<endl;
cout<<LONG_MAX<<endl;
cout<<LONG_MIN<<endl;
cout<<SHRT_MIN<<endl;
cout<<SHRT_MAX<<endl;
cout<<CHAR_MAX<<endl;
cout<<CHAR_MIN<<endl;
cout<<FLT_MAX<<endl;
cout<<FLT_MIN<<endl;
cout<<DBL_MAX<<endl;
cout<<DBL_MIN<<endl;
getch();
return 0;
}
Tasks:
The output should be in the format “The size of int is ----- bytes which is equal to ------ bits”
Conclusion: