Lecture - 3DIT 1, C&C++)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Computer Programming C & C++ D.

IT-I

VARIABLE:-

It is a piece of primary memory (RAM) of the computer where the data can be stored by the user. The name which
is given to the piece of memory for its calling etc. is called variable name or identifier. As given

Variable name abc RAM

Variable or identifier

The basic purpose of variable is that the user can store data any if he/she wants.

RULES FOR CONSTRUCTING VARIABLE NAMES:

 A variable name can be constructing from digits, alphabets or underscore.


 The first character of variable name must be an alphabet or underscore.
 No special character is allowed in variable name except underscore.
 Space is also not allowed in variable name.
 No keyword is used as a variable name.
 A variable name should be meaningful and not too much lengthy.

DIFFERENT DATA TYPES OF VARIABLE:-

The following datatypes are used in C and C++.

1 ) integer, 2 ) float, 3 ) character and 4 ) Boolean.

1) INTEGER DATA TYPE VARIABLE:- it is used to store numeric data without floating point. For example
1, 454 and 44 etc. the total size of integer data type is 2 byte. The int keyword is used for integer data
type variable.
2) FLOAT DATA TYPE VARIABLE:- it is used to store numeric data with floating point. For example 3.4,
556.7 and 1.0 ect. The total size of floating data type variable is 4 bytes. The float keyword is used for
floating data type variable.
3) CHARACTER DATA TYPE VARIABLE: - it is used to store any type of character as numeric, alphabetic or
special. The total size of character data type is 1 byte which is equal to 8 bits. The char keyword is used for
character data type.
4) BOOLEAN DATA TYPE VARIABLE: - It is used to store Boolean data. As 1 or 0. 1 is used for TRUE or ON
while 0 is used for FALSE or OFF. The total size of Boolean data type variable is 1 bit.

Apostle Degree College Nowshera Cantt Page 1


Computer Programming C & C++ D.IT-I

VARIABLE DECLARATION:-

Giving name and data type to a variable is called variable declaration. The compiler reserves specific location in the
memory for the declared variable.

SYNTAX FOR VARIABLE DECLARATION:-

Datatype variable_name;

Example:

int a;

We can declare same data type variable name at a time. As

int a,b,c;

VARIABLE INITIALIZATION: Storing value in a variable is called variable initialization or assignment.

The data can be stored in a variable in two ways. It may be Static or dynamic

STATIC: When the data is stored in a variable during coding is called static. As lets we have integer variable and
named “a”.

a=34;

DYNAMIC: when the data is stored in a variable during running time of the program is called dynamic. In C
language the scanf keyword is used and C++ the cin keyword is used as

cin>>a;

SIMPLE PROGRAM FOR VARIABLE DECLARATION AND INITIALIZATION:


#include <iostream.h>
#include <conio.h>
void main()
{
int first, second, sum;
first=20;
cout << “enter value for dynamic variable”;
cin >> second;
sum = first + second;
cout <<”the total is=”<< sum << endl;
}

Apostle Degree College Nowshera Cantt Page 2

You might also like