C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-13-2002, 10:02 PM   #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.
albertr is offline   Reply With Quote
Old 01-13-2002, 10:28 PM   #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!
QuestionC is offline   Reply With Quote
Old 01-15-2002, 05:37 PM   #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
hairyian is offline   Reply With Quote
Old 01-15-2002, 06:17 PM   #4
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,260
Quote:
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 01-16-2002, 12:04 AM   #5
Linguistic Engineer...
 
doubleanti's Avatar
 
Join Date: Aug 2001
Location: CA
Posts: 2,420
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...
doubleanti is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
backward debugging in Visual Studio?? George2 Tech Board 12 11-05-2006 02:17 AM
making programms faster deian C Programming 23 10-09-2004 12:19 AM
pointers fanaonc C Programming 3 11-17-2001 02:18 AM
Please help newbie with Borland Compiler and simple code!! tranced C++ Programming 3 11-04-2001 03:53 PM
Bad code or bad compiler? musayume C Programming 3 10-22-2001 09:08 PM


All times are GMT -6. The time now is 01:48 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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