What is the use of the sizeof operator in C?
sizeof returns the size in bytes of a type or a variable. It is calculated at compile time and has the type size_t.
#include <stdio.h>
int main(void) {
int arr[10];
printf("%zu\n", sizeof(int));
printf("%zu\n", sizeof(arr));
printf("%zu\n", sizeof(arr) / sizeof(arr[0]));
return 0;
}
Output:
Output of the above program is
4
40
10
The last line is the common way to find the number of elements of an array. Use %zu to print the value.
Learn more about the similar topics:
| Tutorials |
|---|
| C Keywords |
| C Variables |
| C Data Types |
| C Constants |
| C printf and scanf |
| C Escape Sequences |
| C Format Specifiers |