C (and C++) operator returning the size in bytes of an object or a type. There is some potential ambiguity regarding just how large a byte is (C was historically run on machines with word size not a multiple of 8 bits!); this is resolved by decreeing sizeof char == 1 (that is, a char is 1 byte). CHAR_BITS is always there to tell you how many bits to a byte.

The "argument" passed to sizeof should be the name of a type, or any lvalue (possibly -- and confusingly -- a constant lvalue). sizeof returns an unsigned integer value (of type size_t, which is implementation-defined).

Use sizeof to determine how much space to allocate for data structures. For example, struct foo *bar = malloc(100*sizeof(*bar)) makes bar point to an array of 100 struct bars.

Using sizeof imposes no run time overhead; the "return value" is treated exactly as an integer literal. Of course, the actual size of almost any object is highly implementation-dependent, so it is always better to use sizeof rather than to try to guess an object's size.

Since sizeof is an operator, not a preprocessor construct, it cannot be used in #if conditions.

Log in or registerto write something here or to contact authors.