Scope question

Discussion in 'C Programming' started by jacob navia, Sep 20, 2007.

  1. jacob navia

    jacob navia Guest

    polas wrote:
    > Morning all. I have a question - I am really not sure about the answer
    > and hope that it is not compiler specific... I am writing a language
    > translator to translate into C. There are some additional scoping
    > issues in the language which I would like to preserve in the C code.
    > Because of this I was wondering if there is a "good" way of creating a
    > scoped block in C - of course I could use a dummy conditional or
    > something like that, but I was wondering if there is a better way.
    >
    > Cheers,
    > Nick
    >


    Anywhere in your code you can create a block scope.

    ....
    {
    local variables
    statements
    }

    the local variables in that block will be only visible
    in that bloack. You do not need a conditional to open
    a block.
     
    jacob navia, Sep 20, 2007
    #1
    1. Advertising

  2. jacob navia

    polas Guest

    Morning all. I have a question - I am really not sure about the answer
    and hope that it is not compiler specific... I am writing a language
    translator to translate into C. There are some additional scoping
    issues in the language which I would like to preserve in the C code.
    Because of this I was wondering if there is a "good" way of creating a
    scoped block in C - of course I could use a dummy conditional or
    something like that, but I was wondering if there is a better way.

    Cheers,
    Nick
     
    polas, Sep 20, 2007
    #2
    1. Advertising

  3. polas said:

    > Morning all. I have a question - I am really not sure about the answer
    > and hope that it is not compiler specific... I am writing a language
    > translator to translate into C. There are some additional scoping
    > issues in the language which I would like to preserve in the C code.
    > Because of this I was wondering if there is a "good" way of creating a
    > scoped block in C - of course I could use a dummy conditional or
    > something like that, but I was wondering if there is a better way.


    {
    }

    For example:

    int foo(void)
    {
    int i; /* i is in scope here */

    { /* this introduces a new scope, and you don't need
    a dummy conditional. */


    int i; /* this i is in scope here, and the other one is
    no longer visible because this one masks it */


    /* this i is about to go out of scope */
    }

    /* the first i is now visible again */


    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 20, 2007
    #3
  4. jacob navia

    polas Guest

    On 20 Sep, 10:15, jacob navia <> wrote:
    > polas wrote:
    > > Morning all. I have a question - I am really not sure about the answer
    > > and hope that it is not compiler specific... I am writing a language
    > > translator to translate into C. There are some additional scoping
    > > issues in the language which I would like to preserve in the C code.
    > > Because of this I was wondering if there is a "good" way of creating a
    > > scoped block in C - of course I could use a dummy conditional or
    > > something like that, but I was wondering if there is a better way.

    >
    > > Cheers,
    > > Nick

    >
    > Anywhere in your code you can create a block scope.
    >
    > ...
    > {
    > local variables
    > statements
    >
    > }
    >
    > the local variables in that block will be only visible
    > in that bloack. You do not need a conditional to open
    > a block.


    Thats great thanks for the help Richard and Jacob - I never realised
    that a block could be created that way

    Nick
     
    polas, Sep 20, 2007
    #4
  5. jacob navia

    jacob navia Guest

    Philip Potter wrote:
    > polas wrote:
    >> On 20 Sep, 10:15, jacob navia <> wrote:
    >>> polas wrote:
    >>>> Morning all. I have a question - I am really not sure about the answer
    >>>> and hope that it is not compiler specific... I am writing a language
    >>>> translator to translate into C. There are some additional scoping
    >>>> issues in the language which I would like to preserve in the C code.
    >>>> Because of this I was wondering if there is a "good" way of creating a
    >>>> scoped block in C - of course I could use a dummy conditional or
    >>>> something like that, but I was wondering if there is a better way.
    >>>> Cheers,
    >>>> Nick
    >>> Anywhere in your code you can create a block scope.
    >>>
    >>> ...
    >>> {
    >>> local variables
    >>> statements
    >>>
    >>> }
    >>>
    >>> the local variables in that block will be only visible
    >>> in that bloack. You do not need a conditional to open
    >>> a block.

    >>
    >> Thats great thanks for the help Richard and Jacob - I never realised
    >> that a block could be created that way

    >
    > It's nice when Richard and Jacob agree with each other. It doesn't seem
    > like it happens very often round here...
    >
    > Phil
    >


    Well, he *is* an authority in technical things. I would not discuss
    that.
     
    jacob navia, Sep 20, 2007
    #5
  6. polas wrote:
    > On 20 Sep, 10:15, jacob navia <> wrote:
    >> polas wrote:
    >>> Morning all. I have a question - I am really not sure about the answer
    >>> and hope that it is not compiler specific... I am writing a language
    >>> translator to translate into C. There are some additional scoping
    >>> issues in the language which I would like to preserve in the C code.
    >>> Because of this I was wondering if there is a "good" way of creating a
    >>> scoped block in C - of course I could use a dummy conditional or
    >>> something like that, but I was wondering if there is a better way.
    >>> Cheers,
    >>> Nick

    >> Anywhere in your code you can create a block scope.
    >>
    >> ...
    >> {
    >> local variables
    >> statements
    >>
    >> }
    >>
    >> the local variables in that block will be only visible
    >> in that bloack. You do not need a conditional to open
    >> a block.

    >
    > Thats great thanks for the help Richard and Jacob - I never realised
    > that a block could be created that way


    It's nice when Richard and Jacob agree with each other. It doesn't seem like it
    happens very often round here...

    Phil

    --
    Philip Potter pgp <at> doc.ic.ac.uk
     
    Philip Potter, Sep 20, 2007
    #6
  7. Philip Potter said:

    <snip>

    > It's nice when Richard and Jacob agree with each other.


    Don't count on it. I haven't seen Mr Navia's reply, so I can't tell whether
    I agree with him or not.

    > It doesn't seem like it happens very often round here...


    Since he's in my killfile, that's not very surprising. He's there because
    he was wrong so often that people got fed up with my correcting him. Now
    that I've killfiled him, /they/ have to correct him - which they have had
    to do rather a lot.

    I'd be surprised (and delighted) if his reply in this thread were devoid of
    inaccuracies.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 20, 2007
    #7
  8. jacob navia

    Richard Guest

    Richard Heathfield <> writes:

    > Philip Potter said:
    >
    > <snip>
    >
    >> It's nice when Richard and Jacob agree with each other.

    >
    > Don't count on it. I haven't seen Mr Navia's reply, so I can't tell whether
    > I agree with him or not.
    >
    >> It doesn't seem like it happens very often round here...

    >
    > Since he's in my killfile, that's not very surprising. He's there because
    > he was wrong so often that people got fed up with my correcting him. Now
    > that I've killfiled him, /they/ have to correct him - which they have had
    > to do rather a lot.
    >
    > I'd be surprised (and delighted) if his reply in this thread were devoid of
    > inaccuracies.


    It is such a shame that someone with your undoubted skills in C should
    take such delight in being such a vindictive person. Jacob is frequently
    helpful and practical to many posters. He doesn't sneer or talk down to
    anyone. Should you ever make that position available, I doubt if he
    would take it.
     
    Richard, Sep 20, 2007
    #8
  9. jacob navia

    Army1987 Guest

    On Thu, 20 Sep 2007 14:21:53 +0200, Richard wrote:

    > Richard Heathfield <> writes:
    >
    >> Philip Potter said:
    >>
    >> <snip>
    >>
    >>> It's nice when Richard and Jacob agree with each other.
    >>> It doesn't seem like it happens very often round here...

    >>
    >> Since he's in my killfile, that's not very surprising. He's there because
    >> he was wrong so often that people got fed up with my correcting him. Now
    >> that I've killfiled him, /they/ have to correct him - which they have had
    >> to do rather a lot.
    >>
    >> I'd be surprised (and delighted) if his reply in this thread were devoid of
    >> inaccuracies.

    >
    > It is such a shame that someone with your undoubted skills in C should
    > take such delight in being such a vindictive person. Jacob is frequently
    > helpful and practical to many posters.

    Is he? He's in my killfile too, but in the stuff I read written by
    him and quoted by others, the one in this thread are the first
    ones really useful (for someone who doesn't have lcc-win32, at
    least) since weeks.
    > He doesn't sneer or talk down to anyone.

    Doesn't he anymore? Good news...
    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes†feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen
     
    Army1987, Sep 20, 2007
    #9
  10. jacob navia

    Army1987 Guest

    On Thu, 20 Sep 2007 11:38:11 +0000, Richard Heathfield wrote:

    > Philip Potter said:
    >
    > <snip>
    >
    >> It's nice when Richard and Jacob agree with each other.

    [snip]
    > I'd be surprised (and delighted) if his reply in this thread were devoid of
    > inaccuracies.

    To be honest (because I am too good a person, rather than because
    I think he deserves that), I temporarily turned the filter off,
    and I saw that JN's post was entirely quoted by polas. Apart from
    a typo there is nothing wrong with it. Strange but true...
    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes†feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen
     
    Army1987, Sep 20, 2007
    #10
  11. Army1987 said:

    > On Thu, 20 Sep 2007 11:38:11 +0000, Richard Heathfield wrote:
    >
    >> Philip Potter said:
    >>
    >> <snip>
    >>
    >>> It's nice when Richard and Jacob agree with each other.

    > [snip]
    >> I'd be surprised (and delighted) if his reply in this thread were devoid
    >> of inaccuracies.

    > To be honest (because I am too good a person, rather than because
    > I think he deserves that), I temporarily turned the filter off,
    > and I saw that JN's post was entirely quoted by polas. Apart from
    > a typo there is nothing wrong with it. Strange but true...


    If that is the case (and Polas has omitted nothing from that quote), then
    the reply is indeed inaccurate, as I had feared, although it was such a
    trivial question that it was difficult to get it terribly wrong.

    Mr Navia's claim, "Anywhere in your code you can create a block scope." is
    simply not true, and it is so trivial to demonstrate this that I would not
    think it inappropriate to hand the proof to first-week C students as an
    exercise.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 20, 2007
    #11
  12. Army1987 said:

    > On Thu, 20 Sep 2007 14:21:53 +0200, Richard wrote:
    >

    <snip>

    >> It is such a shame that someone with your undoubted skills in C should
    >> take such delight in being such a vindictive person. Jacob is frequently
    >> helpful and practical to many posters.

    > Is he?


    No, but then neither is Richard Riley. Last I checked, he restricted his
    activities to criticising the helpful rather than trying to be helpful
    himself. For that reason, Mr Riley, too, is in my killfile. From your
    quote, it is evident that this does not prevent him making lousy
    deductions and outrageous claims, but hey, life's too short to spend it
    fighting with bozos.

    <snip>

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 20, 2007
    #12
  13. jacob navia

    Army1987 Guest

    On Thu, 20 Sep 2007 15:39:55 +0000, Richard Heathfield wrote:

    > Mr Navia's claim, "Anywhere in your code you can create a block scope." is
    > simply not true, and it is so trivial to demonstrate this that I would not
    > think it inappropriate to hand the proof to first-week C students as an
    > exercise.


    Probably he didn't really mean *that* by "anywhere", but...
    Ok, finding something correct written by him is even harder than
    I imagined.
    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes†feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen
     
    Army1987, Sep 21, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    986
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    689
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    510
  4. Xah Lee
    Replies:
    0
    Views:
    2,277
    Xah Lee
    Feb 26, 2009
  5. Replies:
    0
    Views:
    179
Loading...

Share This Page