New block area for selection and iteration

In C99, there are new block areas for statements and selection statements, and I understand that if , while , etc. themselves are blocks as their subtitle also without { } .

C11 Specification:

6.8.4

A select statement is a block whose scope is a strict subset of the scope of its enclosing block. Each property associated with it is also a block, the volume of which is a strict subset of the approval selection area.

6.8.5

The iteration operator is a block whose volume is a strict subset of the volume of its containing block. The body of the loop is also a block, scope is a strict subset of the iteration scope.

But if I do:

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

GCC gives me errors.

How can I verify that the new rule is C99?

Can someone give me some working examples?

+2
source share
2 answers

What you are trying to do is simply not allowed by the syntax of C. N1570 ยง6.8.4 / p1:

Syntax
& EMSP, expression selection:
& emsp; & emsp; if expression (expression)

Both the expression and the operator are not considered a declaration (expression of an expression is best strict). The best you can do is to place the declaration inside the brackets {} , for example:

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

The only case permitted by the standard (and really introduced by C99) is to place an declaration instead of initializing an expression in a for loop:

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

The reference to this is ยง6.8.5 / p1 (see the following form):

Syntax
& EMSP; iteration operator:
& emsp; & emsp; while expression (expression)
& emsp; & emsp; do statement while (expression);
& emsp; & emsp; for (expression opt ; expression opt ; expression opt ))
& emsp; & emsp; for (expression of opt ; expression of opt ))

The real question here is what is the purpose of a separate area for the if (as from ยง6.8.4 / p5) if you cannot use it with a declaration expression. You see that this is only possible with a for loop.

+4
source

I read an article by Straustrap, where I discussed the problem of integrating C and C ++. As we see, it seems that the problem has not been resolved. Even operator definitions differ mostly. In C, the control expression of an if expression can be equal to the expression. So, for example, this statement

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

valid in C ++ but similar operator

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

not considered in C.

But in any case, your if statement was not specified in C ++, not in C. :)

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

It can be either an expression or an ad. 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 
+1
source

Source: https://habr.com/ru/post/1264481/


All Articles