Home > Enterprise >  Multiple printf in the For-loop as part of the initialization, condition and update
Multiple printf in the For-loop as part of the initialization, condition and update

Time:11-24

Could anyone explain to me why it prints 32 and the overall concept of how this works?

#include <stdio.h>

int main()
{
    int a=1;
    for (printf ("3"); printf ("2"); printf ("1"))

    return 0;
}

CodePudding user response:

Proper indentation would make it clearer:

#include <stdio.h>

int main()
{
    int a=1;
    for (printf ("3"); printf ("2"); printf ("1"))
        return 0;
}

So the following happens:

  • a is initialized to 1. I don't know why this variable exists, since it's never used.
  • for executes its initialization statement, printf("3");. This prints 3.
  • for evaluates its repetition condition, printf("2"). This prints 2 and returns the number of characters that were printed, which is 1.
  • Since the condition is truthy, it goes into the body of the loop.
  • The body executes return 0;. This returns from main(), thus ending the loop.

Since the loop ends, we never evaluates the update expression, printf("1"), so it never prints 1. And we get no repetition of anything.

CodePudding user response:

You know, a program begins to run from the left ‘{’ of function main(), end in the right '}' of function main(), if there is no endless loop.

As your code shows, Your difficulty is to understand the flowchart of the for loop in C language.

As you can see, The syntax of the for loop is:
for (initializationStatement; testExpression; updateStatement)
{
    // statements inside the body of loop
    for loop body;
}
How for loop works?

1.The initialization statement is executed only once.

2.Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is terminated.

3.However, if the test expression is evaluated to true, statements inside the body of the for loop are executed, and the update expression is updated.

4.Again the test expression is evaluated.

This process goes on until the test expression is false. When the test expression is false, the loop terminates.

So, for loop Flowchart

enter image description here

Firstly, Take your code as an example:
#include <stdio.h>
int main(){
    for (printf ("3"); printf("2"); printf("1")) break;
    return 0;
}
Output
32

1.initialization is "printf ("3")", then, print:

3

2.The test expression "printf("2")", that always true, so print:

2

3.for body loop "break", that means ending the execution of the for loop,

do not execute the updated expression "printf("1")"

also, the program jump out of the for loop, and jump to "return 0;",

then, end the the execution of this program.

So, the output is

32
Secondly, Take your changed code as an example:
#include <stdio.h>
int main(){
    for (printf ("3"); printf("2"); printf("1")) ;
    return 0;
}
Output
321212121212121212...121212...1212...

Similarly,

1.initialization is "printf ("3")", then, print:

3

2.The test expression "printf("2")", that always true, so print:

2

3.for body loop "``", empty, then do nothing. goto the updated expression

"printf("1")"

4.the updated expression "printf("1")", then, print

1

5.then, goto the test expression "printf("2")", that is "2.The test

expression "printf("2")", that always true, so print".Because the

the body of loop is "``",empty, then always goto from the updated

expression "printf("1")" to the test expression "printf("2")",

that's why after printing "32" that function prints infinite loop

"12".And, that function never end stop printing "12" unless you

stop that function.

So, So, So the output is

32121212...121212...121212...
Thirdly, Take your recently changed code as an example:
#include <stdio.h>

int main()
{
    int a=1;
    for (printf ("3"); printf ("2"); printf ("1")) 

    return 0;
}
Output
32

1.the program begins to run from the left ‘{’ of function main(),

that's the initialization statement of Temporary variable

"int a=1;".

That statement defines an "int" typed temporary variable "a", and

sets its value to "1", But that print nothing!

2.then, the program goto the for loop.

3.the initialization statement is "printf ("3")", then, print

"3",and

goto the test expression.

3

4.The test expression "printf("2")", that always true, so

print "2", and goto the for loop body expression.

2

5.the for loop body expression "return 0", the expression

"return 0" return '0' to the function main(),and end the

execution of the main(), But print nothing.

So, the output is:

32
End. Thanks!
  • Related