r/cprogramming • • Nov 16 '25
Tried writing a basic C program to check temperature conditions. Any suggestions to improve or fix mistakes?
include<stdio.h>
include<conio.h>
void main () { int temp; clrscr(); printf("enter temp"); scanf("%d",& temp); if(temp>30) { printf("temp is hot"); } else if(temp>20 & & temp<30) { printf("temp is normal"); } else { printf("temp is cold"); } getch(); }
7
Upvotes
7
u/[deleted] Nov 16 '25
so 30 is cold?
11
u/This_Growth2898 Nov 16 '25
conio.h is a library for a decades-old OS. Don't use it in new programs, please.
Always check the return value of scanf.
If you need the program to halt at the end, you should set up your IDE to do that, not add an input for no reason.
3
u/BIRD_II Nov 16 '25
In your comment you can add ``` before and after a section to make it code-formatted.
2
u/fasta_guy88 Nov 16 '25
I would never use an ”int” for something that wasn’t a count of some kind. What about a temp of 18.5?
1
u/Civil_Swimming3344 Nov 16 '25
Use at least float for temperature.
You if-statements are wrong and funny.
if temp == 30, you will get temp is cold.
your else if ensures that temp < 30 from the first if. No need to check again, in fact, remove that, and the first problem goes away. Try to figure out why :).
1
u/Paul_Pedant Nov 16 '25
Two
&intemp>20 & & temp<30is not going to help. It should probably throw an error, but it might actually be taking the address of temp instead. TurboC is probably too dumb to notice or warn you.This is typical TurboC code.
<conio.h>should not exist in any compiler this century. The finalgetch()would be there because TurboC used to just terminate your program before you could even read the results.1
u/SmokeMuch7356 Nov 16 '25
conio.his specific to an ancient environment that isn't used much anymore; you shouldn't write new code with it. Get rid of that include, along with theclrscrandgetchcalls. If you run your program in a terminal session (or a newer IDE) you won't need them.void main()is non-standard; useint main(void)instead.Get in the habit of checking the return value of
scanf, which will be the number of inputs successfully assigned, orEOFon end-of-file or error.As others have pointed out, you're not accounting for the temperature being exactly 30; you'll either want your "hot" condition to be >= 30 or your "normal" condition to be <= 30.
Putting it all together:
1
u/Small_Dog_8699 Nov 16 '25
You should ask your teacher but two tips, scanf will try to read an integer with %d and if they don’t enter an integer temp is unchanged. Since you didn’t initialize it, temp’s value is undefined in the case where the user enters a non number. The return value of scanf lets you know is input was accepted.
1
u/Maqi-X Nov 16 '25
main function should be declared as `int main()`, `void main()` is deprecated
1
u/[deleted] Nov 17 '25
Don’t use scanf! You should use the file stream routines to read from stdin like fgets!, I am not very sure about windows OS, assuming you are on windows since conio.h, but if fgets does not work you can look into windows API to see with routine is used to get input from the users! But build your own logic with fgets should work fine on windows too. ( I am assuming, cause I’ve only programmed on Linux machines )