C语言 C 中的预期表达式错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22764426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected expression error in C
提问by user3481464
I'm trying to create two functions. The first function accepts integer inputs from the user until they are between 0 and 100. The seconds function display the validation to the stdOut. However, it keeps giving me an error saying "Expected expression" when I call the function.
我正在尝试创建两个函数。第一个函数接受来自用户的整数输入,直到它们介于 0 和 100 之间。 seconds 函数向 stdOut 显示验证。但是,当我调用该函数时,它一直给我一个错误提示“预期的表达式”。
#include <stdio.h>
// function prototype for the function input
int input(int);
// function prototype for the function validate
int validate(int);
//main function
int main(void)
{
//calling the function input
input(int x)
//calling the function validate
validate(int y)
return 0;
}
// Function definition for input
int input(int a)
{
int r;
printf("Enter the int value of r\n");
scanf("%d",&r);
}
// Function definition for validate
int validate(int b)
{
int r;
if(r>= 0 && r<= 100)
printf("Valid number");
else
printf("Invalid");
}
回答by zwol
There is at least one bug on almost every line of this program.
这个程序的几乎每一行都至少有一个错误。
This is a standard problem for which there is a whole lot of incorrect advice out there (most importantly, only the strtol/strtoul/strtodfamily of functions should be used to convert strings to numbers; never use the atoifamily and never use scanf) so I am going to give a complete worked example of how to write this program correctly, including proper use of comments.
这对于有一大堆不正确的意见出来有没有一个标准问题(最重要的是,只有strtol/ strtoul/strtod家庭的功能应该被用来将字符串转换为数字;从不使用atoi家庭和从未使用scanf),所以我要给出一个完整的工作示例,说明如何正确编写此程序,包括正确使用注释。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
long read_number_in_range(const char *prompt, long lo, long hi)
{
// a signed 64-bit number fits in 21 characters, +1 for '\n', +1 for NUL
char buf[23], *endp;
long rv;
for (;;) {
puts(prompt);
if (!fgets(buf, sizeof buf, stdin)) {
perror("stdin");
exit(1);
}
errno = 0;
rv = strtol(buf, &endp, 10);
if (endp != buf && (*endp == '#include <stdio.h>
' || *endp == '\n')
&& !errno && rv >= lo && rv <= hi) {
return rv;
}
// if we get here, fgets might not have read the whole line;
// drain any remainder
if (!strchr(buf, '\n')) {
int c;
do c = getchar();
while (c != EOF && c != '\n');
}
puts("?Redo from start");
}
}
int main(void)
{
long val = read_number_in_range("Enter the int value of r", 0, 100);
// do something with val here
return 0;
}
Read on for line-by-line nitpicking of the original program.
继续阅读对原始程序的逐行挑剔。
// function prototype for the function input
Correct.
正确的。
int input(int);
Comment redundant with code.
注释与代码冗余。
// function prototype for the function validate
Function signature incorrect (see comments on body of function).
函数签名不正确(见函数体注释)。
int validate(int);
Comment redundant with code.
注释与代码冗余。
//main function
Function signature incorrect (see comments on body of function).
函数签名不正确(见函数体注释)。
int main(void)
{
Comment redundant with code.
注释与代码冗余。
//calling the function input
Correct.
正确的。
input(int x)
Comment redundant with code.
注释与代码冗余。
//calling the function validate
- Variables cannot be declared inside function call expressions.
- Return value of function is ignored.
- Missing semicolon at end of line.
- 不能在函数调用表达式中声明变量。
- 函数的返回值被忽略。
- 行尾缺少分号。
validate(int y)
Comment redundant with code.
注释与代码冗余。
return 0;
}
- Value returned from
inputshould be passed tovalidate, presumably, instead of a new uninitialized variable. - Variables cannot be declared inside function call expressions.
- Return value of function is ignored.
- Missing semicolon at end of line.
- 返回的值
input应该传递给validate,大概是,而不是一个新的未初始化的变量。 - 不能在函数调用表达式中声明变量。
- 函数的返回值被忽略。
- 行尾缺少分号。
// Function definition for input
Correct.
正确的。
int input(int a)
{
Comment redundant with code.
注释与代码冗余。
int r;
Parameter ais unnecessary.
参数a是不必要的。
printf("Enter the int value of r\n");
Correct.
正确的。
scanf("%d",&r);
Minor: use putswhen there is nothing to format.
次要:puts在没有要格式化的内容时使用。
}
// Function definition for validate
Missing return r;.
失踪return r;。
int validate(int b)
{
Comment redundant with code.
注释与代码冗余。
int r;
Function has no return value, so should be void validate(int b).
函数没有返回值,所以应该是void validate(int b)。
if(r>= 0 && r<= 100)
Unnecessary variable.
不必要的变量。
printf("Valid number");
else
printf("Invalid");
rshould be bon this line.
r应该b在这条线上。
}
Minor: again, puts.
次要:再次,puts。
x = input();
validate(x);
Correct.
正确的。
回答by unwind
You have some stray ints in your calls, they need to go.
你的电话中有一些流浪者int,他们需要离开。
The calls should probably be:
电话应该是:
int main(void)
{
int x=0;
int y=0;
//calling the function input
input(x);
//calling the function validate
validate(y);
return 0;
}
You can't pass an integer toa function and expect it to change in the caller's context, that is not how C's pass-by-value semantics work. You should just return the number from input()instead, i.e. its prototype should be int input(void);.
您不能将整数传递给函数并期望它在调用者的上下文中发生变化,这不是 C 的按值传递语义的工作方式。你应该只返回数字 frominput()相反,即它的原型应该是int input(void);.
回答by Caleb
You need to add semicolons at the end of each line of code in your main()function, and also remove the type specifier in the function calls. Also, don't forget to declare the variables xand ysomewhere:
您需要在main()函数中每行代码的末尾添加分号,并删除函数调用中的类型说明符。另外,不要忘记声明变量x和y某处:
//calling the function input
input(int x)
//calling the function validate
validate(int y)
回答by gnasher729
I bet the compiler asks you for an expression in some place where you write a function declaration with a missing semicolon.
我敢打赌,编译器会在您编写缺少分号的函数声明的某个地方要求您提供表达式。
#include <stdio.h>
int input(int*);
int validate(int);
int main(void){
int x;
input(&x);
if(validate(x))
printf("Valid number");
else
printf("Invalid");
return 0;
}
int input(int *r){
printf("Enter the int value of r\n");
scanf("%d", r);
return *r;
}
int validate(int r){
return r>= 0 && r<= 100;
}
Neither of these is a function call.
这些都不是函数调用。

