How to get the size of data types in C language
/*
Example: Get the datatype size in C language
*/
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char byte;
int main(int argc, char const *argv[]) {
printf(" char is %2d bytes \n", sizeof(char));
printf(" char * is %2d bytes \n", sizeof(char *));
printf(" signed char is %2d bytes \n", sizeof(signed char));
printf("unsigned char is %2d bytes \n", sizeof(unsigned char));
printf("\n");
printf(" int is %2d bytes \n", sizeof(int));
printf(" int * is %2d bytes \n", sizeof(int *));
printf(" short int is %2d bytes \n", sizeof(short int));
printf(" long int is %2d bytes \n", sizeof(long int));
printf(" long int * is %2d bytes \n", sizeof(long int *));
printf(" signed int is %2d bytes \n", sizeof(signed int));
printf(" unsigned int is %2d bytes \n", sizeof(unsigned int));
printf("\n");
printf(" float is %2d bytes \n", sizeof(float));
printf(" float * is %2d bytes \n", sizeof(float *));
printf(" double is %2d bytes \n", sizeof(double));
printf(" double * is %2d bytes \n", sizeof(double *));
printf(" long double is %2d bytes \n", sizeof(long double));
return 0;
}
output example:
char is 1 bytes
char * is 8 bytes
signed char is 1 bytes
unsigned char is 1 bytes
int is 4 bytes
int * is 8 bytes
short int is 2 bytes
long int is 4 bytes
long int * is 8 bytes
signed int is 4 bytes
unsigned int is 4 bytes
float is 4 bytes
float * is 8 bytes
double is 8 bytes
double * is 8 bytes
long double is 16 bytes
So:
In C, the size of int type is 4 bytes.
In C, the size of long int is 4 bytes.
In C, the size of float type is 4 bytes.
In C, the size of double type is 4 bytes.
In C, the size of char type is 4 bytes.