Implicit conversion to C?

What's going on here:

printf("result = %d\n", 1);
printf("result = %f\n", 1);

outputs:

result = 1
result = 0.000000

If, before trying to print them, I guarantee the type of these variables, it works fine. Why doesn't the second print statement get implicitly converted to 1.00000?

+3
source share
5 answers

In the second case, you have a mismatch between your format string and the type of the argument - therefore the result is undefined behavio (u) r.

+8
source

, 1 1.0, , printf "" C , () (const char *). " " , "" - , printf , .

, , , printf (, , , ). , vararg, (, ).

, "variable", .

+4

undefined. Int float

+2

: printf ++. Printf - C, , , .

If you need any kind of validation of the actual type, you should use streams and strings - real C ++ alternatives for the old old C-style font.

+1
source

Interesting, apparently, fine if your put is '1.0'

I assume that printf gets the address of the variable, it does not know what it is. But I would have thought that the compiler would have decency to warn you.

0
source

Source: https://habr.com/ru/post/1775281/


All Articles