为什么要打印安装台的空间?

发布于 2025-02-12 08:40:45 字数 468 浏览 0 评论 0原文

这是echo实用程序程序。它从控制台读取命令行参数,并将其打印回控制台。在以下代码块中,在第10行中,为什么在没有参数字符串结束时将空间写入控制台?

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

int main(int argc, char *argv[])
{
  int i;
  for(i = 1; i < argc; i++){
    write(1, argv[i], strlen(argv[i]));
    if(i + 1 < argc){
      write(1, " ", 1); //line 10
    } else {
      write(1, "\n", 1);
    }
  }
  exit(0);
}

This is the echo utility program. It reads the command-line argument from the console and prints it back to the console. In the following block of code, at line 10, why write a space to the console when there is not the end of argument string?

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

int main(int argc, char *argv[])
{
  int i;
  for(i = 1; i < argc; i++){
    write(1, argv[i], strlen(argv[i]));
    if(i + 1 < argc){
      write(1, " ", 1); //line 10
    } else {
      write(1, "\n", 1);
    }
  }
  exit(0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2025-02-19 08:40:45
if(i + 1 < argc){
  write(1, " ", 1); //line 10
} else {
   write(1, "\n", 1);
}

这仅在最后一个参数之后才打印出新线,否则它将打印一个空间以分开相邻参数。换句话说,输出是单线中的参数,被空间隔开,以纽线结束线路。

if(i + 1 < argc){
  write(1, " ", 1); //line 10
} else {
   write(1, "\n", 1);
}

This simply prints newline after the last argument only, otherwise it prints a space to separate adjacent arguments. In other words, output is arguments in the single line, separated by spaces, ending the line with newline.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文