sizeof is a unary operator in the programming languages C and C++. It generates the storage size of an expression or a data type, measured in the number of char-sized units. Consequently, the construct sizeof (char) is guaranteed to be 1. The actual number of bits of type char is specified by the preprocessor macro , defined in the standard include file limits.h. On most modern computing platforms this is eight bits. The result of sizeof has an unsigned integer type that is usually denoted by size_t.
The operator has a single operand, which is either an expression or the cast of a data type, which is a data type enclosed in parentheses. Data types may not only be primitive types, such as integer and floating-point types, but also pointer types, and compound datatypes (unions, structs, and C++ classes).

Purpose
Many programs must know the storage size of a particular datatype. Though for any given implementation of C or C++ the size of a particular datatype is constant, the sizes of even primitive types in C and C++ may be defined differently for different platforms of implementation. For example, runtime allocation of array space may use the following code, in which the sizeof operator is applied to the cast of the type int:

int *pointer = malloc(10 * sizeof (int));

In this example, function malloc allocates memory and returns a pointer to the memory block. The size of the block allocated is equal to the number of bytes for a single object of type int multiplied by 10, providing space for ten integers.
It is generally not safe to assume the size of any datatype. For example, even though most implementations of C and C++ on 32-bit systems define type int to be four octets, this size may change when code is ported to a different system, breaking the code. The exception to this is the data type char, which always has the size 1 in any standards-compliant C implementation. In addition, it is frequently difficult to predict the sizes of compound datatypes such as a struct or union, due to padding. The use of sizeof enhances readability, since it avoids unnamed numeric constants (magic numbers).
An equivalent syntax for allocating the same array space results from using the dereferenced form of the pointer to the storage address, this time applying the operator to a pointer variable:
int *pointer = malloc(10 * sizeof *pointer);
Use
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 : char c; printf ("%zu,%zu\n", sizeof c, sizeof (int));
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. template <typename... Args> std::size_t GetSize (Args&&... args) { /* Get size of parameter pack.*/ std::size_t Count= sizeof... (Args); return Count; }
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:
#include <stdio.h>
int main(int argc, char **argv) { char buffer[10]; /* Array of 10 chars */
/* Copy at most 9 characters from argv[1] into buffer, * null-terminate the buffer. */ snprintf(buffer, sizeof buffer, "%s", argv[1]);
return 0; }





