c-扫描给予";期望的参数声明符";错误,尽管有参数声明符



我正在尝试使用scanf从输入创建全局变量。我有这样的代码:

#include <stdio.h>
#include <string.h>
/* innitialize our default global variables */
int height[2] = {0, 0}; /*verticle height will be the first number when splitting, aka the number of rows*/
int barrier = 0;
int fill = 0;
int point[2] = {0, 0}; /*verticle height, aka row number, will be the first number*/
/*set the variables*/
scanf("%d %d", &height[0], &height[1]);
int array[height[0]][height[1]]; /*create array with designated size*/
scanf("%d", &barrier); /*set the barrier*/
scanf("%d", &fill); /*set the fill*/
scanf("%d %d", &point[0], &point[1]); /*set the point*/

但我在制作时会产生这些错误

prog0.c:11:7: error: expected parameter declarator
scanf("%d %d", &height[0], &height[1]);
^
prog0.c:11:7: error: expected ')'
prog0.c:11:6: note: to match this '('
scanf("%d %d", &height[0], &height[1]);
^
prog0.c:11:1: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int]
scanf("%d %d", &height[0], &height[1]);
^
prog0.c:11:1: error: conflicting types for 'scanf'
/usr/include/stdio.h:446:24: note: previous declaration is here
extern int __REDIRECT (scanf, (const char *__restrict __format, ...),
^
prog0.c:12:5: error: variable length array declaration not allowed at file scope
int array[height[0]][height[1]]; /*create array with designated size*/
^     ~~~~~~~~~
prog0.c:13:7: error: expected parameter declarator
scanf("%d", &barrier); /*set the barrier*/
^
prog0.c:13:7: error: expected ')'
prog0.c:13:6: note: to match this '('
scanf("%d", &barrier); /*set the barrier*/
^
prog0.c:13:1: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int]
scanf("%d", &barrier); /*set the barrier*/
^
prog0.c:13:1: error: conflicting types for 'scanf'
/usr/include/stdio.h:446:24: note: previous declaration is here
extern int __REDIRECT (scanf, (const char *__restrict __format, ...),
^
prog0.c:14:7: error: expected parameter declarator
scanf("%d", &fill); /*set the fill*/
^
prog0.c:14:7: error: expected ')'
prog0.c:14:6: note: to match this '('
scanf("%d", &fill); /*set the fill*/
^
prog0.c:14:1: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int]
scanf("%d", &fill); /*set the fill*/
^
prog0.c:14:1: error: conflicting types for 'scanf'
/usr/include/stdio.h:446:24: note: previous declaration is here
extern int __REDIRECT (scanf, (const char *__restrict __format, ...),

并且在由于太多错误而超时之前,它继续针对每个扫描重复这些错误。研究扫描和格式,据我所知,我做得对。我的最佳猜测是scanf在全局范围内不起作用。这就是问题所在吗?

您忘记了main函数。这是在你的初学者C教材的第一章。没有main函数,任何程序代码都无法运行。

你可能想要这个:

#include <stdio.h>
#include <string.h>
/* innitialize our default global variables */
int height[2] = { 0, 0 }; /*verticle height will be the first number when splitting, aka the number of rows*/
int barrier = 0;
int fill = 0;
int point[2] = { 0, 0 }; /*verticle height, aka row number, will be the first number*/

int main()
{
/*set the variables*/
scanf("%d %d", &height[0], &height[1]);
int array[height[0]][height[1]]; /*create array with designated size*/
scanf("%d", &barrier); /*set the barrier*/
scanf("%d", &fill); /*set the fill*/
scanf("%d %d", &point[0], &point[1]); /*set the point*/
}

最新更新