Solution for Exercise 9-18
ex0918
#include <stdio.h>
int main()
{
int x;
for(x=0;x=10;x=x+1)
{
puts("What are you lookin' at?");
}
return(0);
}
Output
What are you lookin' at?
What are you lookin' at?
What are you lookin' at?
What are you lookin' at?
And on and on . . .
Notes
* The warning I saw from my compiler is:
ex0918.c:7:14: warning: using the result of an assignment as a condition without
parentheses
Further, the output states: use '==' to turn this assignment into an equality comparison
* The "parentheses" compiler warning is the same one you see when using a single = in an if comparison. I'm not sure of the specifics, but the cause it the same: You used one equal sign (assignment) when you really want two (comparison).
* By the way, the value of variable x cannot explode and stop the program, despite the action x=x+1 taking place after each iteration of the loop. The value of x just keeps increasing until it reaches the maximum size for a signed int. At that point, the value rolls over to a negative number and continues to increment. If left unattended, the code above generates a program that never stops.
Copyright © 1997-2025 by QPBC.
All rights reserved
