programing

아래 프로그램은 C89 모드에서 컴파일할 때 C89, C99 모드에서 컴파일할 때 C99를 어떻게 출력합니까?

prostudy 2022. 7. 27. 21:45
반응형

아래 프로그램은 C89 모드에서 컴파일할 때 C89, C99 모드에서 컴파일할 때 C99를 어떻게 출력합니까?

웹에서 다음 C 프로그램을 찾았습니다.

#include <stdio.h>

int main(){

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

    return 0;
}

이 프로그램의 흥미로운 점은 C89 모드에서 컴파일되어 실행되면C89컴파일되어 C99 모드로 실행되면,C99하지만 이 프로그램이 어떻게 작동하는지 알 수 없습니다.

의 두 번째 주장이 어떻게printf위의 프로그램에서 작동합니까?

C99에서는//-style comments, C89는 그렇지 않습니다.번역 방법:

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 */

대사 코멘트//는 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 */

C99의 이것과 동등합니다.

#include <stdio.h>

int main(){

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

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

왜냐면//코멘트는 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;
}

올바른 코드는 다음과 같습니다.

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

언급URL : https://stackoverflow.com/questions/31115453/how-does-the-below-program-output-c89-when-compiled-in-c89-mode-and-c99-when

반응형