Solution for Exercise 18-1

ex1801

#include <stdio.h>

int main()
{
    char c = 'c';
    int i = 123;
    long l = 12345678910;
    float f = 98.6;
    double d = 6.022E23;

    printf("char\t%lu\n",sizeof(c));
    printf("int\t%lu\n",sizeof(i));
    printf("long\t%lu\n",sizeof(l));
    printf("float\t%lu\n",sizeof(f));
    printf("double\t%lu\n",sizeof(d));
    return(0);
}

Output

char    1
int     4
long    8
float   4
double  8

Notes

* I didn't need to assign values to the variables in Lines 5 through 9; the sizeof operator works on variables whether they're initialized or not.

* The %lu conversion character is required to report the unsigned long int value returned by sizeof.

* Some compilers may let you use the %zd conversion character, which is designed to display size_t variables returned by sizeof.

* Some systems may typedef the size_t value returned by sizeof as an unsigned int, not an unsigned long int. If the compiler bemoans the %lu placeholder, replace it with %u or even try %zd.