【发布时间】:2017-05-04 19:52:45
【问题描述】:
我正在尝试编译一个程序(清单 12.13 - 来自 Stephen Prata 的 C Primer Plus 6th Edition 的 manydice.c),但出现编译错误:'status' undeclared(首次在此函数中使用)。
我也将感谢有关“在任何地方声明”的 c99 标准的澄清。
下面是部分代码:
int main(void)
{
int dice, roll;
int sides;
srand((unsigned int) time(0)); /* randomize seed */
printf("Enter the number of sides per die, 0 to stop.\n");
while(scanf("%d", &sides) == 1 && sides > 0)
{
printf("How many dice?\n");
if((status = scanf("%d", &dice)) != 1)
{
if(status == EOF)
{
break; /* exit loop */
}
...
错误信息是:
||=== Build: Debug in dice (compiler: GNU GCC Compiler) ===|
~manydice.c||In function 'main':|
~manydice.c|19|error: 'status' undeclared (first use in this function)|
~manydice.c|19|note: each undeclared identifier is reported only once for each function it appears in|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
选项:(使用 code::blocks 16.01 mingw 32bit --gcc 版本显示 gcc 4.9.2,Windows 7 Ultimate 32 位)
mingw32-gcc.exe -Wall -g -std=c99 -c
为什么会出现这个错误?
【问题讨论】:
-
因为你还没有定义你正在使用的变量
status。 -
您的代码中没有任何内容看起来像是在尝试声明
status。你认为它是在哪里声明的? -
@Wumpus Q. Wumbley 就像我说的正在学习,正在从上述书中学习这个示例(我输入的内容与书中完全相同),它不会编译,我不知道出了什么问题所以我提出了这个问题。如果您向我解释错误而不是指出更多我不知道的错误,那会更有帮助
-
for(i = 0; i < 5; i++)不应该工作。for(int i = 0; i < 5; i++)是正确的选择。 -
作为参考,the book's published code 有点不同。