xdevel2000
xdevel2000

Reputation: 21364

New block scope for selection and iteration statement

In C99 for iteration statements and selection statements there are new block scopes and I understand that the if, while, etc. itself are block as their sub statement also without { }.

C11 Specification:

6.8.4:

A selection statement is a block whose scope is a strict subset of the scope of its enclosing block. Each associated substatement is also a block whose scope is a strict subset of the scope of the selection statement.

6.8.5

An iteration statement is a block whose scope is a strict subset of the scope of its enclosing block. The loop body is also a block whose scope is a strict subset of the scope of the iteration statement.

But If I do:

if( (int a = 10) == 10 ) // error: expected expression before '==' token
   int j = 10; // error: expected expression before 'int'

GCC give me errors.

How can I verify that C99 new rule?

Can anyone give me some working examples?

Upvotes: 2

Views: 267

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311018

I read an article of Straustrup where he discussed the problem of integration of C and C++. As we can see it seems the probem is unresolved. Even statement definitions differ fundamentally. In C the controlling expression of if statement may be ony an expression. Thus for example this statement

if ( const char *p = std::strchr( "Hello", 'H' ) ) { /*...*/ }

is valid in C++ but the similar statement

if ( char *p = strchr( "Hello", 'H' ) ) { /*...*/ }

is not vaid in C.

But in any case your if statement neither vaid in C++ nor in C.:)

if( (int a = 10) == 10 )

There can be either an expression or declaration. From the C++ Standard

condition:

expression
attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause
attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list

Upvotes: 1

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37934

What you are trying to accomplish is simply not allowed by syntax of C language. N1570 §6.8.4/p1:

Syntax
 selection-statement:
  if ( expression ) statement

Both expression and statement are not considered as declaration (declaration expression to best strict). The best what you can do is to put declaration inside {} brackets, like:

int a = 10;
if (a == 10) {
    int j = 10;
}

The only case allowed by standard (and introduced by C99 indeed) is to place declaration instead of initializing expression in for loop:

for (int a = 0, b = 0; a + b < 20; a++, b++) {
    int j = 10;
}

Reference for that is §6.8.5/p1 (see forth form):

Syntax
 iteration-statement:
  while ( expression ) statement
  do statement while ( expression ) ;
  for ( expressionopt ; expressionopt ; expressionopt ) statement
  for ( declaration expressionopt ; expressionopt ) statement

The real question here is what is the purpose of separate scope for if statement (as of §6.8.4/p5) if you can't take use of it with declaration expression. You see that is only possible with for loop.

Upvotes: 4

Related Questions