Converting Fahrenheit & Celsius: A C Programming Guide

In summary, the conversation is about writing a C program to convert between fahrenheit and celsius degrees. The expert suggests using floating point numbers instead of integers and provides advice on how to correctly use division in C. They also mention potential issues with the compiler and suggest resolving them before proceeding with the program.
  • #1
queenspublic
59
0
Okay. I need to write a C program to convert between fahrenheit and celsius degrees.
 
Last edited:
Technology news on Phys.org
  • #2
Have you asked the computer?
Off the top of my head;
The main definition is wrong - it might compile but it's wrong.
You probably want to use floating point numbers rather than integers
Your tests for 'c' and 'f' don't do what you think they do
 
  • #3
What is the indication leading you to believe there is somethig wrong with your compiler? Also, which compiler product are you using?
 
  • #4
The line beginning with "printf("%d degrees celsius" is missing a semicolon.

You should not say something like 5 / 9 in C. Division in an integer expression will round down, such that 5 / 9 = 0. Say something more like 5.0 / 9.0, or promote "degrees" to a float, so that the compiler knows to use floating point math.

Also maybe some compilers will complain that there is no return type for main().

You should try to get your compiler working. As potential asks, what do you mean "something is wrong with it"?
 

Related to Converting Fahrenheit & Celsius: A C Programming Guide

1. How do I convert Fahrenheit to Celsius in C?

To convert Fahrenheit to Celsius in C, you can use the formula C = (F-32) * 5/9, where C is the temperature in Celsius and F is the temperature in Fahrenheit. This formula can be implemented in a C program using basic arithmetic operations.

2. How do I convert Celsius to Fahrenheit in C?

To convert Celsius to Fahrenheit in C, you can use the formula F = (C * 9/5) + 32, where F is the temperature in Fahrenheit and C is the temperature in Celsius. This formula can be implemented in a C program using basic arithmetic operations.

3. Can I use a function to convert temperature from Fahrenheit to Celsius in C?

Yes, you can use a function in C to convert temperature from Fahrenheit to Celsius. You can define a function that takes in the temperature in Fahrenheit as a parameter and returns the temperature in Celsius using the conversion formula.

4. How do I handle non-numeric input when converting temperature in C?

You can use error handling techniques in C to handle non-numeric input when converting temperature. For example, you can use the scanf() function to read user input and check if the input is a valid number before performing the conversion.

5. Is there a library function in C for temperature conversion?

Yes, the math.h library in C provides the convertToFahrenheit() and convertToCelsius() functions for temperature conversion. These functions take in a temperature value as a parameter and return the converted temperature in either Fahrenheit or Celsius.

Similar threads

Replies
11
Views
3K
  • Programming and Computer Science
Replies
18
Views
10K
  • General Engineering
2
Replies
39
Views
4K
Replies
42
Views
3K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
788
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top