C语言 错误:格式“%d”需要匹配的“int”参数-wformat

8gsdolmq  于 2023-02-18  发布在  其他
关注(0)|答案(3)|浏览(876)

我收到两条错误消息:

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;
fumotvh3

fumotvh31#

您使用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));
xriantvc

xriantvc2#

这是错误的:

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,您不能传递格式说明符,然后错过相应的参数。

fruv7luv

fruv7luv3#

这是不正确的:
printf("Enter an integer x: %d");
你还必须使用逗号和参数将参数包含在结束括号内但在双引号外的末尾。因为它是存储数据的地方(变量)!
这是正确的方法:
printf("Enter an integer x: %d", argument/variable);

相关问题