英文:
what is the meaning of putting puts(" "); at the bottom of the following code?
问题
可以有人帮我理解"puts("")的意义吗?
英文:
# include <stdio.h>
int main(void){
int arr [3][3]={{1,2,3},{4,5,6},{7,8,9}};
int i,j;
for(i =0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ", arr[i][j]);
}
puts("");
return 0;
}
Can anyone help me to understand the meaning of "puts("")?
答案1
得分: 4
puts("")
输出一个空行,然后换行。之前的 printf
不会添加换行,所以 puts("")
在每组三个 printf
后结束一行。您可以更直接地使用 putchar('\n')
来实现相同的效果,通常这是一种更清晰的方式。
这也很重要,以确保终端的提示符将显示在单独的一行上,而不是在程序退出后立即跟在输出之后;即使没有循环,如果程序的所有输出都显示在单行上且没有打印换行符,那么在大多数UNIX式Shell设置中会出现一些不美观的效果。
英文:
puts("")
prints nothing, followed by a newline. The prior printf
s don't add a newline, so puts("")
ends each line after each set of three printf
s. You could achieve the same effect rather more directly with putchar('\n')
, and it's generally a clearer way to go about this.
It's also important for ensuring the terminal's prompt will appear on a separate line, not immediately following the output after the program exits; even without the loop, if all the program's output appeared on a single line and no newline was printed, you'd get some ugly effects on most UNIX-y shell setups.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论