[C I am a beginner ][1]
I dont know what the problem with my code
#include <stdio.h>
main() {
int fahr;
for (fahr = 320; fahr = 0; fahr = fahr - 20)
printf("%d %f \n", fahr, (5.0 / 9.0) *.(fahr - 32));
}
CodePudding user response:
I believe this is what you are looking for.
int main()
{
int fahr;
for (fahr = 320; fahr > 0; fahr = fahr - 20)
{
printf("%d %f \n", fahr,(5.0/9.0)*(fahr-32));
}
return 0;
}
I see two points: the condition to stop the for and the point after the *.
CodePudding user response:
Your fahr = 0
is the killer. It assigns 0 to fahr, sees it's false, and stops without iterating.
fahr == 0
would stop as desired but it would allow underruns if 0 is somehow skipped. Not a problem in this tiny fragment but it can be a life saver in larger and more complex loops where an extra decrement causes the value to skip 0 and happily march to negative infinity, which wraps back to positive and starts again. fahr <= 0
is what I would code.
CodePudding user response:
main
has return typeint
. Use it correctly- in the
for
loopfahr = 0
assigns zero tofahr
. You want logical operator there - probablyfahr != 0
*.(fahr - 32))
- it is invalid syntax in C.
#include <stdio.h>
int main(void) {
int fahr;
for (fahr = 320; fahr != 0; fahr = fahr - 20)
printf("%d %f \n", fahr, (5.0 / 9.0) *(fahr - 32));
}