C - Data Types
One of the most important parts of learning any programming language is to understand what are the available data types, and how data is stored, accessed and manipulated in that language. Based on the data type of a variable, the operating system allocates memory to the variable and decides what can be stored in the reserved memory.
There are three types of data types in C which are categorized below:
Category | Data Types |
---|---|
Basic Data Type | integer, character, floating point, double floating point, etc. |
Derived Data Type | array, pointer, function, reference, etc. |
User Defined Data Type | structure, union, enum, typedef, etc. |
The table below describes basic data types of C in detail:
Category | Data Types |
---|---|
character | char |
Integer | int |
Floating Point | float |
Double Floating Point | double |
The Void Type | void |
There are several types of modifier which can be used to modify the data types in C.
- signed
- unsigned
- short
- long
The table below describes the variable type, memory size, format specifier, and maximum and minimum value which can be stored in the variable.
Category | Memory Size | Format Specifier | Range |
---|---|---|---|
char | 1 byte | %c | -128 to 127 |
unsigned char | 1 byte | %c | 0 to 255 |
signed char | 1 byte | %c | -128 to 127 |
short int | 2 byte | %hd | -32,768 to 32,767 |
unsigned short int | 2 byte | %hu | 0 to 65,535 |
signed short int | 2 byte | %hd | -32,768 to 32,767 |
int | 4 byte | %d | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 byte | %u | 0 to 4,294,967,295 |
signed int | 4 byte | %d | -2,147,483,648 to 2,147,483,647 |
long int | 8 byte | %ld | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 8 byte | %lu | 0 to 4,294,967,295 |
signed long int | 8 byte | %ld | -2,147,483,648 to 2,147,483,647 |
long long int | 8 byte | %lld | -(263) to (263)-1 |
unsigned long long int | 8 byte | %llu | 0 to 18,446,744,073,709,551,615 |
float | 4 byte | %f | 1.2E-38 to 3.4E+38 (~6 digits precision) |
double | 8 byte | %lf | 2.3E-308 to 1.7E+308 (~15 digits precision) |
long double | 16 byte | %Lf | 3.4E-4932 to 1.1E+4932 (~19 digits precision) |
void | 1 byte | %p | void |
sizeof() function
The memory size of a data type might be different as shown in the above table, depending upon the particular platform. To find out the size of a data type, sizeof() function can be used.
#include <stdio.h> int main (){ printf("Size of char: %ld\n", sizeof(char)); printf("Size of short int: %ld\n", sizeof(short int)); printf("Size of int: %ld\n", sizeof(int)); printf("Size of long int: %ld\n", sizeof(long int)); printf("Size of float: %ld\n", sizeof(float)); printf("Size of double: %ld\n", sizeof(double)); printf("Size of void: %ld\n", sizeof(void)); return 0; }
The output of the above code will be:
Size of char: 1 Size of short int: 2 Size of int: 4 Size of long int: 8 Size of float: 4 Size of double: 8 Size of void: 1