Integer Data Types in C

Integer Data Types in C Language with Examples

In this article, I am going to discuss Integer Data Types in C Language with examples. Please read our previous article where we discussed the basics of Data Types in C. We already discussed in our previous article that the Integer data type is divided into six classifications as shown in the below image.

Integer Data Types in C Language with Examples

The first classification is short type. Short again divided into two sub-classifications, it is signed short and unsigned short. Either it is a signed short or is unsigned short, it occupies two bytes of memory.

What is a signed data type?

Using signed data type both positive and negative values we can store.

What is the unsigned data type?

Using unsigned data types, we can store only positive values.

Using 2 bytes of memory what is the minimum and maximum value we can store?

To understand this, look at the memory allocation process. Here I am taking two bytes of memory. 1 byte equals 8 bits, so 2 bytes equals 16 bits. And it takes only binary values i.e. 0 and 1. Now, if we place zeros in all the 16 places then the value will be zero which is the minimum we can store in 2 bytes memory location as shown in the below image.

Using 2 bytes of memory what is the minimum and maximum value we can store?

If we place all ones in all 16 bits, the value is 65535. So, the maximum integer value we can store in 2 bytes is 65535 as shown in the below image.

Using 2 bytes of memory what is the minimum and maximum value we can store?

So, using 2 bytes of memory, the minimum integer value we can store is 0 and the maximum integer value we can store is 65535. Now, let us come to the signed and unsigned data type. 1 byte is 8 bits and 2 bytes is 16 bits. The unsigned short data type can store only positive integer values ranges from 0 to 65535 as shown in the below image.

Integer Data Types in C Language

The signed short data type can store both positive and negative values. So, here just divide the value by 2 i.e. 65536/2 which will result in 32768. The negative or minus value always starts from -1, -2, up to -32768. And the positive value starts from 0, 1, and up to 32767 for signed short as shown in the below image.

Integer Data Types in C Language

Declaration of signed short Data Type in C Language:

If you do not specify whether the variable is a signed variable or an unsigned variable, by default that is a signed variable and can accept both positive and negative values. Following are the examples of declaring signed short variables in c language

short a;
short int a;
signed short a;
signed short int a;

For All the above four signed declarations, the format specifier is %d. In these many ways, we can declare a signed variable in our C program.

Declaration of unsigned short Data Type in C Language:

In unsigned declarations, we must specify explicitly that these are unsigned declarations. Following are the two ways to declare unsigned short data type in c language.

unsigned short a;
unsigned short int a;

For these two unsigned declarations, the format specifier is %u. So, these are the total of six declarations about a short data type. First, four comes under the signed short declarations and the last two comes under the unsigned declarations. For better understanding, please have a look at the below image.

Declaration short Data Type in C Language

The next important thing is what is format specifier?

If you want to read the information or if you want to display the information, formatting is very important. In which format do you have to read information and in which format do you have to print the information. That you have to specify to the computer using a format specifier.

Example:

Now we will see some programs. I just want to print a small value on the console. We are writing the program and the execution starts from the main method. I am declaring one variable; it is a short variable and then prints it on the console.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    short a = 10;
    system("cls");
    printf("%d", a);
    return 0;
}

Output: 10

In the above example, short a = 10; declaring a variable of type short and assigned with value 10. We want to print the value of the variable on the console, so here, we are using printf function which belongs stdio.h header file. Inside the double quotes, we have to write the format specifier. As variable a is a signed variable, so the format specifier is %d and we want to print the value of a. Value of a is 10, so program output will be 10.

Note: To clear the screen in the Linux system use system(“clear”) function which is included in stdlib.h, and if you use this in the window use the system(“cls”).

This is a very simple program and here, format specifier is very, very important. With the help of a format specifier only we are reading the elements and printing the elements.

Complex Examples using short Data Type in C Language:

Next, we will see some complex programs. Consider the limits of signed short data type in the form of a circle. The range of minimum and maximum values of signed short data type is -32768 to +32767 as shown in the below image.

