NO

Author Topic: Simple program: Local variable declaration doesn't work, but global does.  (Read 3076 times)

chassan

  • Guest
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);
}

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Take optimization off (none) and look it in debugger again.
May the source be with you

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
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

  • Guest
Hi,
I had a similar problem with static variables.
Look at http://forum.pellesc.de/index.php?topic=3014.0
It was OK with PellesC 5.

Henrin

chassan

  • Guest
Hi Folks,

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

Best Regards,
Chris