Data types are declarations for variables. It specifies the type and size of data that a variable can store. Each data type requires different amounts of memory and has some specific operations which can be performed over it.
There are the following data types in C
- Basic Data Type
- Derived Data Type
- Enumeration Data Type
- Void Data Type
Data Types | Example |
Basic Data Types | int, char, float, double |
Derived Data Type | array, pointer, structure, union |
Enumeration Data Type | enum |
Void Data Type | void |
Basic Data Types:-
It is also known as Primary Data Type. They are arithmetic types (integer-based and floating-point based). The memory size of the basic data types may change according to 32 or 64-bit operating system.
Type | Memory Size | Format Specifier |
int | Atleast 2, usually 4 | %d |
char | 1 | %c |
float | 4 | %f |
double | 8 | %lf |
short int | 2 | %hd |
unsigned int | at least 2, usually 4 | %u |
long int | at least 4, usually 8 | %ld |
long long int | 8 | %lld |
unsigned long int | 4 | %lu |
unsigned long long int | 8 | %llu |
signed char | 1 | %c |
unsigned char | 1 | %c |
long double | at least 10, usually 16 | %Lf |
We can use the sizeof()
operator to check the size of a variable on a particular platform.
C program to find the sizes of each of C’s data types in our system:–
#include <stdio.h> #include <stdint.h> #include <stdbool.h> int main( void ) { printf( "Size of C data types:\n\n" ); printf( "Type Bytes\n\n" ); printf( "--------------------------------\n"); printf( "char %lu\n" , sizeof( char ) ); printf( "unsigned char %lu\n" , sizeof( unsigned char ) ); printf( "short %lu\n" , sizeof( short ) ); printf( "int %lu\n" , sizeof( int ) ); printf( "unsigned %lu\n" , sizeof( unsigned ) ); printf( "long %lu\n" , sizeof( long ) ); printf( "unsigned long %lu\n" , sizeof( unsigned long ) ); printf( "long long %lu\n" , sizeof( long long ) ); printf( "int64_t %lu\n" , sizeof( int64_t ) ); printf( "unsigned long long %lu\n" , sizeof( unsigned long long ) ); printf( "float %lu\n" , sizeof( float ) ); printf( "double %lu\n" , sizeof( double ) ); printf( "long double %lu\n" , sizeof( long double ) ); printf( "\n" ); return 0; }
int:- Integers are whole numbers that can have both zero, positive and negative values but no decimal values.
** The keyword unsigned uses to store all the value of the number and always positive or Zero.
int a = 5;
Char:- It is used for declaring character type variables and stores a single character.
char i = u;
float:- It is used to store decimal numbers with single precision.
Double:- It is used to store decimal numbers with double precision.
Void Data Type:-
It means nothing or no value available. If a function is not returning anything, its return type should be void
.
** We cannot create variables of void data type.
Derived Data Types:-
Data types that are derived from fundamental data types are derived types.
Enumerated Data Types:-
It can only assign certain discrete integer values throughout the program.