Thread: multiple nonlocal variables

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    a2 has a local scope. When main() returns, a2 ceases to exist. So that local scope is associated with the main() function. Some people describe this as "function scope"
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  2. #17
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Quote Originally Posted by grumpy View Post
    a2 has a local scope. When main() returns, a2 ceases to exist. So that local scope is associated with the main() function. Some people describe this as "function scope"
    Pardon if I'm missing the answer in that, but my question is which one is true:

    A local variable by definition has local scope, a non-local variable by definition cannot have local scope.
    So is this untrue then? [...]
    It is very true.
    or
    a = a + 2; //This uses a2; it is nonlocal to this block, but it has local scope and is currently the only a in scope

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    They are both true, although personally I would not have used quite the wordings that tabstop did.

    a2 is within the scope associated with the function main(), and hides a1 (at file scope). The body of the while loop within main() defines a new scope, That new scope is within the scope associated with main(), so can access a2 - another a (called a3) is introduced within that scope.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by grumpy View Post
    They are both true, although personally I would not have used quite the wordings that tabstop did.

    a2 is within the scope associated with the function main(), and hides a1 (at file scope). The body of the while loop within main() defines a new scope, That new scope is within the scope associated with main(), so can access a2 - another a (called a3) is introduced within that scope.
    I don't like it either, but I was stuck with the nouns listed in the C++ standard.

  5. #20
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by tabstop View Post
    I don't like it either, but I was stuck with the nouns listed in the C++ standard.
    This is just one of those topics that are hard to express within the confines of the standard's grammer. Eh, I think the question has been answered, unless the OP has something else he is confused about.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can this be done - multiple variables stored
    By spadez in forum C Programming
    Replies: 2
    Last Post: 04-14-2009, 10:13 AM
  2. comparing multiple variables..
    By myrddin in forum C Programming
    Replies: 9
    Last Post: 10-16-2007, 07:03 PM
  3. Multiple variables in sprintf
    By 3saul in forum C Programming
    Replies: 4
    Last Post: 02-15-2006, 09:25 PM
  4. Multiple variables
    By Kaho in forum C Programming
    Replies: 2
    Last Post: 08-18-2005, 04:32 AM
  5. multiple instances and variables
    By FOOTOO in forum Windows Programming
    Replies: 1
    Last Post: 04-07-2005, 10:54 AM