Ich bin mit gcc (Ubuntu 4.8.4-2ubuntu1 ~ 14.04.3) 4.8.4 und Compilierung folgenden Code:gcc Autokorrektur-Format in printf
void main()
{
float f= 10.0;
char *str = "hello";
printf("f=%.1f str=%s\n",str, f);
}
Compiler Warnung zeigt, dass erwartet wird:
prac.c: In function 'main':
prac.c:8:1: warning: format '%f' expects argument of type 'double', but argument 2 has type 'char *' [-Wformat=]
printf("f=%.1f str=%s\n",str, f);
^
prac.c:8:1: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'double' [-Wformat=]
Während der Ausführung dieses Programms wird die Ausgabe jedoch automatisch korrigiert.
# ./a.out
f=10.0 str=hello
Wie das passiert. Welches Feature des Compilers macht dies. Jede Möglichkeit, diese Autokorrektur zu deaktivieren.
Undefiniertes Verhalten == Undefiniertes Verhalten. (Es hat nichts mit dem Compiler zu tun, und es gibt keine "Autokorrektur" - es ist nur ein Zufall des ABI.) –
@PaulR: Rollcredits. –