C++ Integer Data Type

Advertisements
Advertisements

Data types describe the kind of data so that computer can store or manipulate them appropriately. This is the reason to declare a data type in C++ programming.

Data types are also necessary because data items such as numbers are used in expressions. If there is a mismatch, for example – integer and real numbers – in an expression then the computer must be able to resolve it. C++ allows the following data types.

  • Integer
  • Float
  • Double
  • Char

Memory Organization

All data is stored in memory which is organized in bytes. A single byte is of 8 bits.

C++ Data Representation
C++ Data Representation

The integer data type takes 2 or 4 bytes to store information. This means it takes 16 or 32 bit space.

Integer Data Types

The integers are negative and positive numbers. C++ add modifiers like int,\hspace{3px} short \hspace{3px} int to integers to describe size and range of values of the integer types. The list of modifiers is given below.

Advertisements
  1. int 
  2. long int
  3. short int
  4. signed int
  5. unsigned int

For example

short int = 2 bytes = 16 bits

The integers have both positive and negative values and each bit position takes two values – 0 or 1.

Therefore, its range is -2^{15} to 2^{15}-1

The unsigned \hspace{3px} integer type takes only positive values and the signed \hspace{3px} integer value takes both positive and negative values. All the integer types take 4 bytes of space except the short \hspace{3px} int which takes only 2 bytes of information.

An Example Program

// Program to display the size of integer data types
#include <iostream.h>
int main()
{
        int a = 10;
        short int b = 12;
        long int c = 100;
        unsigned int d = 30;
        signed int e = 32;
   
        cout << sizeof(a) << "Bytes" << endl;
        cout << sizeof(b) << "Bytes" << endl;
        cout << sizeof(c) << "Bytes" << endl;
        cout << sizeof(d) << "Bytes" << endl;
        cout << sizeof(e) << "Bytes" << endl;
        system("pause");
        return 0;
}

Output

4 Bytes
2 Bytes
4 Bytes
4 Bytes
4 Bytes

The sizeof() function takes the variable as an argument and then output the byte size of the variable. The integer variable has a size of 4 bytes on my windows 764 bit machine. The size of the integer variable may be different on a different machine.

Bestseller

Let Us C++

One of the best selling book in C++ programming language. A perfect book for beginners and people who want to prefer self-study in step by step manner. Simple and easy narration style made this book very popular. The book assumes that you don’t have programming experience and starts with basic to more advanced topics.

Advertisements
Advertisements
Advertisements