Solution for Exercise 18-4

ex1804

#include <stdio.h>

int main()
{
    int array[5];

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

Notes

* For a Macintosh, or some other Unix variant, change the %u conversion character used in printf() at Line 7 to %ld.

* 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.

* Here is sample output:

* 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. That's because the size of a double is 8 bytes. You can change Line 5 in the code, build and run to confirm.