C Program to Convert Fahrenheit to Celsius
C Program to Convert Fahrenheit to Celsius
A school exercise asks you to fahrenheit to celsius. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Convert Fahrenheit to Celsius by subtracting the offset 32 and multiplying by the floating-point ratio 5/9.
Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.
Explain
Explain why fahrenheit to celsius uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Check the freezing-point input 32 Fahrenheit.
Fahrenheit to Celsius: follow the data
Given data
A school exercise asks you to fahrenheit to celsius. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Rule
Convert Fahrenheit to Celsius by subtracting the offset 32 and multiplying by the floating-point ratio 5/9.
Observed result
Celsius: 30.0
Understand the operation
A science class compares temperature scales. Their zero points differ, so scaling before subtracting the offset is the wrong formula.
Read the declarations and operands before the output call. %d formats an int; floating-point results use an appropriate precision with %f. A format describes a value that was already computed—it does not choose the earlier arithmetic.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
double fahrenheit = 86.0;
double celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
printf("Celsius: %.1f\n", celsius);
return 0;
}
Expected output
Celsius: 30.0
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
double fahrenheit = 86.0;
double celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
printf("Celsius: %.1f\n", celsius);
return 0;
}
86 - 32 produces 54.
54 × 5 / 9 produces 30 degrees Celsius.
Follow the worked calculation
Start with the sample
86 - 32 produces 54.
Apply the rule
54 × 5 / 9 produces 30 degrees Celsius.
Check the result
Celsius: 30.0
Celsius: 30.0
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
double fahrenheit = 86.0;
double celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
printf("Celsius: %.1f\n", celsius);
return 0;
}
Check the freezing-point input 32 Fahrenheit.
Worked answer
The worked answer follows the same rule with the requested changed data. Compare its complete source and output: Celsius: 0.0. The result should follow from the operations, rather than from memorizing the first sample.
C17 · complete program
#include <stdio.h>
int main(void) {
double fahrenheit = 32.0;
double celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
printf("Celsius: %.1f\n", celsius);
return 0;
}
Expected output
Celsius: 0.0
Run in C compiler →Recognize the important boundaries
Writing 5 / 9 with integer operands produces zero before multiplication. Use floating-point constants.
The demonstrated values stay within the types’ representable ranges. A program accepting arbitrary external values needs checked conversion and bounds before performing arithmetic.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Convert Fahrenheit to Celsius by subtracting the offset 32 and multiplying by the floating-point ratio 5/9.
Continue with arithmetic operand types.
Continue with validated input.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.