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?
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.
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.
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With