【发布时间】:2021-04-03 00:08:03
【问题描述】:
我正在尝试使用 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 重复这些错误,然后再因太多错误而超时。研究 scanf 和格式化,据我所知,我做得正确。我最好的猜测是 scanf 在全局范围内不起作用。是这个问题吗?
【问题讨论】:
-
你需要一个主函数。
-
@Jabberwocky 我很确定这是 MCVE,因此编译器错误。
标签: c compiler-errors scanf