c - 如何使用此“printf”函数减少参数数量?我应该使用“printf”以外的东西吗?
问题描述
我遇到了一个我一直在尝试解决的问题。我了解 printf 和 puts 之间的区别,所以我认为 printf 是正确的方法。
但是,我收到一条错误消息:
“警告:格式参数过多 [-Wformat-extra-args]
printf("%s", "The sum of %d", first , " and %d", second, " is %d", first + second);
^"
是否有更好的功能来打印我需要的输出,或者可能有不同的方式来格式化这个输出?
#include <stdio.h>
void main()
{
int first, second;
int answer = 1;
while (answer == 1)
{
puts("Please enter the first integer ==> ");
scanf("%d", &first);
puts("Please enter the second integer ==> ");
scanf("%d", &second);
printf("%s", "The sum of %d", first , " and %d", second, " is %d", first + second);
puts("Would you like to add two more integers?\n"
"(1 for yes) ==> ");
scanf("%d", &answer);
}
}
解决方案
您的格式字符串必须是的第一个参数,printf
并且您的输入必须是以下的。例如:
printf("The sum of %d and %d is %d", first, second, first + second);
推荐阅读
- c - C - 哈希表清除建议
- c# - SendKeys.SendWait 花费太多时间 C#
- python - Tarfile 项目的 Tkinter 树视图
- c - 商业软件中的内核模块问题
- git - 如何优化 2000GB Git 数据
- java - Firebase 数据库中的 setValue() 方法不起作用。值未添加到数据库中
- ssl - Kafka,通过 OpenSSL 的自签名证书验证主机名
- mysql - 如何按日期而不是时间分组
- stm32 - 我无法通过 STM32F103 上的 mpu6050 使用 I2C 获取数据
- react-native - React Native Expo 水平和垂直滚动 - Android 和 iOS