C语言 C 中 printf 的标准定义是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6400637/
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
What's the standard definition of printf in C?
提问by Pearl89
What is the function definition of the printf()function as defined in the standard C library?
printf()标准C库中定义的函数的函数定义是什么?
I need the definition to solve the following question:
我需要定义来解决以下问题:
Give the output of the following:
int main() { int a = 2; int b = 5; int c = 10; printf("%d ",a,b,c); return 0; }
给出以下输出:
int main() { int a = 2; int b = 5; int c = 10; printf("%d ",a,b,c); return 0; }
回答by Adam Rosenfield
The C language standard declares printfas follows:
C语言标准声明printf如下:
int printf(const char *format, ...);
It returns an integer and takes a first parameter of a pointer to a constant character and an arbitrary number of subsequent parameters of arbitrary type.
它返回一个整数并接受指向常量字符的指针的第一个参数和任意数量的任意类型的后续参数。
If you happen to pass in more parameters than are required by the format string you pass in, then the extra parameters are ignored (though they are still evaluated). From the C89 standard §4.9.6.1:
如果碰巧传入的参数多于传入的格式字符串所需的参数,则忽略额外的参数(尽管它们仍会被评估)。来自 C89 标准 §4.9.6.1:
If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored.
如果格式的参数不足,则行为未定义。如果格式已用完而参数仍然存在,则对多余的参数进行评估(一如既往),否则将被忽略。
回答by alex
You pass an array of chars (or pointer) as the first argument (which includes format placeholders) and additional arguments to be substituted into the string.
您传递一个字符数组(或指针)作为第一个参数(包括格式占位符)和要替换到字符串中的其他参数。
The output for your example would be 2?1to the standard output. %dis the placeholder for a signed decimal integer. The extra space will be taken literally as it is not a valid placeholder. ais passed as the first placeholder argument, and it has been assigned 2. The extra arguments won't be examined (see below).
您的示例中的输出是2?1到标准输出。%d是有符号十进制整数的占位符。额外的空间将按字面意义使用,因为它不是有效的占位符。a作为第一个占位符参数传递,并且它已被赋值2。不会检查额外的参数(见下文)。
printf()is a variadic functionand only knows its number of additional arguments by counting the placeholders in the first argument.
printf()是一个可变参数函数,只能通过计算第一个参数中的占位符来知道其附加参数的数量。
1Markdown does not allow trailing spaces in inline code examples. I had to use an alternate space, but the space you will see will be a normal one (ASCII 0x20).
1 Markdown 不允许在内联代码示例中使用尾随空格。我不得不使用备用空格,但您将看到的空格将是一个普通空格(ASCII 0x20)。
回答by whooops
回答by zad
Writes to the standard output (stdout) a sequence of data formatted as the format argument specifies. After the format parameter, the function expects at least as many additional arguments as specified in format.
将格式化为格式参数指定的数据序列写入标准输出 (stdout)。在格式参数之后,该函数需要至少与格式中指定的一样多的附加参数。
%d = Signed decimal integer
%d = 有符号十进制整数
回答by phoxis
Its
它的
int printf(const char *format, ...);
formatis a pointer to the format string...is the ellipsis operator , with which you can pass variable number of arguments, which depends on how many place holders we have in the format string.Return value is the number of characters that were printed
format是指向格式字符串的指针...是省略号运算符,您可以使用它传递可变数量的参数,这取决于我们在格式字符串中有多少占位符。返回值是打印的字符数
Have a look here about the ellipsis operator: http://bobobobo.wordpress.com/2008/01/28/how-to-use-variable-argument-lists-va_list/
在这里查看省略号运算符:http: //bobobobo.wordpress.com/2008/01/28/how-to-use-variable-argument-lists-va_list/
回答by Empho
printf("%d ",a,b,c);
For every %(something)you need add one referining variable, therefore
对于每一个%(something)你需要添加一个引用变量,因此
printf("%d ",a+b+c); //would work (a+b+c), best case with (int) before that
printf("%d %d %d",a,b,c); //would print all 3 integers.

