c - 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 '{'
{
解决方案
该printf
函数接受格式字符串作为其第一个参数。也就是说,为了在输出字符串中包含后续参数,您必须指定它们是什么类型以及您希望它们出现在哪里,例如
printf("#%d", 5); // prints "#5"
printf(" %d", 5); // prints " 5"
您%d
用于整数,其他数据请参阅文档。
推荐阅读
- r - 在图表和构面标题之间添加间隙
- assembly - LC3 程序集输出到控制台的问题
- c++ - Libtorch:如何从 tensorRT fp16 半类型指针创建张量?
- asynchronous - 如何链接 cy.request().then()?
- python - 在熊猫中将国家名称转换为int
- c++ - 为什么 SPOJ 素数生成器代码超过时间限制?
- global-variables - 如何在 Shopify 中创建全局变量?
- angular - Angular:模板解析错误:解析器错误:意外标记 = 在列
- sql - 连接两个表,一个具有父子层次结构,输出提供的行数比表 1 多
- c - 为什么我的二维阵列打印不正确?(使用 fprintf)