Tricky Tricks ›› Programming Tricks ›› C Languages Tricks ›› Sizeof

Sizeof

C Programming Tricks

Sizeof :

Did you know that sizeof is an ANSI C operator, not a function or macro? Therefore, it doesn't require parentheses, as seems to be the common usage. Note that the parentheses are required only if a type is used as the operand to sizeof, as in sizeof (int); it can be thought of as taking the sizeof a cast. However, this construct is almost never required because you nearly always have a properly typed data object or pointer to use instead, as in:

 {
    size_t size;
    my_type something;
 
    size = sizeof something; /* look ma, no parentheses! */
 }
 
 #include  /* malloc */
 
 {
    my_type *pointer;
 
    pointer = malloc(sizeof *pointer);
    /* ... */
 }
 

Partners