char
. Consequently, the expression sizeof(char)
evaluates to 1. The number of bits of type char
is specified by the preprocessor macro CHAR_BIT
, defined in the standard include file limits.h. On most modern computing platforms this is eight bits. The result of ''sizeof'' is an unsigned integer that is usually typed as size_t.
The operator accepts a single operand which is either a data type expressed as a cast the name of a data type enclosed in parentheses or a non-type expression for which parentheses are not required.
Purpose
Many programs must know the storage size of a particular datatype. Though for any givenUse
The operator ''sizeof'' produces the required memory storage space of its operand when the code is compiled. The operand is written following the keyword ''sizeof'' and may be the symbol of a storage space, e.g., a variable, an expression, or a type name. Parentheses for the operand are optional, except when specifying a type name. The result of the operator is the size of the operand in bytes, or the size of the memory storage requirement. For expressions, it evaluates to the representation size for the type that would result from evaluation of the expression, which is not performed. For example, since ''sizeof (char)'' is defined to be 1 and assuming the integer type is four bytes long, the following code fragment prints : Certain standard header files, such as ''stddef.h'', define '' size_t'' to denote the unsigned integral type of the result of a ''sizeof'' expression. The ''printf'' width specifier ''z'' is intended to format that type. ''sizeof'' cannot be used in C preprocessor expressions, such as , because it is an element of the programming language, not of the preprocessor syntax, which has no data types. The following example in C++ uses the operator ''sizeof'' with variadic templates. ''sizeof'' can be used with variadic templates in C++11 and above on a parameter pack to determine the number of arguments.Application to arrays
When ''sizeof'' is applied to the name of an array, the result is the number of bytes required to store the entire array. This is one of the few exceptions to the rule that the name of an array is converted to a pointer to the first element of the array, and is possible just because the actual array size is fixed and known at compile time, when the ''sizeof'' operator is evaluated. The following program uses ''sizeof'' to determine the size of a declared array, avoiding a buffer overflow when copying characters:Incomplete types
''sizeof'' can only be applied to "completely" defined types. With arrays, this means that the dimensions of the array must be present in its declaration, and that the type of the elements must be completely defined. For ''struct''s and ''union''s, this means that there must be a member list of completely defined types. For example, consider the following two source files: Both files are perfectly legal C, and code in can apply ''sizeof'' to ''arr'' and . However, it is illegal for code in to do this, because the definitions in are not complete. In the case of ''arr'', the code does not specify the dimension of the array; without this information, the compiler has no way of knowing how many elements are in the array, and cannot calculate the array's overall size. Likewise, the compiler cannot calculate the size of because it does not know what members it is made up of, and therefore cannot calculate the sum of the sizes of the structure's members (and padding). If the programmer provided the size of the array in its declaration in , or completed the definition of by supplying a member list, this would allow the application of ''sizeof'' to ''arr'' or in that source file.Object members
C++11 introduced the possibility to apply the ''sizeof'' parameter to specific members of a class without the necessity to instantiate the object to achieve this. The following example for instance yields and on most platforms.Variadic template packs
C++11 introduced variadic templates; the keyword ''sizeof'' followed byImplementation
When applied to a fixed-length datatype or variable, expressions with the operator ''sizeof'' are evaluated during program compilation; they are replaced by constant result-values. The C99 standard introduced variable-length arrays (VLAs), which required evaluation for such expressions during program execution. In many cases, the implementation specifics may be documented in anStructure padding
When calculating the size of any object type, the compiler must take into account any required data structure alignment to meet efficiency or architectural constraints. Many computer architectures do not support multiple-byte access starting at any byte address that is not a multiple of the word size, and even when the architecture allows it, usually the processor can fetch a word-aligned object faster than it can fetch an object that straddles multiple words in memory. Therefore, compilers usually align data structures to at least aSee also
* typeof * offsetofReferences
{{reflist C (programming language) C++ Articles with example C code Operators (programming) Unary operations