C语言 没有printf的C中的“Hello world”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14794823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:18:39  来源:igfitidea点击:

"Hello world" in C without printf?

c

提问by user1869465

Is it possible to write a "hello world" program in C without the use of the printf function? (while still keeping the program relatively at a few lines)

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

回答by Michael Dickens

This should work:

这应该有效:

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

Why don't you want to use printf? I can't think of any reason not to.

你为什么不想使用 printf 呢?我想不出任何不这样做的理由。

回答by PQuinn

write(stdout, "hello world", strlen("hello world")); 

回答by technosaurus

This is a ridiculous alternative to just using puts("hello world\n");

这是仅使用的荒谬替代方案 puts("hello world\n");

#include <stdio.h>
int main(void){
  char *s="hello world\n";
  while (*s) putchar(*s++);
}

回答by paxdiablo

Well, if we're going to include silly examples (yes, I'm looking at you, technosauraus), I'd go with:

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

#include <stdio.h>

void makeItSo (char *str) {
    if (*str == '##代码##') return;
    makeItSo (str + 1);
    putchar (*str);
}

int main (void) {
    makeItSo ("\ndlrow olleH");
    return 0;
}

Just don't do this for really long strings or you find out what Stack Overflow reallymeans :-)

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