Coupon Accepted Successfully!


Data Types in C++

Data are collections of characters, digits, symbols, etc. The data are used to represent information. The data are classified into various types. Figure indicates all data types. C++ data types can be classified as basic data type, derived data type and user-defined data type.

Description: 130970.png

C++ data types

Basic Data Type

The basic data types supported by C++ are described with their size in bytes and ranges in Table 4.6. Sizes of data types supported by C++ are shown in Figure.

Table 4.6 C++ Basic Data Types with Size and Range

Data Type

Size in Bytes




−128 to 127

unsigned char


0 to 255

signed char


−128 to 127



−32768 to 32767

unsigned int


0 to 65535

signed int


−32768 to 32767

short int


−32768 to 32767

unsigned short int


0 to 65535

signed short int


−32768 to 32767

long int


−2147483648 to 2147483647

signed long int


−2147483648 to 2147483647

unsigned long int


0 to 4294967295



3.4e-38 to 3.4e+38



1.7e-308 to 1.7e+308

long double


3.4e-4932 to 1.1e+4932



−32768 to 32767




Description: 131107.png

Data types and their sizes

Type Modifiers


The keywords signed, unsigned, short, and long are type modifiers. A type modifier changes the meaning of the base data type to produce a new data type. Each of these type modifiers is applicable to the base type int. The modifiers signed and unsigned are also applicable to the base type char. In addition, long can be applied to double data type. When the base type is absent from a declaration, int is supposed.


long l; // int is implied

unsigned char c;

signed int s; // signed is default

unsigned long int u; // int OK, not necessary

The void Data Type


The type void is empty data type. It can be used in three ways:

  1. When specified as a function return type, void means that the function does not return a value.
    void message (char *name)
    cout<<“Hello.”<< name;
    Here, message() is a void function. The keyword void is preceded by the function name. This function when executed displays only message and does not return any value to the calling function.
  2. The void keyword also uses as argument for function. When found in a function heading, void means the function does not take any arguments.
    int fun(void)
    return 1;
    Here, the function is fun() and does not require any argument. It returns an integer value.
  3. When specified as a function return type and in function heading, that is, the function neither returns a value nor requires any argument.
    void fun (void);
    The above function neither returns a value nor requires any argument.

Derived Data Type

The derived data types are pointers, functions, arrays, and references.



A pointer is a memory variable that stores a memory address. Pointer can have any name that is legal for other variable and it is declared in the same fashion like other variable but it is always denoted by ‘*’ operator.


int *x;

float *f;

char *y;

In the first statement ‘x’ is an integer pointer and it tells the compiler that it holds the address of any integer variable. In the same way ‘f’ is a float pointer that stores the address of any float variable and ‘y’ is a character pointer that stores the address of any character variable.

4.13 Write a program to use pointers.


Explanation: In this program, x is an integer variable and *p is an integer pointer. The first cout statement displays the address of variable x. The address of x is assigned to pointer p. The pointer variables are always used to store address of another variable. The second statement displays the value of x using pointer p. Figure 4.8 explains pointers.





A function is a self-contained block or a sub-program of one or more statements that perform a special task when called. The C++ functions are more civilized than C. It is possible to use the same name with multiple definitions known as function overloading. Figure 4.9 illustrates the functions.

Description: 131320.png


A simple program on function is described as follows:

4.14 Write a program to demonstrate user-defined functions.


Explanation: In the above program, the function show() is defined. The function body contains only one cout statement. When the function is executed, a message is displayed. Functions are discussed in detail in Chapter 7 (Functions in C++).



Array is a collection of elements of similar data type in which each element is located in separate memory location, for example


int b[4];

The above statement declares an array b[] which can hold four integer values. The following program illustrates use of array.

4.15 Write a program to declare, initialize an array. Display the elements of an array.


Explanation: In the above program, an array b [4] is declared and initialized. The cout statement displays the array elements. b[0] refers to first element, b[1] refers to second element, and so on. Figure describes the elements in an array and their memory locations.

