Simple program: Local variable declaration doesn't work, but global does.

Started by chassan, March 30, 2010, 05:13:52 AM

Previous topic - Next topic

chassan

Hello folks,

The code (below) exhibits the following (strange) behavior:

WHEN AGE IS DECLARED AS A LOCAL VARIABLE IN MAIN:
1. program does not work as expected
2. "age" is not available in the debug watch window

WHEN AGE IS DECLARED AS A GLOBAL VARIABLE (JUST ABOVE MAIN):
3. program works as expected
4. "age" is available in the debug watch window

(Could this be because I am using Windows Vista?)

Any help is greatly appreciated.

Best Regards,
Chris


#include<stdio.h>

int main(void)
{
   int age;
   age = 18;
   
   switch(age)
   {
      case 16 :
      case 17 :
         printf("Old enough to drive\n");
         break;
      case 18 :
      case 19 :
      case 20 :
         printf("Old enough to vote\n");
         break;
      case 21 :
         printf("Old enough to drink\n");
         break;
   }
   return(0);
}

TimoVJL

May the source be with you

Bitbeisser

Quote from: chassan on March 30, 2010, 05:13:52 AM
Hello folks,

The code (below) exhibits the following (strange) behavior:

WHEN AGE IS DECLARED AS A LOCAL VARIABLE IN MAIN:
1. program does not work as expected
Well, can you be a bit more specific as to what you expect. It seems you have at least a major logic error in your program, not sure if that is causing to thwart your expectations...  ;)

Ralf

henrin


chassan

Hi Folks,

Thanks for all the input.  It worked when I selelcted "none" for the compiler optimization level.

Best Regards,
Chris