error C2065 and IntelliSense



im在c++上编程,我对该代码有以下错误:

#include <stdio.h>
int main(int argc, char* argv[])
{ int x;
    printf("%d","Please enter a numbern");
    scanf(%d,&x);
    printf("%d","You entered 56n");
}

以下是错误:错误1错误C2065:"d":未声明的标识符9

2   IntelliSense: expected an expression    9   8   

谢谢,Peleg

scanf的第一个参数应该是以null结尾的字符串:

scanf("%d",&x);

正如您对printf所做的那样。

scanf(%d,&x);  
------^^---- 

应为scanf("%d",&x);

正如其他人所说,scanf需要一个字符串格式,因此您必须编写

scanf("%d", &x);

此外,您对printf的使用不会产生您想要的结果。传递给printf的第一个字符串是输出格式字符串。"%d"表示下一个参数是一个整数。您的下一个参数是字符串的地址。你真正想写的是这三行中的一行:

printf("%s", "Please enter a numbern");
printf("Please enter a numbern");
puts("Please enter a number");

最后一行对你来说是最好的。第二行也很好,但这只是因为字符串不包含像%d这样的格式化字符。

问题出现在scanf(%d,&x);语句的第一个参数中。此参数应为以null结尾的字符串。您的代码被重写如下:

#include <stdio.h>
int main(int argc, char* argv[])
{ 
    int x;
    printf("Please enter a number %dn");
    scanf("%d",&x);
    printf("You entered %dn", x);
}

最新更新