我收到两条错误消息:
Error: format '%d' expects a matching 'int' argument -wformat
Error: format '%f' expects an arguemnt of double but argument 4 has type int -wformat
我抬起头来试图解决它,但没有用。以下是我的代码 有人能告诉我我做错了吗?
#include <stdio.h>
int main() {
int n = 5, a[5] = {1, 3, 5, 7, 9}; // Declare & initialize array of length 5
int sum;
int i;
for (i = 1; i < n; i++) {
sum = sum + a[i];
printf("Enter an integer x: %d"); // Prompt the user
int x;
scanf("%d", &x); // Read in the integer
// Print out the sum, the division we're performing, and the result (without truncation)
// E.g., The sum of the array is 25; 25/2 = 12.500000
printf("The sum of the array is $d; %d/%d = %f\n", sum, sum, sum / x);
// Declare an integer variable y and initialize it using a hexadecimal constant.
// Print y in decimal, hex, and with leading zeros so that we get the output
// y = 4011 = fab = 0xfab = fab = 0000fab
int y = 0xfab;
printf("y = %d = %x\n", y, y, y, y, y);
return 0;
答案 0 :(得分:2)
您正在以错误的方式使用printf:
printf("Enter an integer x: %d");
您必须指定要在%d出现时打印的整数值,如下所示:
printf("Enter an integer x: %d",someValue);
这也错了:
printf("The sum of the array is $d; %d/%d = %f\n", sum, sum, sum / x);
您正在使用%f打印整数。您应该执行以下操作:
printf("The sum of the array is $d; %d/%d = %f\n", sum, sum, ((double)sum / (double)x));
答案 1 :(得分:0)
这是错误的:
printf("The sum of the array is $d; %d/%d = %f\n", sum, sum, sum / x);
我相信你想要这个:
printf("The sum of the array is $d; %d/%d = %d\n", sum, sum, sum/x);
请注意,上一个%f
变为%d
,因为sum/x
将产生一个整数。
这也是错误的:
printf("Enter an integer x: %d");
删除%d
,你不能传递格式说明符,然后错过相应的参数。