What sizeof returns
sizeof is an operator in C and C++ that gives the size of a type or an object, in bytes. You write sizeof(int) or sizeof value, and the compiler replaces the expression with a number. It is an operator, not a function, although the parentheses make it look like one.
The operand can be a type name, which needs parentheses, or an expression, which does not:
int value = 42;
std::cout << sizeof(int) << "\n"; // a type name needs parentheses
std::cout << sizeof value << "\n"; // an expression can omit them
Is sizeof() in bytes? Yes. The result counts bytes, where a byte is the size of a char. The C and C++ standards require a char to be at least 8 bits, and on nearly all current systems it is exactly 8.
The result has type std::size_t (size_t in C, declared in <stddef.h>). That is an unsigned integer type, so it cannot hold a negative value. For most operands the answer is known at compile time, so asking for a size costs nothing at runtime.
Sizes of basic types
The table below shows typical sizes on mainstream 64-bit desktop systems. The standard sets minimum requirements for these types rather than exact sizes, so the compiler and the platform decide the final numbers.
| Type | Typical size on 64-bit desktop systems |
|---|---|
char |
1 byte, always |
short |
2 bytes |
int |
4 bytes |
long |
8 bytes on Linux and macOS, 4 bytes on Windows |
long long |
8 bytes |
float |
4 bytes |
double |
8 bytes |
| Pointer | 8 bytes |
Don’t rely on a table for your own code. Ask the compiler instead:
#include <iostream>
int main() {
std::cout << "char: " << sizeof(char) << "\n";
std::cout << "int: " << sizeof(int) << "\n";
std::cout << "double: " << sizeof(double) << "\n";
}
On a typical 64-bit desktop system, this prints 1, 4 and 8.
Two common questions come up here. Is char always 1 byte in C? Yes. The standard defines sizeof applied to a char as 1, so sizeof(char) is always 1 in both C and C++.
The int row needs a caveat. Is int 2 or 4 bytes in C? It depends on the platform. The standard requires int to hold at least -32767 to 32767, which means at least 16 bits. With 8-bit bytes, 2 bytes is therefore the smallest possible size, and 4 bytes is the usual choice on most current systems.
For floating-point types, the usual answer to what is the size of a float data type is 4 bytes, with double at 8 bytes on the same systems.
Using sizeof on arrays
For an array, sizeof gives the total number of bytes used by all its elements:
#include <iostream>
int main() {
int scores[10] = {};
std::cout << sizeof(scores) << "\n"; // 40 when int is 4 bytes
std::cout << sizeof(scores) / sizeof(scores[0]) << "\n"; // 10 elements
}
Dividing the total by the size of one element gives the number of elements. This works only when the name refers to the array itself, in the scope where it was declared. From C++17, std::size (in <iterator>) returns the same count more directly, and it also works on standard containers.
When an array turns into a pointer
When you pass an array to a function, it decays to a pointer to its first element. Inside the function, sizeof reports the size of that pointer, not the size of the array:
#include <iostream>
void report(int values[]) {
std::cout << sizeof(values) << "\n"; // pointer size, e.g. 8, not 40
}
int main() {
int scores[10] = {};
report(scores);
}
The function cannot learn the element count from sizeof. Pass the count as a separate parameter, or use a container such as std::vector. If you are learning how to pass an array to a function in C, keep this in mind: the size does not travel with the array.
Strings and string literals
A string literal includes its terminating null character, and sizeof counts it:
#include <iostream>
int main() {
char name[] = "Ada";
const char* label = "Ada";
std::cout << sizeof(name) << "\n"; // 4: three letters and '\0'
std::cout << sizeof(label) << "\n"; // pointer size, not 4
std::cout << sizeof("Ada") << "\n"; // 4
}
sizeof answers a question about the type, not about the text. To measure a string at runtime, use size() on a std::string, or strlen() from <cstring> on a C string.
Structs and padding
The size of a struct can be larger than the sum of its members. The compiler adds padding so that each member starts at a suitably aligned address:
struct Pair {
char letter;
int number;
};
The members take 1 byte and 4 bytes, which adds up to 5. On a typical 64-bit system, though, sizeof(Pair) is commonly 8. Don’t try to predict a struct’s size by adding up its members. The same rule applies to an array of structs in C, where each element takes the full struct size.
Printing sizeof results
In C, print a size_t with the %zu format code, which C99 and later support:
#include <stdio.h>
int main(void) {
printf("%zu\n", sizeof(int));
return 0;
}
Using %d for a size_t value is undefined behavior, and it can print the wrong number. Readers who ask what is %s and %d in C usually need that answer first. In short, %d expects an int, and %s expects a pointer to a null-terminated string. C++ with std::cout avoids the problem, because the stream knows the type of the value.
Common mistakes
Expecting sizeof to count elements through a pointer. A pointer, including one from new[], has pointer size:
int* data = new int[10];
std::cout << sizeof(data) << "\n"; // pointer size, not 40
delete[] data;
Expecting sizeof to measure a container’s contents. sizeof on a std::vector reports the size of the vector object itself, which holds a few internal pointers. It does not include the heap memory for the elements. Use size() for the element count.
Expecting the operand to run. The operand of sizeof is not evaluated, so sizeof(i++) leaves i unchanged. The exception is a variable length array in C, where the operand is evaluated.
Mixing signed and unsigned values. sizeof yields an unsigned type, and a negative int in the same comparison is converted to a very large size_t:
int offset = -1;
if (offset < sizeof(int)) {
// false: -1 becomes a very large size_t
}
Compilers often warn about this mix, so check the warnings and cast explicitly when you need the comparison.
Key takeaways
sizeofis an operator that returns the size in bytes as asize_t.sizeof(char)is always 1. Other sizes depend on the platform, so ask the compiler.- For an array,
sizeofgives the total bytes. Divide by the size of one element to get the count. - An array passed to a function becomes a pointer, so
sizeofreports the pointer size. - A struct can be larger than the sum of its members because of padding.