Data types in C language
What is data type
As you have seen in the previous article, a variable is declared as given below...int a; //variable declaration
- In the above program code snippet, int is called a data type and a is a variable of that data type.
- Data type describe the content a variable can store.
- Data type also explains how data will be stored (the organization) in memory.
Data types can be classified into two broad categories...
- Primitive (Basic) data type
- Composite (Secondary) data types / User-defined data types
In this article we will only discuss primitive data types, User-defined data types will be discussed later.
Primitive data types
Data types which are defined by the system/programming language are called primitive data type. These data type defines a single entity, which can't be divided further.
List of primitive data types along with their memory occupied and the allowed range are described as below...
- Integral data types
- Real data types
- Character data types
Integral data types
Data types | Keyword used | Memory Occupied | Allowed Range |
---|---|---|---|
Short Integers | short / short int / int | 2 bytes | -32768 to 32767 |
Long integers | long/long int | 4 bytes | -2147483648 to 2147483647 |
- Short Integers can be defined by using keywords short/short int/int.
- Most of the c compilers treat int data type equivalent to short but it is not true for all compilers. Some compilers treat it equivalent to long. But in any compiler, the thing which is common is as follows.
- short <= int <= long
Real data types
Data types | Keyword used | Memory Occupied | Allowed Range |
---|---|---|---|
float | float | 4 bytes (6 digits precision) | 3.4e-38 to 3.4e38 |
double | double(14 digits precision) | 8 bytes | 1.7e-308 to 1.7e+308 |
long double | long double(14 digits precision) | 10 bytes | 3.4e-4932 to 1.1e+4932 |
Character data types
Data types | Keyword used | Memory Occupied | Allowed Range |
---|---|---|---|
character | char | 1 byte | -128 to 127 |
Range Calculation
As shown above, the range of each integral data type is shown... How to calculate this range? This article demonstrates you...
- An integer occupies 2 bytes (16 bits) in memory and the memory map of the integer data type is shown in the above figure.
- As shown in the above figure the leftmost bit of integer is called MSB or sign bit, which stores the sign of the number.
- As we know in computers memory everything is stored in the form of 0 and 1.
- So in the sign bit,1 means -ve number and 0 means +ve numbers
- Rest of the 15 bits are used to store data and there can 215 combinations are possible to organize in 15 places. so the range maximum numbers that can be made in this memory are 215.
- These 215 combinations are made with a +ve sign and -ve sign, and the range will be -215 to 215 - 1 would result -32768 to 32767.
- So the general formula to calculate rang is as given.
-2 (n-1) to 2 (n-1) - 1 where n is no. of bits occupied.
unsigned keyword
- We know that integer occupies 2 bytes(16 bits) among them 1 bit is used for storing the sign.
- If the number is positive and we don't want to sacrifice the sign bit to store the sign, then the variable declaration should be prefixed with the unsigned keyword.
- The benefit of creating a variable unsigned is that the range will be double.
Data type | Range |
---|---|
unsigned int a; | 0 to 65535 (0 to 216-1) |
unsigned char a; | 0 to 65535 (0 to 28-1) |
Comments
- Comment Is The Non-Executable Part Of The Program.
- The comment Is written Between /*----------*/.
- /*-----------*/ Is Known As Multiline Comment.
- Comment Is Used For Documentation Or Increase The Understanding Or Readability.
- Nested Comment Are Not allowed.
Previous | Back to C Programming index | Next |
Comments