Description: 131600.png
Array elements and their memory locations



Referencing (&) and dereferencing (*) operators


The & and * operators are used for referencing and dereferencing. The & symbol is also used in C++ to define reference types, and as a bitwise AND operator. We can also use the asterisk (*) as an operator to dereference a pointer and as a multiplication operator.

  1. Referencing operator (&): The referencing operator is used to define referencing variable. A reference variable prepares an alternative (alias) name for previously defined variable. The syntax of the referencing operator is as follows.
    Data-type & reference variable name = variable name;
    int qty=10;
    int & qt=qty;
    Here, qty is already declared and initialized. The second statement defines an alternative variable name, that is, qt to variable qty. If both variables printed displays the same value. Any change made in one of the variable causes the change in both the variables.
    Now, contents of qt and qty will be 20.
    Note that the token & is not an address operator. The declaration int & indicates reference to data type int.
    Principles for declaring reference variable are as follows:
    1. A reference variable should be initialized.
    2. Once a reference variable is declared, it should not refer to any other variable. Once the actual variable and reference variable are connected, they are tied jointly congenitally.
    3. The reference variable can be created referring to pointer variable. The declaration is as follows:
      char * h=“C++”;
      char *&q=h;
    4. A variable can contain various references. Modifying the value of one of them will result a change in all other variables.
    5. Array of references is not allowed.
  2. Dereferencing operator (*): The asterisk (*) in a variable expression is used to declare a pointer to a given type. In the example,
    int *x; Where, x is a pointer of integer type.
    if the operand is a “pointer to function,” the result is a function designator. If the operand is a pointer to an object, the result is an lvalue indicating that object. In the following conditions, the result of indirection is undefined.
    1. The expression is a null pointer.
    2. The expression is the address of an automatic variable and execution and it is out of scope.

Difference between & and * operator


The & is a reference operator. It displays the address of a variable in the RAM. To display the address of the variable, it should be preceded by the variable name.



int b=10;


The above statement displays the address of the integer variable b.

The operator * is used to display the value stored at the address of the variable.



int b = 10;


The above statements display the value of value stored at address of b, that is, value of b.


Description: 131612.png

Difference between & and * operator

As shown in Figure, the statement (a) displays the contents of variable b, the statement (b) displays the address of the variable b, and the statement (c) displays the value of variable b.

4.16 Write a program to declare reference variable to another variable. Display the assigned value using both the variables.


Explanation: In the above program, the variable qty is declared as integer variable and initialized with 10. The variable qt is declared as reference variable for variable qty. We can use variable qt to access the value of qty. Any change made in one of the variable changes the contents of both the variables. The contents of both variables and their addresses are always same. The variables qt and qty are modified using increment and decrement operator. But the contents and address printed of both variables are the same. Figure explains the reference variable with respect to other variable.

Description: 143869.png

Reference variable 

User-Defined Data Type

User-defined data types are structure and classes, union, and enumerated

Structure and Classes

  1. keyword struct: struct is a keyword and used to combine variables of different data types into a single record.


struct < struct name >


<data-type> <variable-name1, variable-name, 2>;

<data-type> <variable-name3, variable-name, 4>;

} <structure variable declarations>;

struct name: An optional tag name that defines the structure type.


structure variables: These are member variables and hold data


Though struct name and structure variables are noncompulsory, one of them should be present. Member variables in the structure are declared by naming a <data-type>, followed by one or more <variable-name> separated by commas.


A semicolon can separate variables of different data types.


struct my_friend


char fname [80], phone[80];

int age, height;

} friendA ;

The structure my_friend defines a variable containing two strings (fname and phone) and two integers (age and height)(Fig 4.13).


Description: 143988.png

Structure and its elements

To access elements in a structure, record selector (.) called as dot operator is used. For example

strcpy (friendA.fname,“Sachin”);

