1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// SIZEOF
int main(int argc, char* argv[]) {
int n = 65;
printf("sizeof(65) == %zd\n", sizeof(65));
printf("sizeof(n) == %zd\n", sizeof(n));
long long_number = 65L;
printf("sizeof(long_number) == %zd\n", sizeof(long_number));
printf("sizeof(int) == %zd\n", sizeof(int));
printf("sizeof 32767 == %zd\n", sizeof 32767);
int array[] = { 10, 11, 12 };
printf("sizeof(array) == %zd\n", sizeof(array));
// type of 'array' is int[3] and an int[3] occupies 12 bytes.
printf("array contains %zd elements\n", sizeof(array) / sizeof(array[0]));
// 12 bytes / 4 bytes (on our platform)
// WARNING: This only works with arrays on the stack (i.e., declared with []).
// It does not work with an address of an array on the heap or data segment.
int* array_on_heap = malloc(3 * sizeof(*array_on_heap));
array_on_heap[0] = 10;
array_on_heap[1] = 11;
array_on_heap[2] = 12;
printf("sizeof(array_on_heap) / sizeof(array_on_heap[0]) == %zd / %zd (!!!)\n",
sizeof(array_on_heap), sizeof(array_on_heap[0]));
printf("sizeof(array_on_heap) / sizeof(array_on_heap[0]) == %zd (!!!)\n",
sizeof(array_on_heap) / sizeof(array_on_heap[0]));
return EXIT_SUCCESS;
}
// I may use sizeof(TYPE) for illustrations today. Do not use that in your code.
// Use only sizeof(EXPRESSION).
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.