【问题标题】:How can I display the line number for a printf statement in c?如何在 c 中显示 printf 语句的行号?
【发布时间】:2016-06-24 12:55:35
【问题描述】:
int main() {
int i, repeatName; // ints
char firstName[50]; // array to store users name

// get first name from user
printf("Please enter your first name: ");
scanf("%s", firstName);

// get amount of times user would like to repeat name
printf("How many times would you like to repeat your name?: ");
scanf("%i", &repeatName);

// tell user name has to be repeated at last one
if (repeatName < 1) {
    printf("The name has to be repeated at least one (1) time. Try again: ");
    scanf("%i", &repeatName);
}

// for loop to repeat name 'x' number of times
for (i = 0; i < repeatName; i++) {
    printf("%s \n", firstName);
}
}

例如:如果用户想显示他们的名字 3 次,它会说:

Your name 

Your name

Your name 

我怎么能说出来:

Line 1 Your name

Line 2 Your name

Line 3 Your name 

【问题讨论】:

  • 行号后面的标准是什么?只是迭代次数吗?

标签: c printf scanf


【解决方案1】:

使用循环中的i变量作为行号

for (i = 0; i < repeatName; ++i)
    printf("Line %d %s\n", i + 1, firstName);

一定要加 1,因为循环索引从 0 开始。您希望第一行说“Line 1”,而不是“Line 0”,依此类推。

编辑:当行号超过一位时,输出不那么漂亮。为了解决这个问题,你可以写

for (i = 0; i < repeatName; ++i)
    printf("Line %-6d%s\n", i + 1, firstName);

这使得行号占用至少 6 个字符,并使行号左对齐:

Line 1     this is my string
Line 2     this is my string
Line 3     this is my string
Line 4     this is my string
Line 5     this is my string
Line 6     this is my string
Line 7     this is my string
Line 8     this is my string
Line 9     this is my string
Line 10    this is my string

【讨论】:

  • 谢谢!这正是我想要做的。也欣赏编辑。
【解决方案2】:

据我所知,没有办法自动执行此操作。但是你也可以让 for 循环中的 i 变量充当行计数器,因为每次迭代都会打印一行:

// for loop to repeat name 'x' number of times
for (i = 0; i < repeatName; i++) {
    printf("Line %d %s \n", i + 1 /* Lines are not 0 based */ , firstName);
}

【讨论】:

    【解决方案3】:

    打印时只需添加循环索引:

    for (i = 0; i < repeatName; i++) {
        printf("Line %d %s \n", i+1, firstName);
    }
    

    【讨论】:

      【解决方案4】:

      试试这个代码

       #include <stdio.h>
          int main() 
          {
          int i, repeatName; // ints
          char firstName[50]; // array to store users name
      
          // get first name from user
          printf("Please enter your first name: ");
          scanf("%s", firstName);
      
          // get amount of times user would like to repeat name
          printf("How many times would you like to repeat your name?: ");
          scanf("%i", &repeatName);
      
          // tell user name has to be repeated at last one
          if (repeatName < 1) {
              printf("The name has to be repeated at least one (1) time. Try again: ");
              scanf("%i", &repeatName);
          }
      
          // for loop to repeat name 'x' number of times
          for (i = 1; i <= repeatName; i++) {
              printf("Line %d Your name %s \n",i,firstName);
          }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-21
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 2012-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多