【问题标题】:Scanf giving "expected parameter declarator" error despite having parameter declarator尽管有参数声明符,但 Scanf 给出“预期的参数声明符”错误
【发布时间】: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


【解决方案1】:

您忘记了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*/
}

【讨论】:

  • 我没有忘记。它是后来的,并且做了很多其他的事情。我试图将所有这些设置为全局变量,包括数组,因为我将在多个函数中编辑数组并且想要在不必传递指针的情况下进行操作。我认为这意味着;是的,scanf 不能在全局范围内工作?
  • @JosephSquires 语句一般不能在函数之外。
  • @您需要在main 或您在main 开头调用的某些初始化函数中包含所有初始化代码。
猜你喜欢
  • 2017-01-26
  • 2021-09-13
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多