# Introduction to C++ Data Types : Integer data types

In this article, I will cover the basics of C++ integer data types. Based on GNU C++ version 4.4.X on 32 bit x86 systems.

Integers are used to store both positive and negative values which are whole numbers (not fractions and without decimals) so they’re most commonly used for counters, flags and iterators.

When working with integer values, it’s very important to know their memory representation and also the limitations which come along.

Once you need an integer variable in C++, you should always be able to answer these questions:
1) Do I need both positive and negative or just non-negative (absolute) values?
2) How large do the values get?

To answer the first question: you can choose to have signed integers (both positive and negative) or unsigned integers (positive numbers, including 0). When you don’t explicitly specify the need for the sign, C++ standard assumes you’re using both positive and negative values, therefore signed integers.

## Signed vs Unsigned

Signed integers use the most significant bit (7th in char, 15th in short, 31st in long and 63rd in long long, counting from zero indexed bit) for storing the sign information. Once the sign bit is set to 1, that indicates a negative value, however in order to read the actual absolute value of a negative integer, we need to invert the remaining 15/31/63 bits and add +1 to interpret the value correctly.

Unsigned integers are simpler and use the whole binary representation (8/16/32/64 bits) for storing the absolute value of a number.

## Size matters!

To answer the second question, we have 4 sizes of integers: 8 bit char, 16 bit short, 32 bit long and 64 bit long long. All of the 4 sizes offer the signed / unsigned version. This is assuming you’re C++ program is executing on a x86 32-bit windows system. For example, on a 64bit linux system, the long and long long might both take up 64bits!

Although char (sometime referred to as byte or octet) is mostly used to represent text information in form of strings or arrays of chars, sometimes it’s useful to store very small numbers into chars (-128 to 127 in the signed version and 0 to 255 in the unsigned version).

## Determine the actual size of an integer on your system

In order to determine the actual BYTE size of an integer on any given system (CPU architecture and OS), you should always the sizeof macro, as follows:

``````#include <iostream>
#include <cstdlib>

using namespace std;

int main() {

cout << "C++ integer data type sizes:" << endl;

cout << "char = " << sizeof(char) << "B" << endl;
cout << "short = " << sizeof(short) << "B" << endl;
cout << "long = " << sizeof(long) << "B" << endl;
cout << "long long = " << sizeof(long long) << "B" << endl;

// system("pause");
return 0;
}
``````

Example output on a x64 CentOS 6+ linux system:

```C++ integer data type sizes:
char = 1B
short = 2B
long = 8B
long long = 8B
```

Example output on a x86 32-bit Windows 7:

```C++ integer data type sizes:
char = 1B
short = 2B
long = 4B
long long = 8B
```

Visit this interactive page which describes all the integer types in C++, hover over the min/max values to see their decimal and binary interpretations:

`https://invision-web.net/web/sources/cpp-data-types/`
Tutorials | Posted on July 11, 2013 by . 