+1 vote
in Programming Languages by (8.9k points)
What is the purpose of the sizeof() operator and how to use it?

1 Answer

+2 votes
by (56.8k points)
selected by
 
Best answer

In C language, the sizeof() operator is used to determine the size of a variable in bytes.

Here is an example to show how to use this operator in your program:

#include <stdio.h>
int main() {
    int num;
    float f;
    char c;
    char name[10];
    printf("Size of int: %lu bytes\n", sizeof(num));
    printf("Size of float: %lu bytes\n", sizeof(f));
    printf("Size of char: %lu bytes\n", sizeof(c));
    printf("Size of array: %lu bytes\n", sizeof(name));
    return 0;
}
The above code returns the following output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of char: 1 bytes
Size of array: 10 bytes

...