我几乎不知道自己在做什么,我有这段代码试图解决一些简单的数学问题:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int n, sum=0;
printf("ENTER NUMBER:");
scanf("%i",n);
while(n>0)
{
sum+=n;
n--;
}
printf("\n sum is:",sum);
return 0;
}
问题是当我尝试编译它时,出现此错误:
main.cpp:23:26: warning: too many arguments for format [-Wformat-extra-args]
printf("\n sum is:", sum);
答案 0 :(得分:2)
编译器警告您忘记指定格式字符串中sum的字段。您可能想要:
printf("\n sum is: %d",sum);
如上所述,它将不打印总和,并且将不使用总和值。因此,警告。
答案 1 :(得分:0)
更正后的代码(注释中指出的修复程序):
#include<stdio.h>
//#include<conio.h> // You don't use anything from these headers (yet?)
//#include<stdlib.h>
//main()
int main() // main has to be defiend as an int function
{
int n, sum = 0;
printf("ENTER NUMBER:");
// scanf("%i", n);
scanf("%i", &n); // scanf need the ADDRRESS of variables
while (n > 0)
{
sum += n;
n--;
}
// printf("\n sum is:", sum);
printf("\n sum is: %i", sum); // printf needs a format specifier for each variable
return 0;
}