W jaki sposób poniższy program wyświetla ' C89 'po skompilowaniu w trybie C89 i` C99' po skompilowaniu w trybie C99?
Znalazłem ten program C z sieci:
#include <stdio.h>
int main(){
printf("C%d\n",(int)(90-(-4.5//**/
-4.5)));
return 0;
}
Interesującą rzeczą w tym programie jest to, że gdy jest kompilowany i uruchamiany w trybie C89, drukuje C89
, A gdy jest kompilowany i uruchamiany w trybie C99, drukuje C99
. Ale nie jestem w stanie rozgryźć jak ten program działa.
Czy możesz wyjaśnić jak działa drugi argument printf
w powyższym programie?
123
Author: 200_success, 2015-06-29
3 answers
C99 zezwala na komentarze w stylu //
, C89 nie. Więc do przetłumaczenia:
C99:
printf("C%d\n",(int)(90-(-4.5 /*Some comment stuff*/
-4.5)));
// Outputs: 99
C89:
printf("C%d\n",(int)(90-(-4.5/
-4.5)));
/* so we get 90-1 or 89 */
132
Author: Paul Rubel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-29 17:32:32
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-29 17:32:32
Komentarz linii //
jest wprowadzony od C99. Dlatego Twój kod jest równy temu w C89
#include <stdio.h>
int main(){
printf("C%d\n",(int)(90-(-4.5/
-4.5)));
return 0;
}
/* 90 - (-4.5 / -4.5) = 89 */
I równe temu w C99
#include <stdio.h>
int main(){
printf("C%d\n",(int)(90-(-4.5
-4.5)));
return 0;
}
/* 90 - (-4.5 - 4.5) = 99*/
25
Author: ikh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-29 12:18:25
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-29 12:18:25
Ponieważ //
komentarze istnieją tylko w C99 i późniejszych standardach, kod jest równoważny z następującym:
#include <stdio.h>
int main (void)
{
int vers;
#if __STDC_VERSION__ >= 201112L
vers = 99; // oops
#elif __STDC_VERSION__ >= 199901L
vers = 99;
#else
vers = 90;
#endif
printf("C%d", vers);
return 0;
}
Poprawny kod to:
#include <stdio.h>
int main (void)
{
int vers;
#if __STDC_VERSION__ >= 201112L
vers = 11;
#elif __STDC_VERSION__ >= 199901L
vers = 99;
#else
vers = 90;
#endif
printf("C%d", vers);
return 0;
}
9
Author: Lundin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-29 13:05:00
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-29 13:05:00