下面的程序在C89模式下编译时如何输出' C89 ',在C99模式下编译时如何输出' C99 ' ?

时间:2023-01-02 22:53:37

I've found this C program from the web:

我在网上找到了C程序:

#include <stdio.h>

int main(){

    printf("C%d\n",(int)(90-(-4.5//**/
    -4.5)));

    return 0;
}

The interesting thing with this program is that when it is compiled and run in C89 mode, it prints C89 and when it is compiled and run in C99 mode, it prints C99. But I am not able to figure out how this program works.

这个程序的有趣之处在于,当它在C89模式下编译和运行时,它会打印C89,当它在C99模式下编译和运行时,它会打印C99。但是我不知道这个程序是如何工作的。

Can you explain how the second argument of printf works in the above program?

你能解释一下printf的第二个参数是如何在上面的程序中工作的吗?

3 个解决方案

#1


132  

C99 allows //-style comments, C89 does not. So, to translate:

C99允许//样式的注释,C89不允许。因此,翻译:

C99:

C99:

 printf("C%d\n",(int)(90-(-4.5     /*Some  comment stuff*/
                         -4.5)));
// Outputs: 99

C89:

C89:

printf("C%d\n",(int)(90-(-4.5/      
                         -4.5)));
/* so  we get 90-1 or 89 */

#2


25  

the line comment // is introduced since C99. Therefore your code is equal to this in C89

行注释//从C99开始引入。因此你的代码在C89中等于这个

#include <stdio.h>

int main(){

    printf("C%d\n",(int)(90-(-4.5/
-4.5)));

    return 0;
}
/* 90 - (-4.5 / -4.5) = 89 */

and equal to this in C99

等于C99

#include <stdio.h>

int main(){

    printf("C%d\n",(int)(90-(-4.5
-4.5)));

    return 0;
}
/* 90 - (-4.5 - 4.5) = 99*/

#3


9  

Because // comments only exist in C99 and later standards, the code is equivalent to the following:

因为//注释只存在于C99和以后的标准中,代码相当于以下内容:

#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;
}

Correct code would be:

正确的代码是:

#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;
}

#1


132  

C99 allows //-style comments, C89 does not. So, to translate:

C99允许//样式的注释,C89不允许。因此,翻译:

C99:

C99:

 printf("C%d\n",(int)(90-(-4.5     /*Some  comment stuff*/
                         -4.5)));
// Outputs: 99

C89:

C89:

printf("C%d\n",(int)(90-(-4.5/      
                         -4.5)));
/* so  we get 90-1 or 89 */

#2


25  

the line comment // is introduced since C99. Therefore your code is equal to this in C89

行注释//从C99开始引入。因此你的代码在C89中等于这个

#include <stdio.h>

int main(){

    printf("C%d\n",(int)(90-(-4.5/
-4.5)));

    return 0;
}
/* 90 - (-4.5 / -4.5) = 89 */

and equal to this in C99

等于C99

#include <stdio.h>

int main(){

    printf("C%d\n",(int)(90-(-4.5
-4.5)));

    return 0;
}
/* 90 - (-4.5 - 4.5) = 99*/

#3


9  

Because // comments only exist in C99 and later standards, the code is equivalent to the following:

因为//注释只存在于C99和以后的标准中,代码相当于以下内容:

#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;
}

Correct code would be:

正确的代码是:

#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;
}