Any value you want to count, whether +VE values and -VE values, the count is always going to start from 0. Positive values are going to be count in a clockwise direction and the maximum value is 32767. The negative values count is going to be the anti-clockwise direction and it will start from 0, -1, -2, up to -32768. For better understanding please have a look at the below diagram.

Complex Examples using short Data Type in C Language

Based on the above diagram we will see one program. I just want to print a big value on the console. We are writing the program and execution starts from the main method. I am declaring one short variable.

#include <stdio.h>
int main()
{
    short a = 32769;
    printf("%d", a);
    return 0;
}

Output: -32767

Why we are getting -32767, not 32769. As the value is a positive number so the count will start clockwise from 0 and reach the maximum value of 32767. Now, please observe, what is the next value of 32767 in clockwise direction, it is -32768 (32767+1 = 32768) and what the next value, it is -32767 (32768+1 = 32769) and that is printed on the console. So, in this case, when the value exceeds, it will print a garbage value. In this case, it is not giving any error instead it prints a garbage value.

Unsigned short Integer Data Type Example in C Language:

Now we will see one more program. Please have a look at the below example. Here, we are declaring a variable of unsigned short type but assigning a negative value i.e. -5. We know, unsigned short only take positive values. Let us first execute the program and see the output.

#include <stdio.h>
int main()
{
    unsigned short a = -5;
    printf("%d", a);
    return 0;
}

Output: 65531

To understand why we are getting 65531 in the output, we need to understand the unsigned short data type in the form of a circle. The range of minimum and the maximum values is 0 to 65535 for unsigned short and it moves in a clockwise direction for +VE values and anti-clockwise direction for -VE values as shown in the below image.

Unsigned short Integer Data Type Example in C Language

Now, as we are assigning -5 to the unsigned variable, so it will start counting in an anti-clockwise direction. So, it will start from 0, then 65535 for -1, 65534 for -2, 65533 for -3, 65532 for -4, and 65531 for -5 and it will store 65531 in the memory location, and that is what you can see in the memory output.

As we are using the %u format specifier, it will look into an unsigned circle diagram for the value of a. In the memory location, the value of a will be stored as 65531. Because -5 is not in the range of unsigned short variable, so it will look in an anti-clockwise direction.

Example: unsigned short Integer in C Language

Now we will see one more program. In the below program, we declare an unsigned variable and assigned it a value 65538.

#include <stdio.h>
int main()
{
    unsigned short a = 65538;
    printf("%u", a);
    printf(" %d", a);
    return 0;
}

Output: 2 2

Let us understand why we are getting 2 as the output. To understand this, first, we need to understand what value is going to be stored in the memory location for the variable a. So, here, the variable a data type is unsigned, so it will check the unsigned circle which is start from 0 and ends with 65535 and counts the numbers in a clockwise direction. So, it will start from 0, and goes up to 65535 in the circle. What is the next value in the clockwise direction of 65535, it is 0. So, 0 for 65536, 1 for 65537, and 2 for 65538. So, in the memory location, it will store the value 2.

Now, in the first printf statement, we have used the %u format specifier, so it will check the unsigned short circle and found value 2 is therein and hence it will print that value. In the second printf statement, we have used %d format specifier, so it will check the signed short circle and found value 2 is therein and hence it will also print that value in the console.

Now we will see one more program. In the below program, we declare one unsigned short variable and assigned it with a value -32772.

#include <stdio.h>
int main()
{
    unsigned short a = -32772;
    printf("%u", a);
    printf(" %d", a);
    return 0;
}

Output: 32764 32764

Now it is a task for you to find out why we are getting 32764 as output?

In the next article, I am going to discuss the Character Data Type in C Language with examples. Here, in this article, I try to explain Integer Data Types in C Language with examples and I hope you enjoy this Integer Data Types in C Language article.

Leave a Reply

Your email address will not be published.