what is the meaning of putting puts(" "); at the bottom of the following code?

huangapple go评论58阅读模式
英文:

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 printfs don't add a newline, so puts("") ends each line after each set of three printfs. 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.

huangapple
  • 本文由 发表于 2023年5月17日 19:09:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定