sizeof
Sometimes in programming, it may be required to know the size of a variable. In that case, we can use the sizeof operator given in ‘C’ programming language.
Many operators are available in ‘C’ programming language like arithmetic operators, logical operators, bitwise operators, etc.
sizeof Operator in C:
- The Sizeof operator of C programming language is used to find out the size of a variable.
- It can be used with any data type, including primitive types (such as
int
,float
,char
) and user-defined types (such as structures and arrays). - The output of the Sizeof operator may be different on different machines because it is compiler dependent.
Syntax
sizeof(type)
Here, type can be a data type or a variable. The sizeof operator returns the size of the type or the variable in bytes.
For example, to determine the size of an int in bytes, you can use:
int size = sizeof(int);
Similarly, to determine the size of a variable x of type float you can use:
float x; int size = sizeof(x);
The sizeof operator is evaluated at compile-time, so the result is known before the program is executed. Keep in mind that the size returned by sizeof may vary depending on the implementation and the platform you are using.
It’s important to note that when used with arrays, the sizeof operator gives the total size of the array in bytes. For example, if you have an array arr of 10 integers, the expression sizeof(arr) would give you the size of the entire array, which would be 10* sizeof(int).
Additionally, the sizeof operator can also be used with the sizeof expression, which allows you to determine the size of a type without needing an actual variable. For example:
size_t size = sizeof(int[10]);
In this case, the sizeof(int[10]) expression gives you the size of an array of 10 integers without the need for a named variable.
To understand more about this operator, let’s do an exercise.
#include <stdio.h> int main(void) { printf("Size of char data type is %d\n",sizeof(char)); return 0; }
In the onlinegdb tool, I created a new project Sizeof. And here in the main.c, let’s write a small program to print the Sizeof char data type.
So, I use the printf statement.
printf(“Size of char data type = %d\n”, sizeof(char));
Here, I write “size of char data type = %d\n” . %d because I want to print the integer value and let me give \n to enter a new line and end that message with a double quote(“).
Then, give the argument for %d. So here, let’s use the sizeof operator. To use the sizeof operator, you have to write the keyword sizeof. sizeof is a standard C programming language keyword. After that, you have to use the parenthesis. And here, inside this parenthesis, you have to mention the operand. The operand can be a variable name, or it can be a data type name. In this case, let’s use the name of the data type. Let’s use char.
Let’s run the program and see the output. The size of char data type = 1. So, that is one byte, as shown in Figure 1.
Now let’s try this same printf with short.
#include <stdio.h> int main(void) { printf("Size of char data type is %d\n",sizeof(char)); printf("Size of short data type is %d\n",sizeof(short)); return 0; }
Let’s run the program. It is printing as the expected output size of short data type = 2. (Figure 2)
Now let’s try for some more data types. Let me mention here int, long, and long long.
#include <stdio.h> int main(void) { printf("Size of char data type is %d\n",sizeof(char)); printf("Size of short data type is %d\n",sizeof(short)); printf("Size of int data type is %d\n",sizeof(int)); printf("Size of long data type is %d\n",sizeof(long)); printf("Size of long long data type is %d\n",sizeof(long long)); return 0; }
Let’s run. And here we can see that(Figure 3) the size of char is 1; short is 2; int is 4; long data type is 8, and long long data type is 4 in this machine. And actually it uses GCC compiler. So, the online GDB tool actually uses a GCC tool to generate the code, and it also uses an X86 machine to run the code. I mean, they have that kind of server at the backend.
Here, the operand can be a variable name. For example, let me create a variable. Let me create a variable of type long long. long long myLongHistory = 900; So, myLongHistory is a variable of type long long.
So, you can also mention the myLongHistory variable name as an operand of sizeof operator. Let me print that.
printf(“size of long long variable = %d\n”, sizeof(myLongHistory));
#include <stdio.h> int main(void) { long long myLongHistory = 900; printf("Size of char data type = %d\n",sizeof(char)); printf("Size of short data type = %d\n",sizeof(short)); printf("Size of int data type = %d\n",sizeof(int)); printf("Size of long data type = %d\n",sizeof(long)); printf("Size of long long variable = %d\n",sizeof(myLongHistory)); return 0; }
Let’s run. Here we can see that(Figure 4), we see the same result. Either you can use the data type name or a variable name.
And you can also do this(Figure 5). So here, you can see that I am creating a new variable called size to store the value which the sizeof operator returns. Here, the value which is being returned from the sizeof operator, I’m assigning that value or storing that value into another variable called size.
And after that, I can also print the value of the size variable.
#include <stdio.h> int main(void) { long long myLongHistory = 900; char size = sizeof(myLongHistory); printf("Size of char data type = %d\n",sizeof(char)); printf("Size of short data type = %d\n",sizeof(short)); printf("Size of int data type = %d\n",sizeof(int)); printf("Size of long data type = %d\n",sizeof(long)); printf("Size of long long variable = %d\n", size); return 0; }
Sizeof operators are used to find out the size of a variable or a data type. So, by using the sizeof operator, you’ll come to know how many bytes will be consumed by that variable inside the computer memory.
So, another tedious method to know all these things is to read the compiler user manual. Instead of doing that, you can use the sizeof operator.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1