Is it legal to create a new scope with { ... } without using if, while, do etc?

R

Roy Hills

I'm writing a local patch, which will be applied to several different
source files. To prevent my local variables from clashing with those
in the main source file, I want to create a new scope.

Currently, I'm using the following, which works perfectly and I've
seen used elsewhere:

do {
int i;
int j;

statements;
} while(0);

However, I'm wondering if I need the do/while bit at all, or if I can
just use the braces on their own, e.g:

{
int i;
int j;

statements;
}

This works OK with "gcc -Wall -pedantic -ansi", but I'm wondering if
it's standard C that should be accepted by any compiler. What's
worrying me is that I've not seen this construct used elsewhere, which
makes me question its validity.

Roy
 
M

Mike Wahler

Roy Hills said:
I'm writing a local patch, which will be applied to several different
source files. To prevent my local variables from clashing with those
in the main source file, I want to create a new scope.

Currently, I'm using the following, which works perfectly and I've
seen used elsewhere:

do {
int i;
int j;

statements;
} while(0);

However, I'm wondering if I need the do/while bit at all, or if I can
just use the braces on their own, e.g:

{
int i;
int j;

statements;
}

This works OK with "gcc -Wall -pedantic -ansi", but I'm wondering if
it's standard C that should be accepted by any compiler. What's
worrying me is that I've not seen this construct used elsewhere, which
makes me question its validity.

Yes, it's perfectly valid (if it's done inside an already
existing block -- i.e. you can't do it at file scope).
I have indeed often used this method myself (for various reasons).


-Mike
 
K

Kenneth Brody

Roy said:
I'm writing a local patch, which will be applied to several different
source files. To prevent my local variables from clashing with those
in the main source file, I want to create a new scope.

Currently, I'm using the following, which works perfectly and I've
seen used elsewhere:

do {
int i;
int j;

statements;
} while(0);

However, I'm wondering if I need the do/while bit at all, or if I can
just use the braces on their own, e.g:

{
int i;
int j;

statements;
}

This works OK with "gcc -Wall -pedantic -ansi", but I'm wondering if
it's standard C that should be accepted by any compiler. What's
worrying me is that I've not seen this construct used elsewhere, which
makes me question its validity.

Yes, it's perfectly legal. It's no different than doing:

if ( condition )
statement;

versus

if ( condition )
{
statement;
}

I use it in many places such as:

#if DEBUG
{
FILE *f = fopen("debug.log","a");
int x, y, z;
if ( f )
{
dump debug info
fclose(f);
}
}
#endif

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
P

pemo

Roy Hills said:
I'm writing a local patch, which will be applied to several different
source files. To prevent my local variables from clashing with those
in the main source file, I want to create a new scope.

Currently, I'm using the following, which works perfectly and I've
seen used elsewhere:

do {
int i;
int j;

statements;
} while(0);

However, I'm wondering if I need the do/while bit at all, or if I can
just use the braces on their own, e.g:

{
int i;
int j;

statements;
}

This works OK with "gcc -Wall -pedantic -ansi", but I'm wondering if
it's standard C that should be accepted by any compiler. What's
worrying me is that I've not seen this construct used elsewhere, which
makes me question its validity.

It's just a macro safety thing I think, e.g.

#define ver(x)\
{\
return x;\
}

#define ver1(x)\
do\
{\
return x;\
}\
while(0)

....

in some code

if (a == b)
ver(c);
else
....

if (a == b)
ver1(c);
else
....

the former expands to:

if (a == b)
{
return c
};
else
....

the latter expands to:

if (a == b)
do{
return c
}while(0);
else
....
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top