A simple question about scope

This is a discussion on A simple question about scope within the C Programming forums, part of the General Programming Boards category; Hi, this is my first post. It concerns the scope of a loop control counter object declared inside the for ...

  1. #1
    external validation
    Join Date
    Dec 2004
    Posts
    9

    A simple question about scope

    Hi, this is my first post.

    It concerns the scope of a loop control counter object declared inside the for statement...

    Here is an example:
    Code:
    {
       statement;
       ...
       statement;
       // let's call this "SCOPE 1"
       // now...
       for (int i=1; 1<=10; i++)
       {
          statement;
          ...
          statement;
          // let's call this "SCOPE 2"
       }
       statement;
       ...
       // now we're back to "SCOPE 1"
    }
    Could someone (well, ideally many people) tell me whether the scope of i (as declared inside the for statement) is SCOPE 1 or SCOPE 2 ?

    Much obliged,
    Doug.
    Last edited by dwylie; 12-10-2004 at 12:14 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Why don't you just try it?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Welcome to the forums!

    In C89 variables must be declared at the start of a block. That is, after a { and before other statements. In C99 and C++ compilers, one of which you must be using, you can declare variables elsewhere. You should be aware that not all compilers imlement C99 yet. A variable declared in the initializer of a for loop will have scope only in the for block. That is "Scope 2" in your example. It will not be visible in "Scope 1". You can easily test scope by trying to use a variable in the different scopes. Your compiler will trigger an error if the variable is not visible.

    Please consider reading this post.

  4. #4
    external validation
    Join Date
    Dec 2004
    Posts
    9

    thanks for the advice

    Right ho.
    I put in the code tags and I am away to start an X-session and learn by experimentation
    D.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  2. simple scope question
    By 7stud in forum C++ Programming
    Replies: 18
    Last Post: 01-30-2005, 06:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM
  5. Scope question
    By mikebrewsj in forum C++ Programming
    Replies: 1
    Last Post: 01-17-2002, 03:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21