Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the below program output `C89` when compiled in C89 mode and `C99` when compiled in C99 mode?

I've found this C program from the web:

#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.

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

like image 271
Spikatrix Avatar asked Oct 20 '22 01:10

Spikatrix


1 Answers

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

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 */
like image 118
Paul Rubel Avatar answered Oct 26 '22 10:10

Paul Rubel