Using incorrect format specifier in printf

suggest change

Using an incorrect format specifier in the first argument to printf invokes undefined behavior. For example, the code below invokes undefined behavior:

long z = 'B';
printf("%c\n", z);

Here is another example

printf("%f\n",0);

Above line of code is undefined behavior. %f expects double. However 0 is of type int.

Note that your compiler usually can help you avoid cases like these, if you turn on the proper flags during compiling (-Wformat in clang and gcc). From the last example:

warning: format specifies type 'double' but the argument has type
      'int' [-Wformat]
    printf("%f\n",0);
            ~~    ^
            %d

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents