delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/22/15:21:29

Comments: Authenticated sender is <mert0407 AT sable DOT ox DOT ac DOT uk>
From: "George Foot" <george DOT foot AT merton DOT oxford DOT ac DOT uk>
To: puva AT earthlink DOT net
Date: Sat, 22 Aug 1998 20:19:51 +0000
MIME-Version: 1.0
Subject: Re: Local Variables
Reply-to: george DOT foot AT merton DOT oxford DOT ac DOT uk
CC: djgpp AT delorie DOT com
Message-Id: <E0zAJDw-0003vw-00@sable.ox.ac.uk>

On 22 Aug 98 at 14:38, puva AT earthlink DOT net wrote:

> I don't know if i'm sending this question to the right place or not,
> hehe, but I can't seem
> to use local variables in my source files. Like:
> 
> void main()
> {
>         int a;
> }
> 
> would give an error like, "parse error before int" or "parse error
> before a". It works fine if the variables are outside of main(), but not
> anything local. Is there something wrong with how the compiler is setup?
> or is there a header missing or something?

Did you actually try to compile the above code?  It works for me.  
Note though that `main' should return `int'.

I think perhaps you're trying to declare local variables other than 
at the start of a block.  This is not allowed (in C), and is a 
horrible thing to want to do anyway.  If you want a local variable to 
have limited scope in a function, make a subblock:

    #include <stdio.h>
    
    int main (void)
    {
        char s = 'X';
        printf ("Outside subblocks: s = %c\n", s);
        {
            char *s = "Hello world!";   /* Start of block, so we can declare */
            printf ("In first subblock: s = %s\n", s);
        }
        printf ("Outside subblocks again: s = %c\n", s);
        {
            int s = 5;                 /* Start of another block -- this is a */ 
                                       /* different `s' */ 
            printf ("In second subblock: s = %d\n", s);
        }
        printf ("Outside subblocks yet again: s = %c\n", s);
        return 0;
    }

Output:

    Outside subblocks: s = X
    In first subblock: s = Hello world!
    Outside subblocks again: s = X
    In second subblock: s = 5
    Outside subblocks yet again: s = X

-- 
george DOT foot AT merton DOT oxford DOT ac DOT uk

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019