Integer data types in C Language
This Integer data types in C Language post explains clearly how many integer types present in C language, difference between signed and unsigned integer types, how memory allocates to integers in memory.
- C provides several standard integer types, from small magnitude to large magnitude numbers: char, short int, int, long int.
- Each type can be signed or unsigned.
- Signed types can represent positive and negative numbers where as unsigned can represent zero and positive numbers.
- The short int is a signed small integer.
- C allows abbreviated or longer names for the same type
signed short int
For the unsigned version use one of these:
unsigned short int.
Note : In the declaration of variable if we don’t specify whether the variable is signed or unsigned, by default it is signed type hence it allows both Positive and Negative values.
Memory representation of signed and unsigned using 1 byte memory allocation:
- 1 byte is equal to 8 bits memory
- Using 8 bit location we can store a maximum value of 255.
- As we need to store both positive and negative values in case of signed integer types, we need to divide the maximum value with 2.
- In case of signed type, only 7 bit locations used to store the data and 1 bit is to store signed(+ve or –ve) bit.
- The following diagram represents clearly how memory will be allocated to store the data.