The output seems to depend on the compiler, as far as I've played with this. In any event, there are the usual warnings you would expect; but the output is shuffled around with the 3.14 replaced by a wholly different number (usually a large integer).
I am not too concerned about the type-clash error here, but I am more concerned about the shuffling of numbers. On my Ubuntu 24.4 system,
I get:
2 1 1015677328
Can anyone tell me why the order of the numbers have changed? That is really what I am most interested in.
@paulk, there's no strangeness here, the compiler reveals all...ignore at your peril
gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
gcc -o p2 p2.c
p2.c: In function ‘main’:
p2.c:2:23: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]
2 | int main() { printf("%d %d %d\n", 3.14, 2, 1); return 0; }
| ~^ ~~~~
| | |
| int double
| %f
I skimmed through the article, and, so I invoked "undefined behaviour". Ah, that might explain why I got a big number for 3.14 - printf doesn't do rounding, and so it just went momentarily insane for a microsecond and gave a really big integer instead.
So, I would suppose that included in "undefined behaviour" is changing the order of the parameters. Or is there a logical reason that the misbehaved parameter got moved to the back and the other two shifted up.
Oh, I saw that error as well. The type clash was deliberate. I was trying to see if it would compile, or (like Java) throw some kind of exception, but instead, it gave me the numbers with some out of order, and one weird one where the float would have been if I used %f.
I understand the warning, and that %d is something I shouldn't do if I needed %f or to use round(3.14) instead.
I just thought the shuffling of numbers was strange and unnecessary, and had no problem with getting numbers like -2014752368 for using a bad format specifier.