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

count
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;
}
  1. 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
  1. 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
  1. 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

  1. count starts at 3.
  2. This local run reports sizeof(int) as 4.
  3. bytes = count * sizeof(int).
  4. The default byte count is 3 * 4, which is 12.
  5. The program prints int=4 and bytes=12. | count | local sizeof(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.