0% found this document useful (0 votes)
18 views7 pages

2-Data Types C++

Uploaded by

Zulqurnan Anjum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

2-Data Types C++

Uploaded by

Zulqurnan Anjum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Computer Programming Lab

Experiment 2

Exploring Variables, Key Words, Identifiers & Various Data Types in C++

Objectives:

 To understand variables in C++


 To understand keywords in C++
 To understand data types in C++
 To use various data types in programs

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.

Rules to Name a 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;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 2

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;
}

Multiple Variable Declaration in C++ Program:

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;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 2

float a=5.2, b=6.2, c=.8;


char d='g', e='h', v='i';
std::string j="Shah", k="g";
bool s=true, q=false;
cout<<(x+y+z)<<endl<<(a+b+c)<<endl<<d<<endl<<e<<endl<<v<<endl<<j<<endl<<k<<endl<<s<<endl<<q<
<endl;
getch();
return 0; }

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(){

const int a=5;

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(){

const int a=5;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 2

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

case extern protected throw continue if signed virtual volatile

catch float public try default inline sizeof void do

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 2

Data Types:

A data type specifies the size and type of information a variable will store. Basic data types used in C++ are:

Data Type Size Description

boolean 1 byte Stores true or false values

char 1 byte Stores a single character/letter/number, or ASCII values

2 or 4 Stores whole numbers, without decimals


int
bytes

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;}

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 2

#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:

1) Add the correct data type to the following function body:


------ num_1=9;
------ num_2=9.9999;
------ letter=’A’;
--------- K=false;
---------- text=”task”;
2) Print the output of example codes using escape sequences.
3) Write a program to add two decimal numbers.
4) Write a program to find the area and perimeter of a rectangle.
5) Write a program to subtract three numbers using multiple variable declaration.

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 2

6) Write a program to find the number of bits of the following:


 Int
 Float
 Char
 String

The output should be in the format “The size of int is ----- bytes which is equal to ------ bits”

Conclusion:

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah

You might also like