Friday, January 4, 2013

Using sizeof to Determine the Size of a Data Type in ANSI C

The size of an ANSI C data type is dependent on the compiler.  However it can be easily determined by using the sizeof operator.

Here's a  example on how to do it:
#include<stdio.h>

typedef struct
{
    int  field1;
    long field2;
    char field3[30];
}ExampleStructure;

typedef union
{
    int field1;
    double field2;
}ExampleUnion;

typedef enum
{
    enum1,
    enum2,
    enum3
}ExampleEnum;

int main(void)
{
    printf("char          : %d bytes\n"
           "short int     : %d bytes\n"
           "int           : %d bytes\n"
           "long int      : %d bytes\n"
           "long long int : %d bytes\n"
           "float         : %d bytes\n"
           "double        : %d bytes\n"
           "long double   : %d bytes\n"
           "ExampleStruct : %d bytes\n"
           "ExampleUnion  : %d bytes\n"
           "ExampleEnum   : %d bytes\n"
           "void*         : %d bytes\n"
           "char*         : %d bytes\n"
           "short int*    : %d bytes\n"
           "int*          : %d bytes\n"
           "long int*     : %d bytes\n"
           "long long int*: %d bytes\n"
           "float*        : %d bytes\n"
           "double*       : %d bytes\n"
           "long double*  : %d bytes\n"
           "ExampleStruct*: %d bytes\n"
           "ExampleUnion* : %d bytes\n"
           "ExampleEnum*  : %d bytes\n",
           sizeof(char),
           sizeof(short),
           sizeof(int),
           sizeof(long),
           sizeof(long long),
           sizeof(float),
           sizeof(double),
           sizeof(long double),
           sizeof(ExampleStructure),
           sizeof(ExampleUnion),
           sizeof(ExampleEnum),
           sizeof(void*),
           sizeof(char*),
           sizeof(short*),
           sizeof(int*),
           sizeof(long*),
           sizeof(long long*),
           sizeof(float*),
           sizeof(double*),
           sizeof(long double*),
           sizeof(ExampleStructure*),
           sizeof(ExampleUnion*),
           sizeof(ExampleEnum*));
    return 0;
}
/*Output
char          : 1 bytes
short int     : 2 bytes
int           : 4 bytes
long int      : 4 bytes
long long int : 8 bytes
float         : 4 bytes
double        : 8 bytes
long double   : 12 bytes
ExampleStruct : 40 bytes
ExampleUnion  : 8 bytes
ExampleEnum   : 4 bytes
void*         : 4 bytes
char*         : 4 bytes
short int*    : 4 bytes
int*          : 4 bytes
long int*     : 4 bytes
long long int*: 4 bytes
float*        : 4 bytes
double*       : 4 bytes
long double*  : 4 bytes
ExampleStruct*: 4 bytes
ExampleUnion* : 4 bytes
ExampleEnum*  : 4 bytes
 */
The program above calculates the size of each type and outputs it to console. The output was obtained using the gcc compiler. If you use other compiler, you might obtain different results.

TIP 1: The size of a structure is determined by the size of its members plus the padding bits. In our case we have 2 (field1) + 4 (field2) + 30 (field3) + 4 (padding) = 40 bytes. 
TIP 2: The size of an union is determined by the size of its largest member (in our case long field2). 
TIP 3: The size of an enumeration corresponds in most cases to the size of int.
TIP 4: All pointer types have the same size which usually corresponds to the size of int.
You can read more about the assumptions you can make when dealing with primitive data types here.

References:

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger