Solution for Exercise 18-4

ex1804

#include <stdio.h>

int main()
{
    int array[5];

    printf("The array has a size of %lu.\n",sizeof(array));
    return(0);
}

Output

The array has a size of 20.

Notes

* The array doesn't need to be assigned values to make the sizeof operator work. That's because space is allocated to store 5 int values when the program runs. The sizeof operator merely reports on the amount of space allocated.

* An int array of 5 elements occupies 20 bytes of storage because each int value uses 4 bytes: 5 × 4 = 20.

* If the array were declared as a double of 5 elements it would use 40 bytes of storage because the size of a double is 8 bytes. You can change Line 5 in the code, build and run to confirm.