C Deciding integer type
We already know different data types in C Programming. C Provides different type of integer such as - Integer,long,short.
Now our concern is about the usage of the data types. Which integer type we should use to fulfil our requirement. Below is the answer to our question.
Different Type of data types : integer | long | byte
How One Should Decide Which Data Type of Integer we should use in order fulfil our requirement ?
- Integer data type Can be Divided can be used up to great extend depending upon application.
- Character data type can hold values up to 127
- Short int and int data type can hold values up to 32,767
- Long integer data type can hold values up to 2,147,483,647.
How to Decide ?
- You need to decide type of integer used in program at compile time. We need to analyse the program to get clear picture of the data types used.
- Suppose you need to application which will accept a number greater than 1 lac then in that case you should choose higher range of data types.
- Suppose you need to use status flags like processor i.e 1 bit for each status then you can use byte data type
- Also decide the scope of the variable and the probable use of variable used in program.
- Suppose we are using a variable as counter which will count 10 numbers then short int is sufficient to store the data.
- In some cases if we are writing program that will accept 4-Digit number then we must use integer or long integer depending upon range specified in the below table.
Reference : data type table
Type | Storage size | Value range |
---|---|---|
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
short | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | 0 to 65,535 |
long | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned long | 4 bytes | 0 to 4,294,967,295 |
Conclusion :
below is the conclusion -
SIZE(char) <= SIZE(short) <= SIZE(int) <= SIZE(long)