For starters, the function foo
having the return type int
returns nothing.
The while
loop:
while (--n > 0)
{
//..
}
gets control only in the case when the value of the pre-decrement expression --n
is greater than 0
.
That is, within the while
loop, the variable n
is neither equal to 0
nor to -1
.
So, control will be passed at once to the label default
within the switch
statement.
switch (n)
{
case -1:
case 0:
for (int j = 0; j < m; ++j)
default:
printf(":-)");
break;
}
You may equivalently rewrite the while
loop without the switch
statement the following way to make it clearer:
while (--n > 0)
{
goto Default;
for (int j = 0; j < m; ++j)
{
Default: printf(":-)");
}
}
That is, control is at once passed inside the for
loop. According to the C Standard (6.8.5 Iteration statements)
4 An iteration statement causes a statement called the loop body to be
executed repeatedly until the controlling expression compares equal to
0. The repetition occurs regardless of whether the loop body is entered
from the iteration statement or by a jump.
It means that the for
loop will contain one statement:
printf(":-)");
will be executed.
However, the initial initialization of the variable j
in the for
loop is bypassed. From the C Standard (6.2.4 Storage durations of objects)
6 For such an object that does not have a variable length array type,
its lifetime extends from entry into the block with which it is
associated until execution of that block ends in any way. (Entering an
enclosed block or calling a function suspends, but does not end,
execution of the current block.) If the block is entered recursively,
a new instance of the object is created each time. The initial value
of the object is indeterminate. If an initialization is specified
for the object, it is performed each time the declaration or compound
literal is reached in the execution of the block; otherwise, the value
becomes indeterminate each time the declaration is reached.
So, the variable j
has indeterminate value. It means that the for
loop and, as a result, the function itself, have undefined behavior.