Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 5th, 2013, 03:40 AM
 fgrubercpa
Registered User

Join Date: Jul 2013
Posts: 2
Time spent in forums: 12 m 59 sec
Reputation Power: 0
Beginner Q:)

I have what should be a rather simple beginning c program to convert celsius to Farenh. below.

the file will compile but won't 'build' for some reason. Any suggestions?
thx
_____________________________

#include <stdio.h>
/*Print Farenheit table*/
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;
}
}

#2
July 5th, 2013, 04:02 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,842
Time spent in forums: 3 Months 22 h 41 m 40 sec
Reputation Power: 2199
You left out the main function. Every C program requires a main function.

You should realize by now that C is case-sensitive, which means that if you change the case of any character in a variable, type, or function name, then that is a different identifier. Therefore, Main is something entirely different from main.

PS
This line,
celsius = 5 * (fahr-32) / 9;
will not do what you think it does. You are expecting it to perform floating-point division, but since both the dividend and divisor are integers it will perform integer division instead.

For example,
5.0/9.0 = 0.55555556
but
5/9 = 0

Changing your expression to dividing by 9.0 should be enough to invoke floating-point division. But then you'll be assigning a double to an int which will raise a warning about losing accuracy. You can truncate the result by casting it to int, or you could round off by adding 0.5 before you cast it to int.

Last edited by dwise1_aol : July 5th, 2013 at 04:09 AM.

#3
July 5th, 2013, 12:52 PM
 fgrubercpa
Registered User

Join Date: Jul 2013
Posts: 2
Time spent in forums: 12 m 59 sec
Reputation Power: 0
Cool,
thank you

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Beginner Q:)