Data Types in C
Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. It specifies the type of data that the variable can store like integer, character, floating, double, etc. The data type is a collection of data with values having fixed values, meaning as well as its characteristics.
The data types in C can be classified as follows:
|Primitive Data Types||Arithmetic types can be further classified into integer and floating data types.|
|Void Types||The data type has no value or operator and it does not provide a result to its caller. But void comes under Primitive data types.|
|User Defined DataTypes||It is mainly used to assign names to integral constants, which make a program easy to read and maintain|
|Derived types||The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types.|
Different data types also have different ranges up to which they can store numbers. These ranges may vary from compiler to compiler. Below is a list of ranges along with the memory requirement and format specifiers on the 32-bit GCC compiler.
|Data Type ||Memory (bytes) ||Range ||Format Specifier |
|short int ||2 ||-32,768 to 32,767 ||%hd |
|unsigned short int ||2 ||0 to 65,535 ||%hu |
|unsigned int ||4 ||0 to 4,294,967,295 ||%u |
|int ||4 ||-2,147,483,648 to 2,147,483,647 ||%d |
|long int ||4 ||-2,147,483,648 to 2,147,483,647 ||%ld |
|unsigned long int ||4 ||0 to 4,294,967,295 ||%lu |
|long long int ||8 ||-(2^63) to (2^63)-1 ||%lld |
|unsigned long long int ||8 ||0 to 18,446,744,073,709,551,615 ||%llu |
|signed char ||1 ||-128 to 127 ||%c |
|unsigned char ||1 ||0 to 255 ||%c |
|float ||4 ||1.2E-38 to 3.4E+38||%f |
|double ||8 ||1.7E-308 to 1.7E+308||%lf |
|long double ||16 ||3.4E-4932 to 1.1E+4932||%Lf |
The integer data type in C is used to store the whole numbers without decimal values. Octal values, hexadecimal values, and decimal values can be stored in int data type in C. We can determine the size of the int data type by using the sizeof operator in C. Unsigned int data type in C is used to store the data values from zero to positive numbers but it can’t store negative values like signed int. Unsigned int is larger in size than signed int and it uses “%u” as a format specifier in C programming language. Below is the programming implementation of the int data type in C.
- Range: -2,147,483,648 to 2,147,483,647
- Size: 2 bytes or 4 bytes
- Format Specifier: %d
Note: The size of an integer data type is compiler-dependent, when processors are 16-bit systems, then it shows the output of int as 2 bytes. And when processors are 32-bit then it shows 2 bytes as well as 4 bytes.
Integer value with positive data: 9 Integer value with negative data: -9 Integer value with an unsigned int data: 89 Integer value with an long int data: 99998
Character data type allows its variable to store only a single character. The storage size of the character is 1. It is the most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.
- Range: (-128 to 127) or (0 to 255)
- Size: 1 byte
- Format Specifier: %c
Value of a: a Value of a after increment is: b Value of c: c
In C programming float data type is used to store floating-point values. Float in C is used to store decimal and exponential values. It is used to store decimal numbers (numbers with floating point values) with single precision.
- Range: 1.2E-38 to 3.4E+38
- Size: 4 bytes
- Format Specifier: %f
9.000000 2.500000 0.000200
A Double data type in C is used to store decimal numbers (numbers with floating point values) with double precision. It is used to define numeric values which hold numbers with decimal values in C. Double data type is basically a precision sort of data type that is capable of holding 64 bits of decimal numbers or floating points. Since double has more precision as compared to that float then it is much more obvious that it occupies twice the memory as occupied by the floating-point type. It can easily accommodate about 16 to 17 digits after or before a decimal point.
- Range: 1.7E-308 to 1.7E+308
- Size: 8 bytes
- Format Specifier: %lf
123123123.000000 12.293123 2312312312.123123
Void Data types
The void data type in C is used to specify that no value is present. It does not provide a result value to its caller. It has no values and no operations. It is used to represent nothing. Void is used in multiple ways as function return type, function arguments as void, and pointers to void.
// function return type void void exit(int check); // Function without any parameter can accept void. int print(void); // memory allocation function which // returns a pointer to void. void *malloc( size_t size);
We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:
The size of int data type : 4 The size of char data type : 1 The size of float data type : 4 The size of double data type : 8
To check your knowledge of data types in C, go through the Quiz on Data Types.