Pelles C forum

C language => Beginner questions => Topic started by: chassan on March 30, 2010, 05:13:52 AM

Title: Simple program: Local variable declaration doesn't work, but global does.
Post by: 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
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);
}
Title: Re: Simple program: Local variable declaration doesn't work, but global does.
Post by: TimoVJL on March 30, 2010, 09:23:02 AM
Take optimization off (none) and look it in debugger again.
Title: Re: Simple program: Local variable declaration doesn't work, but global does.
Post by: Bitbeisser on March 30, 2010, 06:40:14 PM
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
Title: Re: Simple program: Local variable declaration doesn't work, but global does.
Post by: henrin on April 10, 2010, 04:45:45 PM
Hi,
I had a similar problem with static variables.
Look at http://forum.pellesc.de/index.php?topic=3014.0 (http://forum.pellesc.de/index.php?topic=3014.0)
It was OK with PellesC 5.

Henrin
Title: Re: Simple program: Local variable declaration doesn't work, but global does.
Post by: chassan on April 16, 2010, 09:11:07 PM
Hi Folks,

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

Best Regards,
Chris