没有printf的C中的"Hello world"?

use*_*465 3 c

是否可以在不使用printf函数的情况下在C中编写"hello world"程序?(同时仍然保持程序相对几行)

Mic*_*ens 6

这应该工作:

int main (void)
{
    puts("Hello, World!");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

你为什么不想用printf?我想不出任何理由不这样做.

  • 实际上,如果你的用例只是一个带有换行符的普通字符串,那么_not_使用`printf`会更有意义.`puts`几乎肯定更有效率,因为它不必担心格式化字符.+1,一旦`main`改为其中一个规范形式:-) (5认同)
  • @paxdiablo我见过GCC优化`printf("字符串文字");````into(puts("string literal");`.我发现它是因为它破坏了我使用动态库注入劫持`printf()`的尝试:O (4认同)

pax*_*blo 5

好吧,如果我们要包括一些愚蠢的例子(是的,我在看着你,科技龙),我会选择:

#include <stdio.h>

void makeItSo (char *str) {
    if (*str == '\0') return;
    makeItSo (str + 1);
    putchar (*str);
}

int main (void) {
    makeItSo ("\ndlrow olleH");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

只是不要对很长的字符串执行此操作,否则您会发现 Stack Overflow 的真正含义:-)