In this section we will learn what the sizeof operator is and how to use it in C.
sizeof in C
In data types section we mentioned that each basic data type has a pre-decided size.
For example, int data type takes about 4 bytes of memory space or long takes about 8 bytes of memory space, etc.
But it turns out that the size of data types is system dependent!
For example, depending on the underlying architecture, in one system the int data type is 4 bytes in length but in another system it might end up taking 8 bytes of memory space.
Now, in C programming there’s an operator named sizeof by which we can see the actual and real size of each data types on each system.
C sizeof Operator Syntax:
sizeof value;
sizeof data-type;
sizeof (data-type);
sizeof(value);
Note: the use of parentheses is optional.
This operator takes one argument and that is the name of a data type or a variable of the data type that we want to get its size.
This operator will return an inter value and the unit of measurement for this value is byte. For example, if the returned value is 4, it means the size of the target data type is 4 bytes.
Example: using the sizeof operator in C
#include <stdio.h>
int main() {
int sizeOfInt = sizeof(int);
int sizeOfLong = sizeof(long);
int sizeOfLongLong = sizeof(long long);
int sizeOfShort = sizeof(short);
int sizeOfFloat = sizeof float;
int sizeOfDouble = sizeof double;
int sizeOfChar = sizeof char;
printf("int: %d , long: %d , long long: %d , short: %d , float: %d , double: %d , char: %d n" , sizeOfInt,
sizeOfLong, sizeOfLongLong,sizeOfShort, sizeOfFloat, sizeOfDouble, sizeOfChar );
return 0;
}
Result:
int: 4 , long: 4 , long long: 8 , short: 2 , float: 4 , double: 8 , char: 1
To use the sizeof operator we can simply put the name of the target data-type (or its variable) in front of the sizeof operator.
Note: As the example above shows, optionally we can put the data-type or its variable in parentheses when working with sizeof operator. But remember, the use of parentheses is optional.
This operator can be useful when you want to make sure your target data type is at least big enough to hold a particular value.
For example, let’s say we have a number that is big enough that can’t be stored in a variable of type int. So we decided to use either long or long long data type.
The problem is that there’s a chance that the long data type is actually equally the same size as int data type! So in that case we should switch to long long data type. But we don’t know for sure what system consider long as greater than int data type and what system don’t!
So what we can do here is to use the conditional statements like if with the combination of sizeof operator before declaring a variable.
Example: sizeof operator in C
#include <stdio.h>
int main() {
if (sizeof(long) > sizeof(int)){
long variableName = 1000000000;
}else{
long long variableName = 1000000000;
}
return 0;
}
As you can see, here we’ve compared the size of the long data type with the size of the int to see if the long data type has a greater memory space compared to the int. Now if that was true, then the data type of the variableName becomes long, otherwise the long long data type will be used instead.
Note: check out if section to learn more about this conditional statement.