-3

I've known that

for (...; ...; ...)
    printf ("Fulfill the limits.");

and

if  (...)
    printf ("Fulfill the limits.");

are C standard.

But

for (...; ...; ...)
    if (...)
       for (...; ...; ...)
           if (...)
              for (...; ...; ...)
                  printf ("Fulfill all the limits.");

is compiled successfully, and run without (logic) errors.

Does complicated-nested for/if statement really follow the C standards or just mingw32 compiler-specific?

Any reference?

Thanks.

Community
  • 1
  • 1
Kevin Dong
  • 5,001
  • 9
  • 29
  • 62

2 Answers2

4

It is perfectly valid syntax. The syntax is not complicated, simply nested like x1 + (x2 + (x3 + ..).

That is, if/for is a statement and if/for contains a statement. This can be seen in the recursive BNF grammar rules:

statement:
   "if" "(" expression ")" statement |
   "for" "(" expression? ";" expression? ";" expression? ")" statement |
   ..
user2864740
  • 60,010
  • 15
  • 145
  • 220
2

Yes. Your complicated ( of course not :) ) nested for/if statement is following the C standard. But

 if (...; ...; ...)  

is not in C standard.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • @EricPostpischil Well... While you both are discussing the C grammar, I have read through my reference book. In the book, just as what EricPostpischil said, I cannot find any of the grammar in C Lang. The only thing I could find is that `for(;;) Statement;` and `if() Statement` are correct usage, and the book doesn't mention whether the `complicated-nested` is correct usage or not, but `complicated-nested` can be perfectly compiled with obsolete Turbo C Compiler, and GCC/Mingw. – Kevin Dong Nov 25 '13 at 14:31
  • @KVD There's no such thing as a "complicated nested" statement. From the grammar, it can be deduced that using an if inside a for inside and if inside a for, etc... is valid. It's implicit, because that (i. e. a recursive definition) is the most convenient way for defining it. So it **is** in the standard, you just have to read it correctly. –  Nov 25 '13 at 17:14