• Now Online : 64
  • admin@codemyne.net


Data types

The need for data types is quite obvious to anyone who is familiar with at least one programming language. Data types allow a programmer to create variable for manipulating data in programs. The power of a programming language is often measured by the variety and abundance of data type’s that it supports.

Data types in C++ can be classified under various categories – fundamental and user defined. The fundamental data types are those at the lowest level that is, used for actual representation in memory. The fundamental data types and their functions are listed in the fallowing table.

Name Description Size* Range*
Char Character or small integer 1 byte signed: -128 to 127
unsigned: 0 to 255
short int
Short Integer 2 bytes signed: -32768 to 32767
unsigned: 0 to 65535
Int Integer 4 bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
Long Int
Long Integer 4 bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
Bool Boolean value. It can take one of two values: true or false 1 byte true or false
Float Floating point number 4 bytes 3.4e +/- 38 (7 digits)
Double Double precision floating point number 8 bytes 1.7e +/- 308 (15 digits)
Long double Long double precision floating point number 8 bytes 1.7e +/- 308 (15 digits)
wchar_t Wide character. 2 bytes 1 wide character

Example 1:

#inclde <iostream.h>
int  main()
    //declare variable;
    int x,y;
    int res;
    x= 4 ;
    y= 3;
    x = x+1;
    res = x - y;
    //print out the result
    return 0;

Example 2:

int   main()
    char mychar;
    mychar = 'f';
    cout <<"my first char:" <<mychar<<endl;
    mychar = 's';
    cout<<"my different char: " << mychar<<endl;
    return 0;

The size of a data type or object can be determined by using the statement sizeof operator. This determines the size of the particular type or object.

User defined keywords

User defined data types are those defined by the user using the typedef keyword. User does not actually create a new data type, but rather defines a new name for an existing type. The general form of the typedef statement is

         typedef type newname;

Where type is any valid data type and newname is the name for this data type. For, example, a new name for float can be created using

         typedef float balance;

Compiler recognizes balance as another name for float. A float variable can be created using

         balance overdue;

User-defined data types can also be created in two other ways: by sing the struct keyword and by sing the class keyword, which will be discussed in detail in the later parts of this book.

// defined constants: calculate circumference  
#define PI 3.14159
#define NEWLINE "\n"
int main()
    float r = 5.0; //radius
    float circle;
    circle = 2*PI*r;
    return 0;


A variable is a named location in memory that is used to hold a vale that may be modified by the program. All variables must be declared before they can be used.

Variables are fundamental to any language. Values can be assigned to variables, which can be changed in the course of program execution. The value assigned to a variable is placed in the memory allocated to that variable. Variables can be created of fundamental as well as user- defined data types.

Character variables: are used to store characters like ‘a’, ’C’, ’K’ and so on

Integer variables: are used to store whole numbers

Float variables: are used to store floating-point numbers

Boolean variables: are used to store true or false. A bool is used to express result of logical operations

Declaration of variables:

There is a difference between C and C++ with respect to the place of their declaration within the variable’s scope or the program. C requires all variables to be declared at the beginning of the program or function. A declaration that comes after an action statement is invalid in C. The compiler will refuse to compile the program.

This is not so in the case of C++, variables can be declared anywhere within the program or function – as and when they are required. The general form of declaration is

         type variable_list;

Here, type must be a valid data type and variables_list may consist of one or more identifier names separated by commas. Here are some declarations:

         int i, j, k;
         Double balance, profit;
         Char a;

Type Qualifiers

C++ includes two type qualifiers, the keywords const and volatile that define the nature and behavior of variables.

Const Type Qualifier

Variables of type const may not be changed by the program. The compiler is free to place the variable of this type into read-only memory (ROM). For example,

         const int a = 10;

Volatile Type Qualifier

A volatile variable is the opposite of const. the volatile type qualifier tells the compiler that the program can change the variable in unseen ways.

         volatile int a = 10;

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment