In this article, I’m presenting Integer data type in C Programming. Please read my previous article where I have discussed in short about All Data Type in C language.
Here I’m presenting full detail about Integer data type in C with examples.
- 1 Integer Data Types in C Language with Examples
- 1.1 Using 2 bytes of memory what is the minimum and maximum value we can store?
- 1.2 Declaration of signed short Data Type in C Language:
- 1.3 Declaration of unsigned short Data Type in C Language:
- 2 Conclusion
Integer Data Types in C Language with Examples
As we have already discussed in previous post that Integer data type has 6 categories as shown in the below image.
Initially there are 3 types of int which are short, int, and long. These three types are categorised into signed and unsigned types individually, so that there are 6 types of integer Data Type.
Now one question arises here:
What are signed and unsigned data type?
Using signed data type both positive and negative values we can store, whereas 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 that I showing you. Here I am taking two bytes of memory. 1 byte equals 8 bits, and 2 bytes equals 16 bits. And it takes only binary values 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.
If we place all ones in all 16 bits, the value we will get is 65535. So, the maximum integer value we can store in 2 bytes is 65535 as shown in the below image.
So using 2 byte of memory, the minimum & maximum value we can store is 0 & 65535 respectively.
Now come to the signed & unsigned integer 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.
Now, the signed short data type can store both positive and negative values. So, here just divide the value by 2, we will get 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.
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
Following are the examples of declaring signed short variables in c language:
short int a;
signed short a;
signed short int a;
%d is the format specifier of all above signed declaration.
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;
%u format specifier is used for these above two unsigned declaration.
Look at the below image to understand clearly.
Now the next Question arises is What is format specifier?
If you want to read or display the information, formatting is very important. In which format do you have to read and print information. That you have to specify to the computer using a format specifier.
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.
short a = 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.
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.
short a = 32769;
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.
unsigned short a = –5;
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.
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.
unsigned short a = 65538;
printf(” %d“, a);
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.
unsigned short a = –32772;
printf(” %d“, a);
Output: 32764 32764
This is all about Integer data type in C. If you have any double please mention it in comment. So that you will get your query answer as soon as possible.
I hope you have got useful information from this post.
Thanks for visit!
< Previous Post
Next Post >
Sizeof and Limits of Data Type in C