'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [-Wformat=] [duplicate]

I keep getting compile warnings but I don't know how to fix it:

'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [

The program runs fine but I still get the compile warnings:

/* Sizeof.c--Program to tell byte size of the C variable */
#include <stdio.h>

int main(void) {
    printf("\nA Char is %d bytes", sizeof( char ));
    printf("\nAn int is %d bytes", sizeof( int ));
    printf("\nA short is %d bytes", sizeof( short ));
    printf("\nA long is %d bytes", sizeof( long ));
    printf("\nA long long is %d bytes\n", sizeof( long long ));
    printf("\nAn unsigned Char is %d bytes", sizeof( unsigned char ));
    printf("\nAn unsigned int is %d bytes", sizeof( unsigned int));
    printf("\nAn unsigned short is %d bytes", sizeof( unsigned short ));
    printf("\nAn unsigned long is %d bytes", sizeof( unsigned long ));
    printf("\nAn unsigned long long is %d bytes\n",
            sizeof( unsigned long long ));
    printf("\nfloat is %d bytes", sizeof( float ));
    printf("\nA double is %d bytes\n", sizeof( double ));
    printf("\nA long double is %d bytes\n", sizeof( long double ));

    return 0;

}

Solution 1:

sizeof returns size_t you need to use %zu for the format string instead of %d. The type of unsigned integer of size_t can vary (depending on platform) and may not be long unsigned int everywhere, which is covered in the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 4:

The value of the result is implementation-defined, and its type (an unsigned integer type) is size_t, defined in (and other headers).

Also note that using the wrong format specifier for printf is undefined behavior, which is covered in section 7.19.6.1 The fprintf function, which also covers printf with respect to format specifiers says:

If a conversion specification is invalid, the behavior is undefined.248) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

Update

Visual Studio does not support the z format specifier:

The hh, j, z, and t length prefixes are not supported.

the correct format specifier in this case would be %Iu.

Solution 2:

The compiler is warning you that you may suffer a loss of precision. That is, the format specifier that you're using to print a sizeof, %d, is not capable of printing the full range of size_t. Change %d to %zu and your warning will go away.

Solution 3:

i had the same problem in Linux. the same program runs without error in windows (means '%d' worked without error), but for linux i had to replace all the '%d' with'%lu' to run the program.