Reputation: 498
There's this code in C
int fun()
{
static int num = 40;
return num--;
}
int main()
{
for(fun(); fun(); fun())
{
printf("%d ", fun());
}
getchar();
return 0;
}
the output comes out to be: 38 35 32 29 26 23 20 17 14 11 8 5 2
I'm not able to figure out why the program doesn't continue to print beyond 2. i.e. in the negatives .shouldn't it continue printing ... -1 -4 -7....infinite loop Can anyone explain ?
Upvotes: 1
Views: 147
Reputation: 36477
The whole magic happens inside your for()
loop.
The basic format is like this:
for (<pre-iteration>; <condition>; <post-iteration>)
<code>
Code inside pre-iteration will be executed once before the first iteration/loop.
Code inside condition is evaluated before each and every iteration. If the resulting value is true
, the loop continues, if it's false
, the loop is left.
Code inside post-iteration is executed after each and every iteration.
To make the whole code easier to understand, it's important to know that you're able to write such a for()
loop using a while()
loop as well:
<pre-iteration>
while (<condition>) {
<code>
<post-iteration>
}
Back to your example:
for (fun(); fun(); fun())
{
printf("%d ", fun());
}
Using a while()
this can be written like this:
fun();
while (fun()) {
printf("%d ", fun());
fun();
}
Since fun()
will return the value of the static value num
and then decrement it by 1
the first iteration will look like this:
// This is going to be the first call, so the static variable is initialized: num = 40
fun(); // returns 40; num = 39
while (fun()) { // returns 39; num = 38 (true -> loop continues)
printf("%d ", fun()); // returns 38; num = 37 (-> 38 is written to console)
fun(); // returns 37; num = 36
}
The next iteration (code before the loop is no longer executed for obvious reasons):
fun(); // no longer executed
while (fun()) { // returns 36; num = 35 (true -> loop continues)
printf("%d ", fun()); // returns 35; num = 34 (-> 35 is written to console)
fun(); // returns 34; num = 33
}
This continues till the last iteration:
fun(); // no longer executed
while (fun()) { // returns 0; num = -1 (false -> loop exits)
printf("%d ", fun()); // no longer executed
fun(); // no longer executed
}
I'm not able to figure out why the program doesn't continue to print beyond 2. i.e. in the negatives .shouldn't it continue printing ... -1 -4 -7....infinite loop Can anyone explain ?
Yes, it might continue, but only if that specific call to fun()
wouldn't return 0
, i.e. set the initial value of num
to 41
and the program will run indefinitely (or at least till the return value happens to be 0
at some point).
Upvotes: 3
Reputation: 30126
When you print fun()
, you do not see the actual value of num
, but the value of num+1
.
This is because you are using a postfix-decrement (num--
) and not a prefix-decrement (--num
).
So when you 2 is printed, the actual value of num
is 1.
At the end of this iteration, fun()
is invoked, setting num
to 0.
At the beginning of the next iteration, num
is evaluated as false and the for
loop is terminated.
Upvotes: 1
Reputation: 74596
fun()
is evaluating to 0
here:
for(fun(); fun(); fun())
// ^
0
is the equivalent of false
in C, so the loop is exiting.
The code relies on the fact that num - 1
is a multiple of 3, as fun()
is evaluated once at the start of the loop and then 3 times every loop. If, for example, you changed the definition to
static int num = 41;
fun()
would return 0
in the wrong place and your loop would continue into the negative numbers.
Upvotes: 5
Reputation: 3209
for(fun(); fun(); fun())
second part of for()
loop is a condition - if fun()
returns 0, which is interpreted as false, the loop terminates. lately i got a brain fart and wrote a statement that condition is true only for positive numbers.
Upvotes: 1
Reputation: 15347
When
printf("%d ", fun());
is run, fun() has value 2 Then the last fun() in the next command is executed to increase the counter (normally):
for(fun(); fun(); fun())
fun() is 1.
Then the condition to continue the for loop is performed, which is the second fun() in:
for(fun(); fun(); fun())
At this point, fun() is 0 which makes the for loop stop.
Upvotes: 1
Reputation: 4203
Let's look at what happens in the second call to fun()
in the for
loop. The first time the loop is entered, fun()
has been evaluated once already, so num
is 39
. The second time we get to the loop, fun()
has been executed three more times, so it is now 36
. This process continues, until eventually, the second call to fun()
returns 0
. In C, 0
means false
, so the loop terminates at that stage.
Upvotes: 2