C语言 如何在 C 中打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2162758/
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
How to print in C
提问by sbsp
Very much a beginner to c, in fact this is my first tester program.
非常是 c 的初学者,实际上这是我的第一个测试程序。
I can't actually figure out how to print this number out to the terminal.
我实际上无法弄清楚如何将这个数字打印到终端。
#include <stdio.h>
int addNumbers(int a, int b)
{
int sum = a + b;
return sum;
}
int main(void)
{
int a = 4;
int b = 7;
printf(addNumbers(a,b));
return 0;
}
I am sure that in java I could just replace the printf with system.out and it would have worked. I tried searching the answer earlier but if you don't know what to search it's hard to find an answer.
我确信在 java 中我可以用 system.out 替换 printf 并且它会起作用。我之前尝试过搜索答案,但如果您不知道要搜索什么,则很难找到答案。
回答by nickf
回答by John Bode
The first argument to printf()is always a string value, known as a format control string. This string may be regular text, such as
的第一个参数printf()始终是一个字符串值,称为格式控制字符串。该字符串可能是常规文本,例如
printf("Hello, World\n"); // \n indicates a newline character
or
或者
char greeting[] = "Hello, World\n";
printf(greeting);
This string may also contain one or more conversion specifiers; these conversion specifiers indicate that additional arguments have been passed to printf(), and they specify how to format those arguments for output. For example, I can change the above to
这个字符串也可能包含一个或多个转换说明符;这些转换说明符指示附加参数已传递给printf(),并且它们指定如何格式化这些参数以供输出。例如,我可以将上面的更改为
char greeting[] = "Hello, World";
printf("%s\n", greeting);
The "%s" conversion specifier expects a pointer to a 0-terminated string, and formats it as text.
“%s”转换说明符需要一个指向以 0 结尾的字符串的指针,并将其格式化为文本。
For signed decimal integer output, use either the "%d" or "%i" conversion specifiers, such as
对于有符号十进制整数输出,请使用“%d”或“%i”转换说明符,例如
printf("%d\n", addNumber(a,b));
You can mix regular text with conversion specifiers, like so:
您可以将常规文本与转换说明符混合使用,如下所示:
printf("The result of addNumber(%d, %d) is %d\n", a, b, addNumber(a,b));
Note that the conversion specifiers in the control string indicate the numberand typesof additional parameters. If the number or types of additional arguments passed to printf()don't match the conversion specifiers in the format string then the behavior is undefined. For example:
请注意,控制字符串中的转换说明符指示附加参数的数量和类型。如果传递给的附加参数的数量或类型printf()与格式字符串中的转换说明符不匹配,则行为未定义。例如:
printf("The result of addNumber(%d, %d) is %d\n", addNumber(a,b));
will result in anything from garbled output to an outright crash.
将导致从乱码输出到彻底崩溃的任何事情。
There are a number of additional flags for conversion specifiers that control field width, precision, padding, justification, and types. Check your handy C reference manual for a complete listing.
有许多用于控制字段宽度、精度、填充、对齐和类型的转换说明符的附加标志。查看您方便的 C 参考手册以获取完整列表。
回答by T.E.D.
printf is a fair bit more complicated than that. You have to supply a format string, and then the variables to apply to the format string. If you just supply one variable, C will assume that is the format string and try to print out all the bytes it finds in it until it hits a terminating nul (0x0).
printf 比那要复杂一些。您必须提供格式字符串,然后是要应用于格式字符串的变量。如果您只提供一个变量,C 将假定它是格式字符串并尝试打印出它在其中找到的所有字节,直到它遇到终止 nul (0x0)。
So if you just give it an integer, it will merrily march through memory at the location your integer is stored, dumping whatever garbage is there to the screen, until it happens to come across a byte containing 0.
所以如果你给它一个整数,它会在你的整数存储位置愉快地通过内存,将那里的任何垃圾倾倒到屏幕上,直到它碰巧遇到一个包含 0 的字节。
For a Java programmer, I'd imagine this is a rather rude introduction to C's lack of type checking. Believe me, this is only the tip of the iceberg. This is why, while I applaud your desire to expand your horizons by learning C, I highly suggest you do whatever you can to avoid writing real programs in it.
对于 Java 程序员,我认为这是对 C 缺乏类型检查的相当粗鲁的介绍。相信我,这只是冰山一角。这就是为什么,虽然我赞赏你希望通过学习 C 来扩展你的视野,但我强烈建议你尽你所能避免用它编写真正的程序。
(This goes for everyone else reading this too.)
(这也适用于其他阅读本文的人。)

