Sizeof

sizeof

Sizeof is a unary operator in C that generates the size of a variable or datatype.

sizeof (char) // 1 Byte, type must be enclosed in parenthesis

The actual number of bits of the types is specified by prepocessor macros in limits.h.

/* the following code fragment illustrates the use of sizeof
 * with variable and expressions (no parens needed),
 * and with type names (parens needed)
 */
 char c;
 printf("%zu, %zu", sizeof c, sizeof(int))

The output for this program should be 1,4

When applied to a fixed length data type or variables, expressions with the operator sizeof are evaluated during program compilation; they are replaces by a constant result.

Structure padding

Man structures include padding between the different datatypes in memory so it may be bigger than the sum of the sizeof the types in the structure.

Written on September 15, 2017