C拼图:你将如何在控制台上打印一些东西?

bak*_*kra 10 c puzzle

/*you cannot change anything from here below*/
main()
{
exit(0);
}
/*you cannot change anything from here up*/
Run Code Online (Sandbox Code Playgroud)

这是在一次采访中被问到的.

我被告知要在控制台上打印一些东西.

任何人?

Tyl*_*nry 20

真的很惊讶没人发布这个:

#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;
}
Run Code Online (Sandbox Code Playgroud)

在运行时打印,没有任何未定义的行为.


ten*_*our 19

奇怪的问题......

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*/
Run Code Online (Sandbox Code Playgroud)

  • 也许成功的申请人将被要求在生产中使用这种技术 (5认同)
  • 这不是相关的,但我不知道这如何证明了申请人的能力. (3认同)

use*_*313 5

#include <stdio.h>
#define exit(c) return puts("foobar"),0
Run Code Online (Sandbox Code Playgroud)

超过主要


dir*_*tly 3

一种实现定义的方法是pragma在编译期间使用指令进行打印。

#pragma message "Compiling " __FILE__ "..."
Run Code Online (Sandbox Code Playgroud)

或者,您可以在运行时使用一些宏和 printf 来完成此操作(但不能在某些方面引入 UB)。

#define exit(x) printf("Hello, world!")
int main() {
 exit(0); 
 return 0; /* if pre-C99 */
}
Run Code Online (Sandbox Code Playgroud)