In C, Every variable has an affiliated data type. Every data type requires a different size of memory and has different specific operations that can be performed over it.
There are two different types of data types in the C language:
- Primary data types
- Derived data types
Primary Data Types
Primary data types are the data types that are already defined in the C language. These data types are also known as built-in or primitive data types. It is the most fundamental data type in the C language.
Examples :- int(integer), float(floating point), char(character), etc.
Derived Data Types
Derived data types are a collection of fundamental data types joined together. For example:- array, structure, pointers, etc.
Some of the frequently used data types C are given as follows,
- char: It stores a single character. Char data type occupies 1 byte of memory in almost all C compilers.
- int: An int data type is used to store an integer.
- float: float data type is used to store decimal numbers also known as floating numbers. It stores decimal numbers with single precision.
- double: double data type also stores decimal numbers like a float but with double precision.
- Array: A collection of primary data types like int, float, and char.
- struct: Structure allows you to combine different data types together.
- long: It stores integers with more range. It occupies 4 bits on a 32-bit compiler
Different data types have different memory allocations. These memory allocations are compiler-dependent. A list of ranges of some data types along with the format specifier and memory allocation on 32-bit GCC compiler is given as follows,
C Data Type | Size (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 | 2.3E-308 to 1.7E+308 | %lf |
Example
#include <stdio.h> int main() { int a = 4; char c = 'd'; printf("%d\n", a); printf("%c", c); return 0; }
Output:
4
d
sizeof() Operator
sizeof() operator can be used to compute the size of any data type in C.
Example
#include <stdio.h> int main() { int b = 5; printf("Size of a : %d\n", sizeof(b)); printf("Size of data type, int : %d\n", sizeof(int)); printf("Size of data type, char : %d\n", sizeof(char)); printf("Size of data type, float : %d\n", sizeof(float)); printf("Size of data type, double : %d\n", sizeof(double)); return 0; }
Output:
Size of a : 4
Size of data type, int : 4
Size of data type, char : 1
Size of data type, float : 4
Size of data type, double : 8
void Data Type
void means null. The null data type is used to specify a function that returns no value.
Example
#include <stdio.h> void main() { printf("This function returns no value"); }
Output:
This function returns no value