sizeof
sizeof()
is a compile-time function that return the size,
in bytes, of a variable or a type.
char c; int i; float f; double d; printf("%zu\n", sizeof(c)); printf("%zu\n", sizeof(i)); printf("%zu\n", sizeof(f)); printf("%zu\n", sizeof(d)); printf("%zu\n", sizeof(double));
1 4 4 8 8
The printf
format for sizeof
is %zu
.
sizeof
an arraysizeof
give you the size of an array, in bytes.
int a[13]; printf("%zu\n", sizeof(a));
52
int a[13]; printf("%zu\n", sizeof(a)/sizeof(a[0]));
13
void bar(int b[]) { printf("b: %zu\n", sizeof(b)); } int main() { int a[1000]; bar(a); printf("a: %zu\n", sizeof(a)); }
c.c: In function 'bar': c.c:2: warning: 'sizeof' on array function parameter 'b' will return size of 'int *' c.c:1: note: declared here b: 8 a: 4000
&
with scanf
.
void bar(int b[]);
void bar(int *b);
void bar(int *b) { printf("b: %zu\n", sizeof(b)); } int main() { int a[1000]; bar(a); printf("a: %zu\n", sizeof(a)); }
b: 8 a: 4000
b
is a pointer.
All pointers on this computer are that size.