Sizeof Operator

C provides the special unary operator sizeof to determine the size in bytes of any data type.

The number of bytes used to store a particular data type may vary between systems. When writing programs that depend on data type sizes, and that will run on several computer systems, use sizeof to determine the number of bytes used to store the data types.

Example 1

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
    char c;
    short s;
    int i;
    long l;
    float f;
    double d;
    long double ld;
    int array[ 20 ], *ptr = array;
 
    printf( " sizeof c = %d"
    "\tsizeof(char) = %d"
    "\n sizeof s = %d"
    "\tsizeof(short) = %d"
    "\n sizeof i = %d"
    "\tsizeof(int) = %d"
    "\n sizeof l = %d"
    "\tsizeof(long) = %d"
    "\n sizeof f = %d"
    "\tsizeof(float) = %d"
    "\n sizeof d = %d"
    "\tsizeof(double) = %d"
    "\n sizeof ld = %d"
    "\tsizeof(long double) = %d"
    "\n sizeof array = %d"
    "\n sizeof ptr = %d\n",
    sizeof c, sizeof( char ), sizeof s,
    sizeof( short ), sizeof i, sizeof( int ),
    sizeof l, sizeof( long ), sizeof f,
    sizeof( float ), sizeof d, sizeof( double ),
    sizeof ld, sizeof( long double ),
    sizeof array, sizeof ptr );
 
    getchar();
    return 0;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.