Programmer Coding

sizeof() operator in C Language

sizeof() operator

The size operator is the most frequently used operator in C. It is a compile-time unary operator that calculates the size of its operand. Returns the size of the variable. It can be used for any data type, floating point type, floating point type, and measurement type conversion.

When sizeof() is used with a file type, it returns only the amount of memory allocated for the file type. Output may vary between different systems; for example, a 32-bit system may display different output and a 64-bit system may display the same information differently.

Example

#include <stdio.h>

int main() {
int a = 50;

printf(“Size of variable a: %zu bytes\n”, sizeof(a));
printf(“Size of int data type: %zu bytes\n”, sizeof(int));
printf(“Size of char data type: %zu bytes\n”, sizeof(char));
printf(“Size of float data type: %zu bytes\n”, sizeof(float));
printf(“Size of double data type: %zu bytes\n”, sizeof(double));

return 0;
}

Output

Size of variable a: 4 bytes
Size of int data type: 4 bytes
Size of char data type: 1 bytes
Size of float data type: 4 bytes
Size of double data type: 8 bytes

When the sizeof() is used with an expression, it returns the size of the expression. Here is an example.

#include <stdio.h>

int main() {
char a = ‘S’;
double b = 4.65;

printf(“Size of variable a: %zu bytes\n”, sizeof(a));
printf(“Size of an expression: %zu bytes\n”, sizeof(a + b));

int s = (int)(a + b);
printf(“Size of explicitly converted expression: %zu bytes\n”, sizeof(s));

return 0;
}

Output

Size of variable a: 1 bytes
Size of an expression: 8 bytes
Size of explicitly converted expression: 4 bytes

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top