Thread: Question: C99 strange form of for loops...

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078

    Question: C99 strange form of for loops...

    ISO 9989:1999 standard says, about the language grammar, that this is a valid construction (@ 6.8.5):

    Code:
    for ( declaration expression-opt; expression-opt ) statement
    I've never seen such construction and 6.8.5.3 specifies only the usual form. The compiler (GCC), it appears, doesn't allow this form

    Someone have examples for the 2 arguments form? (I'm not able to make it work!).

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Ahhhhhhhh... Found it! It is because a declaration is defined as:

    Code:
     declaration-spacifiers init-declarator-list-opt;

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to go back to the syntax for a declaration:
    Code:
    declaration:
        declaration-specifiers init-declarator-list_opt ;
    So, the for loop syntax is actually equivalent to:
    Code:
    for ( declaration-specifiers init-declarator-list_opt ; expression_opt ; expression_opt ) statement
    that's why later in clause 6.8.5.3 the standard expresses the two possibilities in a single form:
    Code:
    for ( clause-1 ; expression-2 ; expression-3 ) statement
    but in this form the text then has to account for the different possibilities that clause-1 could take, i.e., declaration or expression.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about form...
    By CommonTater in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2011, 05:54 PM
  2. question: i/o data form .txt or .xls files ??
    By camelman in forum C++ Programming
    Replies: 21
    Last Post: 11-17-2007, 03:48 PM
  3. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  4. strange numbers, while loops questionable?
    By exluddite in forum C++ Programming
    Replies: 8
    Last Post: 05-06-2004, 11:11 AM
  5. Bitwise Explanation in the Form of a Question
    By Krak in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2004, 10:14 PM

Tags for this Thread