C 错误:格式';%d';需要匹配的';int';参数-wformat

C 错误:格式';%d';需要匹配的';int';参数-wformat,c,C,我收到两条错误消息: 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() {

我收到两条错误消息:

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;
#包括
int main(){
int n=5,a[5]={1,3,5,7,9};//声明并初始化长度为5的数组
整数和;
int i;
对于(i=1;i
您以错误的方式使用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);
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));
这是错误的:

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

你不能让
printf(“输入一个整数x:%d”);
不给它一个整数作为参数来打印。
%d
要求后面跟着一个int参数。另外
printf(“数组的和是$d;%d/%d=%f\n”
有四个
%
说明符,但只有三个参数被传递以满足它们。“格式“%f”需要一个匹配的“int”参数”-您确定这是确切的消息吗?“格式%d需要一个匹配的int参数”和“格式%f需要一个双精度参数,但参数4的类型为int”我只看到三个格式说明符…(提示:其中一个
d
s的前缀是
$
,而不是
%
)@user3236142不,这不起作用。在注释中的示例中,您说“例如,数组的和是25;25/2=12.500000”。使用%d您无法打印这样的值,它只需打印“12”。如果你想像示例中那样打印,你应该按照我的答案中的修复。好的,谢谢!我现在没有得到任何编译错误,但也没有得到所需的输出:我得到以下结果:在x:04处输入一个整数//零不应该在那里..我输入4,我得到:数组的和是$d;32770/32770=8192.500000 y=4011=fab help?!@HAL9000:正在查看!感谢您的帮助!:)我尝试了,这是我得到的输出:输入一个整数x:02(0不应该在那里)数组的总和是$d;32770/32770=16385.000000 y=4011=fab@user3236142你说的是sum/sum,但你在计算sum/x