Sizeof - Use

Use

The sizeof operator is used to determine the amount of space a designated datatype would occupy in memory. To use sizeof, the keyword "sizeof" is followed by a type name or an expression (which may be merely a variable name). If a type name is used, it must always be enclosed in parentheses, whereas expressions can be specified with or without parentheses. A sizeof expression results in a value equal to the size in bytes of the datatype or expression (with datatypes, sizeof evaluates to the size of the memory representation for an object of the specified datatype; for expressions it evaluates to the representation size for the type that would result from evaluation of the expression, which however is not evaluated). For example, since sizeof(char) is defined to be 1 and assuming ints are 4 bytes long, the following code will print 1,4:

Because types are not known to the C preprocessor, sizeof cannot be used in #if expressions.

/* the following code illustrates the use of sizeof * with variables and expressions (no parentheses needed), * and with type names (parentheses needed) */ char c; printf("%zu,%zu\n", sizeof c, sizeof (int));

Certain standard headers such as stddef.h define size_t to denote the unsigned integral type of the result of a sizeof expression, which is always positive. The printf width specifier z should be used to format that type.

Read more about this topic:  Sizeof

Famous quotes containing the word use:

    ... it is use, and use alone, which leads one of us, tolerably trained to recognize any criterion of grace or any sense of the fitness of things, to tolerate ... the styles of dress to which we are more or less conforming every day of our lives. Fifty years hence they will seem to us as uncultivated as the nose-rings of the Hottentot seem today.
    Elizabeth Stuart Phelps (1844–1911)