Integer Data Type

In this tutorial, we will learn about the integer data type in C, which is used to represent integers.

Whenever we create a variable in C, we need to tell the compiler what type of value it will store. If we want to store integers, we use the integer data type. The integer data type is the most commonly used numeric data type in C, and it can be divided into two types: signed integers and unsigned integers.

Now, let’s take a closer look at each type individually.


Signed Integers

Basics

Signed integers are used to represent both positive and negative values. The leftmost bit, known as the most significant bit, is reserved for indicating the sign of the integer. If the leftmost bit is 0, the integer is positive. If the leftmost bit is 1, the integer is negative.

By default, the integer data type in C is signed. This means that int var and signed int var are equivalent.

It’s important to note that computers internally represent numbers in binary form, using only 0s and 1s. In C, integer types can have different sizes. The “int” type, whether signed or unsigned, is typically 32 bits long. However, it can be 16 bits on older machines. For the sake of consistency, let’s assume that the integer type is 4 bytes or 32 bits long (since 1 byte equals 8 bits).

Calculating the Range of Signed Integers

Since we have assumed that the size of a signed integer is 32 bits, there is a specific range that defines the values it can hold. The range of a signed integer type (or int) is -2147483648 to +2147483647. This means that the smallest possible value for a signed integer with 32 bits is -2147483648, and the largest possible value is +2147483647. But how did we arrive at this range? To understand the calculation, let’s consider a 4-bit signed integer type.

Please refer to the following image to see all the possible combinations with 4 bits.

As shown in the image above, the 2’s complement representation is used to represent both positive and negative numbers. To find the 2’s complement representation of a negative number, such as -1, we follow these steps:

Step 1: Find the 1’s complement representation of 1.
To obtain the 1’s complement, we simply invert (flip) all the bits of the number.

Step 2:   Add 1 to the 1’s complement.
After finding the 1’s complement, we add 1 to it.

Using these steps, we can represent any negative number in 2’s complement form. It is also important to note that the range of an n-bit integer type can be determined using the formula:

Applying this formula to the 32-bit signed integer type, we get:

Therefore, the range of a 32-bit signed integer type is from -2147483648 to +2147483647.


Unsigned Integers

Basics

An integer that doesn’t have a sign bit is referred to as an unsigned integer. In the case of unsigned integers, the leftmost bit is considered part of the number’s magnitude.

To declare a variable as an unsigned integer, you can add the keyword “unsigned” in front of it. For instance, the variable “var” in the following example is an unsigned integer:

unsigned int var;

Calculating the Range of Unsigned Integers

Considering a 4-bit integer type and refer the following image for the possible combinations.

Applying this concept to a 32-bit unsigned integer type, the range can be calculated as follows:

Therefore, the range of a 32-bit unsigned integer type is from 0 to 4294967295.



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.