我几乎不知道我在做什么,我有这段代码,我试图解决一些简单的数学问题:
#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);
编译器警告您忘记在格式字符串中指定 sum 字段。您可能想要:
printf("n sum is: %d",sum);
如上所述,它不会打印总和,也不会使用总和值。因此警告。
更正的代码(注释中指出的修复(:
#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;
}