The sizeof operator tells you how many bytes a variable or a data type consumes in memory. This note covers how to write it, what you can put inside it, and why its answer can change from one machine to another. It continues from Int And Long Data Types, where the sizes of int and long were shown to depend on the compiler.
What the sizeof Operator Does
C has many operators, like arithmetic operators and bitwise operators. The sizeof operator is one of them. It is used to find out the size of a variable or a data type, meaning how many bytes that variable consumes inside the computer’s memory.
The answer is compiler dependent. The same expression can print a different number on a different machine, because each compiler sizes types to suit the hardware it builds code for. This is the same rule explained in Int And Long Data Types: int may be 2 or 4 bytes, and long may be 4 or 8 bytes, depending on the compiler.
How to Write It
sizeof(operand)Every part of this, one by one:
sizeof: a standard C keyword, not a function. You write it as-is.( ): the parentheses that hold the operand.operand: what you want the size of. It can be a data type name likeint, or a variable name likefile_len.
Read plainly, the line says: give me the number of bytes used by this type or this variable.
Checking the Size of Every Data Type
Here is a program that prints the size of the integer data types. The variable file_len is declared so we can test a variable name as the operand later:
#include <stdio.h>
int main()
{
long file_len = 20000;
printf("Size of char: %d\n", sizeof(char));
printf("Size of short: %d\n", sizeof(short));
printf("Size of int: %d\n", sizeof(int));
printf("Size of long: %d\n", sizeof(long));
printf("Size of long long: %d\n", sizeof(long long));
// Printing size of variables
printf("Size of variable: %d\n", sizeof(file_len));
return 0;
}Walk through it one printf at a time:
- The first five lines pass a data type name to
sizeof, so each one prints the size of that type. %dprints the value returned bysizeofas an integer, and\nends the line.- The last printf passes the variable name
file_len, sosizeofprints the size of that variable.
On GCC, running on an x86 machine, this prints:
Size of char: 1
Size of short: 2
Size of int: 4
Size of long: 8
Size of long long: 8
Size of variable: 8
Read each line as a plain statement:
charis 1 byte, which matches the 8 bit layout shown in Char Data Type.shortis 2 bytes, which is the one fixed size explained in Short Int Data Type.intis 4 bytes andlongis 8 bytes on this compiler, which is one of the allowed choices from Int And Long Data Types.long longis also 8 bytes here.- The variable
file_lenhas typelong, sosizeof(file_len)gives the same answer assizeof(long), which is 8.
These numbers are for this machine only
The tool used in the lecture is OnlineGDB, which runs the GCC compiler on an x86 server at the backend. On a compiler that gives
int2 bytes, the same program would print 2 forint. The sizes of the types themselves come from the compiler, so always runsizeofon your own machine to be sure.
The Operand Can Be a Variable Name
The operand does not have to be a type. Any variable works, and sizeof returns the size of that variable’s type:
long long myLongHistory = 900;
printf("Size of long long variable: %d\n", sizeof(myLongHistory)); // prints 8The output is the same as sizeof(long long), because myLongHistory was declared as a long long. So the rule is simple: you can pass the name of the data type or the name of a variable of that type, and you get the same number.
Storing the Result in a Variable
sizeof returns a value, so you can store that value in a variable and use it later, like any other value:
int size = sizeof(long long);
printf("Size of long long variable: %d\n", size); // prints 8Step by step:
sizeof(long long)runs first and produces the value 8.- That value is stored into the variable
size. - The printf then prints
size, which shows 8.
Why This Operator Is Useful
You could learn each type’s size by reading the compiler’s user manual, but that is tedious. The sizeof operator gives you the answer directly for the compiler in front of you, which is the exact machine your program will run on.
Previous: Int And Long Data Types