【问题标题】:How to use one print statement to output multiple lines of text如何用一条print语句输出多行文本
【发布时间】:2022-11-18 21:18:55
【问题描述】:

这不是问题我只是想知道我是否可以使用更少的打印语句来解决这个问题。

#include <stdio.h> // for print statments
int main(int argc, char const *argv[]) {

  int thisNumber;
  printf("%s", "Hey you! input your Number here: " );

  scanf("%d", &thisNumber );

  printf( "%s","Your number is: " );
  printf("%d\n", thisNumber );
  return 0;
}

我试过这个:

#include <stdio.h> // for print statments
int main(int argc, char const *argv[]) {
  int thisNumber;
  printf("%s", "Hey you! input your Number here: " );

  scanf("%d", &thisNumber );

  printf( "Your number is: %d", thisNumber );

  return 0;
}

输出是:

> Hey Bekhruz! input your Number here: <my input say:125>
> Your number is: 125%

出于某种原因,我在这段代码的末尾有一个 % 符号。为什么会发生,我该如何解决?谢谢!

【问题讨论】:

  • 这并不能解释 % 符号,但你确实有 printf("%d\n", thisNumber );,它在末尾有换行符 \n - 你没有在压缩版本中包含它
  • 耶!它起作用了,实际上解决了我的 % 符号问题。谢谢你!

标签: c printing


【解决方案1】:

那是你的 shell 提示符。问题不在于%的出现;问题是它与程序的输出在同一行。这通过输出换行符来解决。

printf( "Your number is: %d
", thisNumber );

当您合并这两个语句时,您遗漏了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2018-12-12
    • 2016-12-29
    相关资源
    最近更新 更多