Search code examples
csyntax-errorpelles-c

Undeclared identifier 'number', how do I get around this?


I am trying to show the a number that I have specified in a program, but recieve the following error:

main.c(23): error #2048: Undeclared identifier 'number'.

#include <stdio.h>



int main()

{

 {

int number = 32 ;

}

printf("integer is %d \n", number );

    return 0;
}

I'm aware the solution to this must be very simple to some users, however i'm following instructions from a book and as far as I know i'm following to the letter.

Please any help would be greatly appreciated.


Solution

  • Your problem is scoping:

    int main()
    {
       {       
          int number = 32 ;     <== Number enters scope here
       }                        <== Number leaves scope here
    
       printf("integer is %d \n", number );  <== Number is out of scope scope here
    
       return 0;
    }
    

    What happens within brackets, stays within brackets.

    (weeeell, it sounds better than "the first rule of scoping is that you don't talk about scoping" ;-)