User Menu

Login



Share with

Varibales and Datatypes PDF Print E-mail

Concept of Data Types

Data types are means to identify the type of data and associated operations of handling it. C++ provides a predefined set of data types for handling the data it uses. When variables are declared of a particular data type then the variable becomes the place where the data is stored and data types is the type of value(data) stored by that variable. Data can be of many types such as character, integer, float etc.

 range

Variable in C++

Variables are memory locations to store data of a program. Whenever we want to accept a data (as input to your program) or store some temporary data, we would likely to store that data into a temporary memory location (or variable).Variable must first be declared before it can be used.

Once a variable is declared, we are telling the compiler the name of the variable, the initial value of the variable (optional) and the type of data our variable can hold.

The name of the variable must be a valid identifier. The following image show that internal structure of a typical C++ variable inside the RAM(Random Access Memory)

 memorymap

Variable Declaration in C++:

All variables must be declared before use. A declaration specifies a type, and contains a list of one or more variables of that type as follows:

type variable_name;

Here, type must be a valid C++ data type including char, int, float, double or any user defined object etc., and variable_name may consist of one or more identifier names separated by commas. Some valid declarations are shown here:

int i, j, k;
char c, ch;
float f, salary;
double d;

A variable declaration with an initializer is always a definition. This means that storage is allocated for the variable and could be declared as follows:

int i = 100;

Variable Initialization in C++:

Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows:

type variable_name = value;

Some examples are:

int a = 3, b = 5;    // initializing a and b.
byte c = 22;         // initializes c.
double d = 12.3     //  initializes d.
char z = 'x';        // the variable z has the value 'x'.

For declarations without an initializer: variables with static storage duration are implicitly initialized with 0; the initial value of all other variables is undefined.

Example of various types of variables:

#include <iostream.h>
int main ()
{
  // Variable declaration:
  int a, b;
  int c;
  float f;
 
  // Initialization
  a = 10;
  b = 20;
  c = a + b;
  cout << c << endl ;

  f = 12.0/2.0;

  cout << f << endl ;
  return 0;
}

Output:

30

6.0000

Lvalues and Rvalues

Variables are lvalues and may appear on the left-hand side of an assignment. Numeric literals are rvalues and may not be assigned and can not appear on the left-hand side. Following is a valid statement:

int a = 2; // Here a is a Variable

But following is not a valid statement and would generate compile-time error:

1 = 2; // Left Hand Side has a Constant

Scope of Variables

A scope is a block of the program. There are three places where variables can be declared:

Inside a function or a block which is called local variables,

In the definition of function parameters which is called formal parameters.

Outside of all functions which is called global variables.

Local Variables:

Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. Following is the example using local variables:

#include <iostream.h>

int main ()
{
  // Local variable :
  int a, b;
  int c;
 
  // actual initialization
  a = 10;
  b = 20;
  c = a + b;
 
  cout << c;
  return 0;
}

Global Variables:

Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the lifetime of your program.

A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables:

#include <iostream.h>

// Global variable 
int t;
int main ()
{
  // Local variables 
  int a, b;
 
  // Initialization
  a = 10;
  b = 20;
  t = a + b;
 
  cout << t;
  return 0;
}

A program can have same name for local and global variables but value of local variable inside a function will take preference. For example:

#include <iostream.h> 

// Global variable:
int t = 20;
 
int main ()
{
  // Local variable with same name as Global variable
  int t = 120;
 
  cout << t;
   return 0;
}

When the above code is compiled and executed, it produces following output:

120

Initializing Local and Global Variables:

When a local variable is defined, it is not initalised by the system, we have to initalise it. Global variables are initalised automatically by the system when we define them as follows:

Data Type    Initialser
int          0
char         '\0'
float        0
double       0
pointer      NULL

It is a good programming practice to initialize variables properly otherwise, sometime program would produce unexpected result.

Data Types Modifiers

1.signed

2.unsigned

3.short

4.long

Int, char, float, double data types can be preceded with these modifiers to alter the meaning of the base type to fit various situations properly. Every data type has a limit of the largest and smallest value that it can store known as the range. An integer (usually 2 bytes long) can store any value ranging from -32768 to 32767.

Data Type modifiers usually alter the upper and lower limit of a data type. Unsigned modifier makes a variable only to store positive values. For Example- if a data type has a range from –a to a then unsigned variable of that type has a range from 0 to 2a. Preceding any data type with signed is optional because every data type is signed by default. Short integers are 2 bytes long and long integers are 4 bytes long.

scroll back to top