Data Types
Sizeof
sizeof reports how many bytes a type or value uses on the current platform.
sizeof
`sizeof(int)` returns the byte count for one `int`.
byte count
Multiplying by a count estimates how many bytes a sequence of values needs.
Sizeof
sizeof_types.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
int main(void) {
int count = 3;
int bytes = (int)(count * sizeof(int));
printf("int=%zu\n", sizeof(int));
printf("bytes=%d\n", bytes);
return 0;
}
#include <stdio.h>
int main(void) {
int count = 5;
int bytes = (int)(count * sizeof(int));
printf("int=%zu\n", sizeof(int));
printf("bytes=%d\n", bytes);
return 0;
}
#include <stdio.h>
int main(void) {
int count = 8;
int bytes = (int)(count * sizeof(int));
printf("int=%zu\n", sizeof(int));
printf("bytes=%d\n", bytes);
return 0;
}
count ← 3, bytes ← 12
3int main(void) {4 int count→ 3 = 3; //@count=5, 85 int bytes→ 12 = (int)(count3 * sizeof(int));67 printf("int=%zu\n", sizeof(int));8 printf("bytes=%d\n", bytes12);9 return 0;10}outputint=4 bytes=12
count ← 5, bytes ← 20
3int main(void) {4 int count→ 5 = 5;5 int bytes→ 20 = (int)(count5 * sizeof(int));67 printf("int=%zu\n", sizeof(int));8 printf("bytes=%d\n", bytes20);9 return 0;10}outputint=4 bytes=20
count ← 8, bytes ← 32
3int main(void) {4 int count→ 8 = 8;5 int bytes→ 32 = (int)(count8 * sizeof(int));67 printf("int=%zu\n", sizeof(int));8 printf("bytes=%d\n", bytes32);9 return 0;10}outputint=4 bytes=32
Follow the Bytes
countstarts at3.- This local run reports
sizeof(int)as4. bytes = count * sizeof(int).- The default byte count is
3 * 4, which is12. - The program prints
int=4andbytes=12. | count | localsizeof(int)| bytes | | --- | --- | --- | | 3 | 4 | 12 | | 5 | 4 | 20 | | 8 | 4 | 32 |
Exercise: sizeof_types.c
Reproduce bytes=12, then use count 5 and 8 with this local sizeof(int)=4 result to predict each byte count.