sizeof operator

Using sizeof operator, we can get the size of a variable or a datatype.

Example

sizeof(int) will be 4. Integer allocates 4 bytes to store the value.

sizeof(char) will be 1. Character allocates 1 byte to store the value.

sizeof(float) will be 4. float allocates 4 bytes to store the value.

sizeof(double) will be 8. double allocates 8 bytes to store the value.




Size of datatypes

Example

#include<stdio.h>

int main()
{
    printf("sizeof(int)    = %d\n",sizeof(int));
    printf("sizeof(char)   = %d\n",sizeof(char));
    printf("sizeof(float)  = %d\n",sizeof(float));
    printf("sizeof(double) = %d\n",sizeof(double));

    return 0;
}




Size of variables

Example

#include<stdio.h>

int main()
{
    char c;
    double d;

    printf("sizeof variable c = %d\n",sizeof(c));
    printf("sizeof variable d = %d\n",sizeof(d));
    //10 is an int value and it will take 4 bytes
    printf("sizeof(10)        = %d\n",sizeof(10));

    return 0;
}