Home > Back-end >  C program does not have output [closed]
C program does not have output [closed]

Time:10-02

[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:

  1. main has return type int. Use it correctly
  2. in the for loop fahr = 0 assigns zero to fahr. You want logical operator there - probably fahr != 0
  3. *.(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));
}
  •  Tags:  
  • c
  • Related