Thread: Can you have nested code block? What does the compiler do? For example ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    1

    Question Can you have nested code block? What does the compiler do? For example ...

    What happens when you have nested code blocks like follows:
    Note: this is just an example. The actual code I'm working with is
    much, much messier.

    example:
    IF ( validnumber(i) )
    { /* outer code block */

    { /* inner code block */

    /* do something in here */
    }

    }

    The inner code block is surrounded by { } but it doesn't follow
    a conditional statement, or function name.

    Does anyone know whether the program will move from the outer code block
    into the inner code block and then out again back to the outer
    code block?

    Or will the compiler complain?

    The reason I ask is that I'm working with some code that uses a lot of macro string substitution,
    and in some cases code blocks may or may not end up with
    leading IF conditions statements after the preprocessor is done
    with it.

    Please help.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Don't worry, in cases like that, it will just work the same as if the inner braces weren't there.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    Consider:

    if (cond) something;

    and...

    if (cond) {
    something;
    }

    Both of these do the same thing... just to make it a bit clearer what's happening here...(items closed in <>'s are placeholders for what is described). If statements take the following form:

    if ( <condition_expression> ) <statement>

    (in reality, it's a little more complex than this, but this serves to get the point across).

    A statement can be a simple statement (like an expression terminated by a semi-colon), or a compound statement amongst other things.

    A compound statement inside another compound statement (say, in a function) behaves exactly the same as everywhere else - as the statement used in an if or while statement, as the compound statement in a function definition and so on.

    There's nothing special about compound statements... they're the same everywhere!

    Ian Woods

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The inner code block is surrounded by { } but it doesn't follow
    a conditional statement, or function name.

    Does anyone know whether the program will move from the outer code block
    into the inner code block and then out again back to the outer
    code block?
    You can use as many braces as you want. The compiler will not generate errors, it is valid:

    Code:
    #include <stdio.h>
    int main ( void )
    {
        int x;
        printf("x is visible\n");
        {
            int y;
            printf("x and y are visible\n");
            {
                int z;
                print("x, y and z are visible\n");
            }
            printf("x and y are visible\n");        
        }
        printf("x is visible\n");
        return 0;
    }
    Note that you can include additional variables with a limited scope this way.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    limiting scope is important, and using braces correctly can help you control scope very well. however make sure that in your macro's scope, you don't declare varible names that you'd have in the mother scope, as your compiler would then be confused perhaps.
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. backward debugging in Visual Studio??
    By George2 in forum Tech Board
    Replies: 12
    Last Post: 11-05-2006, 02:17 AM
  2. making programms faster
    By deian in forum C Programming
    Replies: 23
    Last Post: 10-09-2004, 12:19 AM
  3. pointers
    By fanaonc in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 02:18 AM
  4. Replies: 3
    Last Post: 11-04-2001, 03:53 PM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM