Updated April 6, 2023
Definition of C sizeof() Operator
Sizeof() operator in C is machine-dependent functionality which varies from compiler to compiler. It can be said that it is a byte specific functionality. It helps in providing the byte and size of the variables and the number it occupies for the allocation of the variable to the memory. Sizeof() function is exclusively used to find out the exact size of a type of the variable used for programming in C.sizeof operator has a return type which returns total bytes in memory to represent the bytes. It is very useful in the implementation and development of portable applications as it is very flexible and easy to adopt for its versatility.
Syntax with Parameters:
Sizeof() operator in C has various styles for representation:
- type: type is the variable passed in the function to represent the type of data type to be used.
sizeof(type)
- variable-name: variable-name is the variable passed to the function for determining the bytes occupied by the memory.
sizeof(variable-name)
- expression: It is the parameter that is passed to the functionality for determining the bytes in the memory to compute the values for the expression.
sizeof(expression)
How Does sizeof() Operator Work in C?
sizeof() operator is a flexible and versatile operator for computation of the bytes and the memory for ingesting the required values and return those values after computation. It is not at all compiler-specific and thus varies from compiler to compiler. It compiles any unary data type and then used for computing the size of its operand. It returns the size of a variable. If a machine is 32 bits, then the memory allocation and other computation will work in the same way in a manner that the output will vary with a 32-bit machine and if it is 64 bits then it will vary with the 64 bits. Also, if the sizeof operator passes a parameter as expression then it will first parse the entire regular expression and then it will return an output with that size of the expression. Similarly, for the other parameters like type and variable-name, it works the same it will take the parameter as type then it will point to the data type as int, char, or float to be taken into consideration for the function. Even the same works for the variable name the value for the variables is also computable. To compute the number of bytes needed when the variable is assigned as an array or the linked list everything gets calculated using the sizeof () operator seamlessly.
Examples of sizeof() in C
Following are the examples are given below:
Example #1
This program demonstrates the sizeof operator to be used with the primitive data type as an argument to be passed to get a value of the data type.
Code:
#include<stdio.h>
intmain() {
int m = 80;
float n = 5.2;
printf("size of int becomes: %d\n", sizeof(m));
printf("size of float becomes %fu\n", sizeof(n));
printf("size of char becomes: %ld\n", sizeof(char));
return 0;
}
Output:
Example #2
This program is used to demonstrate the sizeof() operator where the sizeof() operator will function with the expression and will have a return type as the regular expression itself after passing from the size of operator as a parameter.
Code:
#include <stdio.h>
intmain()
{
int p = 15;
float f = 18.20;
int q = 32;
double r = 10.34;
printf("Size of Regular expression becomes %lu", sizeof(p + (f - q )*r));
return 0;
}
Output:
Example #3
This program is used to demonstrate the sizeof() operator compatible with the variables assigned and then finding the number of bytes and values that is needed for allocation to the memory as shown in the output.
Code:
#include <stdio.h>
intmain() {
char m_var1 = 26;
int p_var2 = 'i';
double o_var3 = 15.99;
printf("size of the character variable assigned %c\n", sizeof(m_var1));
printf("size of the integer variable assigned %d\n", sizeof(p_var2));
printf("size of the double or float variable assigned%f\n", sizeof(o_var3));
return 0;
}
Output:
Example #4
This program is performed for demonstrating the sizeof operator function by passing the user-defined values as a parameter and then calculating the value of the number of values.
Code:
#include<stdio.h>
structrubik_cube
{
intcube_no ;
char color;
};
intmain() {
structrubik_cube d;
printf("Total number of cubes embedded within the rubik cube with color %c\n", sizeof(d));
}
Output:
Example #5
This program is used to find the size of the array dynamically using sizeof () operator which is used dynamically allocating the memory to the elements within the array as shown in the output.
Code:
#include <stdio.h>
intmain()
{
intarr[] = { 10,19,24,0,6,42,78,60};
printf("Total elements of array list :%u ", sizeof(arr) / sizeof(arr[5]));
return 0;
}
Output:
Example #6
This program demonstrates the memory allocation for a float or double value which can be used in the various operating systems as sizeof () operator varies compiler to compiler as shown.
Code:
#include<stdio.h>
#include<stdlib.h>
intmain() {
double *s;
s = (double*)malloc(6 * sizeof(double));
return 0;
}
Output:
No output
Advantages of using sizeof() in C
There are several advantages of using the operators in C same is the case with sizeof() operator in C. Some of the advantages of using the sizeof() operators in C are as follows:
- To find and calculate the number of elements in an array and the type of fashion it is arranged, the sizeof the operator comes as a savior as it is used for the calculation of elements in an array automatically.
- For dynamic allocation of memory while getting allocated to a block it is a great added advantage as it helps in the memory allocation with enough memory and size of the memory in that particular machine which may be difficult to calculate and keep a hold of.
Conclusion
Sizeof() operator is a flexible and versatile operator in C programming language as It helps in streamlining for allocation of memory and it even helps in the calculation of requisite bytes of memory and the value with desired return types of data types, variables, and Expressions. sizeof operator in C can easily perform dynamic allocation of memory easily.
Recommended Articles
This is a guide to sizeof() in C. Here we also discuss the definition and how does sizeof() operator work in c? along with different examples and its code implementation. You may also have a look at the following articles to learn more –