Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C puzzle: how will you print something on console?

Tags:

c

puzzle

/*you cannot change anything from here below*/
main()
{
exit(0);
}
/*you cannot change anything from here up*/

This was asked during an interview.

I was told to print something on console.

anybody?

like image 675
bakra Avatar asked Aug 20 '10 11:08

bakra


2 Answers

Really surprised that nobody posted this yet:

#include <stdio.h>

#if 0

/*you cannot change anything from here below*/
main()
{
exit(0);
}
/*you cannot change anything from here up*/

#endif

int main()
{
   printf("Hello, World!");
   return 0;
}

Prints at runtime and no undefined behavior whatsoever.

like image 84
Tyler McHenry Avatar answered Nov 15 '22 00:11

Tyler McHenry


weird question...

int main(void)
{
    printf("hello");
    return 0;
}
#define main int lol
/*you cannot change anything from here below*/
main()
{
exit(0);
}
/*you cannot change anything from here up*/
like image 19
tenfour Avatar answered Nov 15 '22 00:11

tenfour