我正在尝试创建两个函数。第一个函数接受来自用户的整数输入,直到它们介于 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");
}
Run Code Online (Sandbox Code Playgroud)
这个程序的几乎每一行都至少有一个错误。
这对于有一大堆不正确的意见出来有没有一个标准问题(最重要的是,只有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 == '\0' || *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;
}
Run Code Online (Sandbox Code Playgroud)
继续阅读对原始程序的逐行挑剔。
#include <stdio.h>
Run Code Online (Sandbox Code Playgroud)
正确的。
// function prototype for the function input
Run Code Online (Sandbox Code Playgroud)
注释与代码冗余。
int input(int);
Run Code Online (Sandbox Code Playgroud)
函数签名不正确(见函数体注释)。
// function prototype for the function validate
Run Code Online (Sandbox Code Playgroud)
注释与代码冗余。
int validate(int);
Run Code Online (Sandbox Code Playgroud)
函数签名不正确(见函数体注释)。
//main function
Run Code Online (Sandbox Code Playgroud)
注释与代码冗余。
int main(void)
{
Run Code Online (Sandbox Code Playgroud)
正确的。
//calling the function input
Run Code Online (Sandbox Code Playgroud)
注释与代码冗余。
input(int x)
Run Code Online (Sandbox Code Playgroud)
//calling the function validate
Run Code Online (Sandbox Code Playgroud)
注释与代码冗余。
validate(int y)
Run Code Online (Sandbox Code Playgroud)
input应该传递给validate,大概是,而不是一个新的未初始化的变量。 return 0;
}
Run Code Online (Sandbox Code Playgroud)
正确的。
// Function definition for input
Run Code Online (Sandbox Code Playgroud)
注释与代码冗余。
int input(int a)
{
Run Code Online (Sandbox Code Playgroud)
参数a是不必要的。
int r;
Run Code Online (Sandbox Code Playgroud)
正确的。
printf("Enter the int value of r\n");
Run Code Online (Sandbox Code Playgroud)
次要:puts在没有要格式化的内容时使用。
scanf("%d",&r);
Run Code Online (Sandbox Code Playgroud)
}
Run Code Online (Sandbox Code Playgroud)
失踪return r;。
// Function definition for validate
Run Code Online (Sandbox Code Playgroud)
注释与代码冗余。
int validate(int b)
{
Run Code Online (Sandbox Code Playgroud)
函数没有返回值,所以应该是void validate(int b)。
int r;
Run Code Online (Sandbox Code Playgroud)
不必要的变量。
if(r>= 0 && r<= 100)
Run Code Online (Sandbox Code Playgroud)
r应该b在这条线上。
printf("Valid number");
else
printf("Invalid");
Run Code Online (Sandbox Code Playgroud)
次要:再次,puts。
}
Run Code Online (Sandbox Code Playgroud)
正确的。