codescracker


c++

C++ Variables



« Previous Tutorial Next Tutorial »


Variables represent named storage locations, whose values can be manipulated during program run. For instance, to store name and marks of a student during a program run, we require two storage locations, having different name so that these can be distinguished easily.

Variables, called as symbolic variables, serve the purpose. The variables are called symbolic variables because these are named locations. For instance, the following statement declares a variable i of the data type int :

int i;

Here is an example program, uses variables to store the value entered by the user, to print the stored value back to the output screen

/* C++ Variables */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, num;
	char ch;
	cout<<"Enter a number: ";
	cin>>num;
	cout<<"\nPrinting the entered number five times:\n";
	for(i=0; i<5; i++)
	{
		cout<<num<<"  ";
	}
	cout<<"\n\nEnter a character: ";
	cin>>ch;
	cout<<"\nPrinting the entered number five times:\n";
	for(i=0; i<5; i++)
	{
		cout<<ch<<"  ";
	}
	getch();
}

Here is the sample run of the above C++ program:

c++ variables

There are the following two values associated with a symbolic variable :

C++ Variable Declaration

Here is the general form to declare a variable in C++

type name;

Here, type is any valid C++ data type and name is the name of the variable. A variable name is an identifier. Therefore, all the rules of identifier naming apply in declaring the name of a variable. Following declaration creates a variable age of int type :

int age;

Let's take an example program.

/* C++ Variables */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int age;
	cout<<"Enter your age: ";
	cin>>age;
	cout<<"\nYour age is "<<age;
	getch();
}

Here is the sample run of the above C++ program:

c++ variable declaration

To declare signed or unsigned variable, place these modifiers before the data type. For example :

signed int value;
unsigned short count;
signed long gross;
double pival;
long double res;

To declare more than one variable of the same data type, do the following :

double salary, wage;
int month, day, year;
unsigned long distance, area;

C++ Variable Initialization

All the example definitions of above examples are simple definitions. A simple definition does not provide a first value or initial value to the variable i.e., variable is uninitialized and the variable's value is said to be undefined. A first value (initial value) may be specified in the definition of a variable. A variable with a declared first value is said to be an initialised variable.

Here is the general form to initialize values to a variable in C++

type variable_name = value;

Here is a code fragment showing the variable initialization in C++

int val = 100;

Let's take an example program demonstrating C++ variable initialization

/* C++ Variables */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int val=100;
	int num=230;
	cout<<"The value of val is "<<val;
	cout<<"\nThe value of num is "<<num;
	cout<<"\n\nThe sum of val and num is "<<val+num;
	getch();
}

Here is the sample output of this C++ program:

c++ variable initialization

In both cases, val initialized with a first value of 1001. Following are some more examples of initialized variables :

double price = 213.54, discount = 0.15;
float bal = 0.26;
long val = 25L;

Note - An l (small L) or L suffix on an integer means the integer is a type long constant, a u or U suffix indicates an unsigned int constant and ul or lu indicates a type unsigned long constant. A variable can be declared anywhere in the C++ program but before its first reference.

Dynamic Initialization in C++

One additional feature of C++ is that it permits initialization of the variables at run time. This is referred to as dynamic initialization. A variable can be initialized at run time using expressions at the place of declaration. For example, if a variable has been declared as follows :

.
.
float avg;
avg = sum/count;
.
.

You can also write the above two statement in one statement like this:

float avg = sum/count;

It will initialize avg using the information available at run time i.e., using the values of sum and count known at run time. Here is an example program demonstrating dynamic initialization in C++

/* C++ Variables */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	float sub1, sub2, sub3;
	float sum=0, avg=0;
	int count=3;
	cout<<"Enter marks obtained in 3 subjects:\n";
	cin>>sub1>>sub2>>sub3;
	sum=sub1+sub2+sub3;
	avg=sum/count;
	cout<<"\n";
	cout<<"You got "<<sum<<" marks with an average of "<<avg;
	getch();
}

Here is the sample run of this C++ program:

dynamic initialization in c++

Important - Variables that are not initialized are not empty. If you do not initialize your variable, they will contain junk (or garbage) values left over from the program that last used the memory they occupy now, until the program places a value at that memory location.

More Examples

Here are some more C++ examples listed, that you can go for:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test