4.17 Write a program to declare struct object, initialize it and display the contents.


Explanation: In the above program, struct my_friend is defined with four member variables. Identifier A is an object of struct my_friend. The initialization of data members of struct is done using dot operator with object A. The cout statements display contents of object A.

  1. keyword class: The class is a new keyword introduced in C++. Its use is the same as struct keyword. To declare a class, following syntax is used.


<classkeyword> <class- name> [<:baseclasslist>] {<member variable list>}

Class keyword: It is one of the keywords class, struct, or union.


Class-name: It can be any unique name inside its scope.


Base classlist: If the class is derived class, then it follows the list of the base class(es). It is optional.

Member variable list: Defines the data member variables and member functions.


class circle


int radius; // data member

int area (void); // member function


Reader may refer chapter 8 on class for more details.



A union is same as compared to a struct, but the only difference is that it allows the user to declare variables that share the same memory space.

The union requires bytes that are equal to the number of bytes required for the largest members. For example, if the union contains char, integer, and long integer, then the number of bytes reserved in the memory for the union is 4 bytes


union [<union name>]


<data-type> <variable names>;

} [<union variables name>];




union charorint


char c;

int i;

} number;

C++ will allocate sufficient storage in union variable number to hold the largest element in the union. The union member variables number.c and number.i use the same memory location. In this way, writing into one will replace the other. Member variables of a union are accessed in the same way as a struct.

4.18 Program to access variable of different data types in union.


Explanation: In the above program, ABC is the name of the union. a, num, f1 are the variables of char, int, and float, respectively. X is the variable associated with the union type. Dot (.) operator is used to access the variable of any data types.

Anonymous unions


An anonymous union does not contain tag name. Elements of such union can be accessed without using tag name. Consider the following example.



int k;

float j;


Both the member variables of union have the same memory location. They can be accessed as follows:

K = 20;

j = 2.2;

The declaration should not declare a variable of the above union type. Following program illustrates the use of anonymous union.

4.19 Write a program to declare anonymous union and access its elements.


Explanation: In the above program, anonymous union is declared. The union has two data member variables k as integer variable and f as a float variable. The union has no tag name; hence, it is called as anonymous union. The member variable of such a union can be accessed directly like normal variable. Both the variables hold the same memory location.

Enumerated Data Type


The enum is a keyword. It is used for declaring enumeration data types. The programmer can declare new data type and define the variables of these data types that can hold. For example, the user can define the material as new data type. Its variable may be solid, liquid, and gas. Thus the three values are restricted for this new data type. These enumeration data types are useful in switch() case statement.


The syntax for enumerated data type is as follows and it uses a keyword enum.


enum logical { false,true};


enum logical {true=2, false=4};


enum components{solid,liquid,gas};

This statement declares a user-defined data type. The keyword enum is followed by the tag name logical. The enumerators are the identifiers false and true. Their values are constant unsigned integers and start from 0. The identifier false refers to 0 and true to 1. The identifiers are not to be enclosed with quotation marks. Also note that integer constants are not permitted and we can start the constants as given in the second statement. In the second statement, true refers to 2 and false refers to 4. In the third statement, the components is the user-defined data type and the variables attached to it are solid, liquid, and gas.

The ANSI C++ and Turbo C++ allow us to declare variables of enum type.


logical =N // N is of the type logical

logical F=false // valid

logical TT=1 // invalid in C++

logical TT=(logical) 1 // valid

int k=true; // valid

We can also define enum without tag name. Consider the following example.


enum{yes, no};


Here, yes is 0 and no is 1 and can be used as int answer=yes.

4.20 Write a program to declare enum data type and display their values.


Explanation: In the above program, enum data type logical is declared with two values, that is, false and true. The false contains value 0 and true contains the value 1. The cout statement displays the contents of true and false. True means 1 and false means 0.

Test Your Skills Now!
Take a Quiz now
Reviewer Name