首页 > 解决方案 > mario,cs50的“格式字符串未使用数据参数”错误

问题描述

我无法完成 cs50 的马里奥代码(迈出一步)。有人告诉我这里需要添加什么吗?

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    int n;
    do
    {
    n = get_int("Height: ");// height determines how may steps are to be made
    }
    while (n < 1 || n > 8); 
    for (int i = 0; i < n; i++)
    {
      for (int j = 0; j < n; j++)
      {
      printf(" ", n - 1 - i);
      printf("#", i + 1);
      printf("\n");
    }

}

错误说明了这一点,我无法弄清楚如何解决这个问题。

marioblock.c:16:19: error: data argument not used by format string [-Werror,-Wformat-extra-args]
      printf(" ", n - 1 - i);
             ~~~  ^
marioblock.c:17:19: error: data argument not used by format string [-Werror,-Wformat-extra-args]
      printf("#", i + 1);
             ~~~  ^
marioblock.c:21:2: error: expected '}'
}
 ^
marioblock.c:5:1: note: to match this '{'
{

标签: cstringformatcs50

解决方案


printf函数接受格式字符串作为其第一个参数。也就是说,为了在输出字符串中包含后续参数,您必须指定它们是什么类型以及您希望它们出现在哪里,例如

printf("#%d", 5); // prints "#5"
printf(" %d", 5); // prints " 5"

%d用于整数,其他数据请参阅文档


推荐阅读