Reputation: 21
How does this type of loop work and why is it printing x=1
; instead x=3
?
#include <stdio.h>
#include <stdlib.h>
int main() {
char x = 2;
for (; x++;);
printf("x=%d", x);
}
Upvotes: 1
Views: 756
Reputation: 64710
for(;x++;);
This is a loop with no body.
It will stop when x++
evaluates to False (0)
.
Because x
starts at 2
, and has type char
, it will (most likely) increment to +127, then wrap around to -128, then increment to up to 0
, when the loop finally ends.
(As the commenters point out, wrap-around on signed types is not part of the spec, and is technically implementation-defined behavior. But most common implementations do wrap around in a predictable fashion.)
When the loop is finally done, the value of x
is 1
, due to the post-increment, and the value will be printed out.
The final output will be: x=1
Upvotes: 0
Reputation: 223663
After Int
is corrected to int
, the loop for(;x++;);
will execute the null statement ;
until x++
evaluates as false (zero).
Since x
starts at two, it is incremented to three, then four, and so on. This continues until x
reaches the maximum value of a char
. Then adding one yields a value that char
cannot represent.1 This number is then converted to char
for storing in x
.
If char
is unsigned, this conversion wraps to zero, so zero is stored in x
. If char
is signed, the result of the conversion is implementation-defined (or a signal is raised). A typical result is to wrap to the minimum char
value, commonly −128. In this case, the loop then proceeds to increment x
through −127, −126, and so on, until it reaches zero.
Thus, in either of the common cases above, x
eventually reaches zero. Then the next evaluation of x++
increments x
to one and evaluates to zero. Then the loop exits, and printf("x=%d",x);
prints “x=1”.
Other behaviors are possible. For example, a C implementation could define the conversion of an out-of-range value to char
to produce 127, and then the loop would not terminate. (Interestingly, though, the rule in C 2018 6.8.5 6 allows a compiler to assume the loop terminates, even though actually executing it would not.)
1 The arithmetic is done in the int
type, so it does not overflow (except in hypothetical bizarre C implementations where char
is the same width as int
). In specifying the postfix ++
operator, C 2018 6.5.2.4 2 refers to the additive and assignment operators, which promote char
operands to int
.
Upvotes: 9
Reputation: 91119
This code relies on overflow behaviour, and I think with signed types (which char
possibly can be) this is undefined behaviour.
But if it isn't, the following works:
char x=2;
x gets assigned 2.
for(;x++;);
This is a loop with an empty method body and its break condition x++
. You could as well write while (x++);
.
It loops, doing nothing but incrementing x
, until x evaluates "false" (0) before incrementing:
That last increment makes x
have the value 1.
Thus,
printf("x=%d",x);
prints x=1
.
Upvotes: 0
Reputation: 422
char uses one byte of memory. So, its range is from -128 to 127.
char x=2;
for(;x++;);
This loop will run for values x = {2, 3, ..., 127, -128, -127, ..., -1, 0}.
As soon as x becomes 0, the condition check will stop the loop but the increment will also take place.
Hence, 0 becomes 1 and hence 1 will be printed.
Upvotes: -1