1565986223
1565986223

Reputation: 6718

Compile time error when declaring variables in C inside control section

I am trying to compile a program (Listing 12.13 - manydice.c from Stephen Prata's C Primer Plus 6th Edition) but am getting a compile error: 'status' undeclared (first use in this function).

I'd be grateful also for clarifications about the c99 standard of "declaring anywhere".

Here is part of the code:

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            */
            }
            ...

The error message is:

||=== 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)) ===|

Options: (Using code::blocks 16.01 mingw 32bit --gcc version shows gcc 4.9.2, Windows 7 Ultimate 32 bit)

 mingw32-gcc.exe -Wall -g -std=c99  -c

Why am I getting this error?

Upvotes: 0

Views: 545

Answers (2)

John Bollinger
John Bollinger

Reputation: 181008

In the first place, you claim that variable declarations are permitted in C "control sections", among which you apparently include the condition of an if statement. You are mistaken. From C99, a declaration is permitted in place of the first expression in the control clause of a for loop, but not in place of the controlling expressions in other flow-control structures, such as if or while statements.

In the second place, "declaration" is not synonymous with "first appearance". A declaration of an identifier associates at least type information with that identifier, and its placement establishes the identifier's scope. If there is no declaration -- as in your case -- then there is no scope in which the identifier may be used. Primordial C had looser rules about that, but inasmuch as you seem to want to rely on C99, those are irrelevant.

The simplest possible declaration for your variable status would be this:

int status;

That would need to appear before the first use of that variable, and its scope extends to the end of the innermost enclosing block, or to the end of the file if it appears outside any block. In your case, however, I would probably just replace

        if((status = scanf("%d", &dice)) != 1)

with

         int status = scanf("%d", &dice);

         if (status != 1)

That both declares status and computes an initial value for it, which you then use. I find that clearer than performing the value computation and test in the same expression.

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134356

Your expectation (or understanding) is wrong.

Quoting chapter §6.8.4.1, C11, the syntax for the if statement is

if ( expression ) statement

and, a variable declaration is not an expresssion statement.

You can, however, have a variable defined in the statement part.

Upvotes: 0

Related Questions