Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C simple program doesn't work

Tags:

c

gcc

Hello everyone! I'm trying to learn C language and have this trouble:

Example code from book works as it should work:

#include <stdio.h>
/* печать таблицы температур по Фаренгейту
и Цельсию для fahr = 0, 20, ..., 300 */

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;
    }
}

Output:

0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148

But code that I've written doesn't do anything!:

#include <stdio.h>

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

    printf("Enter lower temperature:");
    scanf("%d", &lower);

    printf("Enter upper limit:");
    scanf("%d", &lower);

    printf("Enter step:");
    scanf("%d", &lower);

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

Output:

$ gcc fahr.c -o fahr.out
$ ./fahr.out
Enter lower temperature:0
Enter upper limit:300
Enter step:20
$

What's wrong?

like image 370
vil Avatar asked Jul 07 '13 14:07

vil


People also ask

Why my C program is not running in VS code?

Go to the menu Code > Preferences > Settings. In the User tab on the left panel, expand the Extensions section. Find and select Run Code Configuration. Find and check the box Run in Terminal.

Why is my program not running?

One reason programs do not open is because their files are corrupted. This can be difficult to pinpoint, but one way is to use the System File Checker to look for missing or corrupted information. Open the command prompt by typing Windows + R and then cmd. Hit Enter.

Why my Hello World program is not working?

It's only fitting that the first thing it says is "Hello, world!" There can be syntax error in your program like missing header file, termination syntax error etc. So catch these errors and remove them. Then your program will work.

Can I run C program in Sublime Text 3?

Lots and lots of features that pretty much work out of the box, open source, just install and use. How can I run C program in Sublime Editor Text 3 in Linux? You can build/compile in Sublime Editor, but you can't run it on Sublime Editor. You can run it through terminal.


2 Answers

Change this part ->

 printf("Enter upper limit:");
    scanf("%d", &lower);

    printf("Enter step:");
    scanf("%d", &lower);

to

 printf("Enter upper limit:");
    scanf("%d", &upper);

    printf("Enter step:");
    scanf("%d", &step);
like image 183
Luka Pivk Avatar answered Sep 21 '22 23:09

Luka Pivk


All of your scanfs read the variable lower.

scanf("%d", &lower);

This is very typical when you copy and paste sections of code. Welcome to C we hope you grow to love it as much as the rest of us.

like image 27
ojblass Avatar answered Sep 17 '22 23:09

ojblass