0

My code in c is:

#include <stdio.h>

main()
{
    int fahr, celsius;
    int lower, upper, step;

    lower = 0;  
    upper = 300;    
    step = 20;  

    fahr = lower;
    while (fahr <= upper); {
        celsius = 5 * (fahr - 32) / 9;
        printf("%d\t%d\n", fahr, celsius);
        fahr = fahr + step;
    }
}

I have compiled it with gcc without any errors and am trying to execute it with windows prompt. However, it keeps thinking and doesn't show anything. What is the problem?

3 Answers3

1

Remove the semicolon after the while statement.

Like this, the while loop is an infinite loop, because the code inside the curly brackets is not considered to be the loop body. The while loop has an empty body and will never terminate.

The code in the curly brackets will never be executed.

M. Spiller
  • 2,325
  • 1
  • 6
  • 15
  • It's worth noting that GCC will warn you about this issue if you enable warnings (-Wall for most of them, but this specific issue is caught by -Wmisleading-indentation). You should pretty much always pass -Wall, especially while you're learning. – nemequ Mar 23 '19 at 14:47
0

Your while loop as written doesn’t do what you intend it to. The mistake is on this line:

while (fahr <= upper); {

The semicolon here prematurely terminates the while statement. This creates an infinite loop, because your program is now continually evaluating fahr <= upper without modifying the value of fahr.

Delete that semicolon and your program will execute as expected.

Joe
  • 219
  • 2
  • 7
0

Change

    while (fahr <= upper); {

To

    while (fahr <= upper){

Because of the semicolon after while loop the while loop is not going into the { } portion. So the value of fahr & upper is never changing and the while loop is running forever. Remove the semicolon, your code will work well.

Niloy Rashid
  • 545
  • 2
  • 12