Need help understanding this for loop code in C

后端 未结 5 1254
孤城傲影
孤城傲影 2021-01-11 17:22

Consider the following code in C:

void main()
{
    int a=0;

    for(printf(\"\\nA\"); a; printf(\"\\nB\"));

    printf(\"\\nC\");
    printf(\"\\nD\");            


        
相关标签:
5条回答
  • 2021-01-11 17:39

    The gcc output is correct.

    The Turbo C++ 3.0 output in the first case is correct.

    The TurboC++ 3.0 output in the second case is wrong.

    You appear to have found an edge case, leading to incorrect code generation, in the Turbo C++ 3.0 compiler.

    A for-stmt in C or C++ has the general syntax

    for ( initialization ; test ; reinitialization ) stmt

    The initializations are performed once, before the loop starts. The test is performed at the TOP of the loop. If the test is true, the stmt is performed, and then the reinitializations, and the loop repeats.

    In your case, printf("\nA") is the initialization, a (or 0) is the test, printf("\nB") is the reinitialization, and the stmt is empty.

    You should have seen the A (and you did). The test should have failed on the first pass, meaning you should never have seen the stmt (but you don't know), and you should never have seen the B. This is where Turbo C++ 3.0 screwed up on the second test.

    0 讨论(0)
  • 2021-01-11 17:41

    The TCC output for the 2nd example is wrong.

    From the C99 standard:

    The statement

    for ( clause-1 ; expression-2 ; expression-3 ) statement

    behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. [...]

    Obviously, there are no iterations here, so expression-3 should never be executed.

    Similarly, in the C90 standard (or at least in a draft that I found), it says:

    Except for the behavior of a continue statement in the loop body, the statement

         for (  expression-1 ;  expression-2 ;  expression-3 )  statement
    

    and the sequence of statements

          expression-1 ;
         while ( expression-2) {
                   statement
                  expression-3 ;
         }
    

    are equivalent.

    0 讨论(0)
  • 2021-01-11 17:43

    What is the full "for" loop syntax in C (and others in case they are compatible)?

    This question quotes the applicable part of the standard. The 3rd expression shouldn't be evaluated unless the loop executes at least once. So, I'd say that in the 2nd case the old compiler is wrong to print 'B'.

    0 讨论(0)
  • 2021-01-11 17:57

    the semantics of for is that the first expression is evaluated (initializer) then the second expression is evaluated (terminator) then if the terminator evaluated to non-zero the body of the for is executed, then the third expression (advancement) is evaluated and back to evaluating the terminator.

    Since you have no body, that part amounts to no expressions evaluated. Based on this the loop should be executed as follows:

    printf("\nA");
    a; // yields 0 -> terminate loop
    

    This is indeed what happens.

    In your second example the same should happen (as is for gcc) as 0 evaluates to 0.

    It is possible that turbo C++ -- seeing the 0 constant -- attempted to perform some sort of a loop unrolling optimization (and failed to do it properly)

    0 讨论(0)
  • 2021-01-11 17:58

    Turbo C++ 3.0 was release in the 1990's, and was very quickly followed up with a release of 3.1.

    I would guess that your ancient compiler had a number of bugs in it, which were updated quickly. In addition it might not have had such bugs, but may have emitted optimized assembly which fails under the new pipe lining architectures.

    In any event, it is guaranteed that Turbo C++ 3.0 is not supported on your current platform. When it comes to a compiler not being supported because the platform was created nearly 20 years later, you cannot really fault it for emitting the wrong program.

    0 讨论(0)
提交回复
热议问题