C语言 C中printf函数的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4867229/
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
Code for printf function in C
提问by Rainer Zufall
Possible Duplicate:
source code of c/c++ functions
可能的重复:
c/c++ 函数的源代码
I was wondering where I can find the C code that's used so that when I write printf("Hello World!"); in my C programm to know that it has to print that string to STDOUT. I looked in <stdio.h>, but there I could only find its prototype int printf(const char *format, ...), but not how it looks like internally.
我想知道在哪里可以找到使用的 C 代码,以便在我编写 printf("Hello World!"); 时使用。在我的 C 程序中知道它必须将该字符串打印到 STDOUT。我查看了 <stdio.h>,但在那里我只能找到它的原型 int printf(const char *format, ...),而不是它内部的样子。
回答by mschaef
Here's the GNU version of printf... you can see it passing in stdoutto vfprintf:
下面是GNU版本printf...你可以看到它传递stdout到vfprintf:
__printf (const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = vfprintf (stdout, format, arg);
va_end (arg);
return done;
}
Here's a linkto vfprintf... all the formatting 'magic' happens here.
这里有一个链接到vfprintf...所有的格式“神奇”发生在这里。
The only thing that's truly 'different' about these functions is that they use varargs to get at arguments in a variable length argument list. Other than that, they're just traditional C. (This is in contrast to Pascal's printfequivalent, which is implemented with specific support in the compiler... at least it was back in the day.)
这些函数唯一真正“不同”的是它们使用可变参数来获取可变长度参数列表中的参数。除此之外,它们只是传统的 C。(这与 Pascal 的printf等价物形成对比,后者是在编译器中通过特定支持来实现的......至少在当时是这样。)

