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.
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.
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.
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; %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.
- unsigned short a;
- unsigned short int a;
- %u format specifier is used for these above two unsigned declaration.
Now the next Question arises is What is format specifier? Read More...
Original Post: www.dheerajpatidar.com