The formula to convert Fahrenheit to Celsius in C is celsius = (fahrenheit - 32) * 5 / 9; where fahrenheit is a floating-point variable holding the input temperature. This page provides a working calculator that performs the conversion, and explains the math, the C code, and the common pitfalls when implementing it.
Formula
The conversion uses the linear relationship between the Fahrenheit and Celsius scales. The formula is:
C = (F − 32) × 5/9
- C – temperature in degrees Celsius (°C)
- F – temperature in degrees Fahrenheit (°F)
The constant 32 represents the offset: water freezes at 32 °F but at 0 °C. The factor 5/9 is the ratio of the scale sizes: a change of 9 °F equals a change of 5 °C. In C, because integer division truncates, you must use floating-point types (float or double) or write 5.0/9.0 to preserve the fractional part.
Worked Example
Using the default value of 98.6 °F (normal human body temperature):
- Subtract 32: 98.6 − 32 = 66.6
- Multiply by 5: 66.6 × 5 = 333.0
- Divide by 9: 333.0 / 9 = 37.0
So 98.6 °F equals 37.0 °C. The calculator above returns 37.00 °C with two decimal places, matching this result.
Assumptions
- The Fahrenheit input is a real number (can be negative).
- The formula is exact for all temperatures above absolute zero (−459.67 °F).
- No rounding is applied until the final output; intermediate values are kept in full floating-point precision.
Writing the C Code
A complete C program that reads a Fahrenheit value and prints Celsius looks like this:
#include <stdio.h>
int main(void) {
double fahrenheit, celsius;
printf("Enter temperature in Fahrenheit: ");
scanf("%lf", &fahrenheit);
celsius = (fahrenheit - 32) * 5.0 / 9.0;
printf("%.2f °F = %.2f °C\n", fahrenheit, celsius);
return 0;
}
Key points in the code:
- Use
double(orfloat) for both variables; usingintwill truncate the result to an integer, e.g., 98.6 would become 98 before conversion. - Write
5.0and9.0instead of5and9. In C,5 / 9is integer division and equals 0, making the whole expression zero. - The
%lfformat specifier reads a double;%fis for float. - Print with
%.2fto show two decimal places, which is conventional for temperature readings.
Common Mistakes
The most frequent error is integer division. If you write (f - 32) * 5 / 9 with all integer variables, C computes 5/9 as 0, so any input returns 0. Another mistake is reversing the formula, e.g., (F * 9/5) + 32, which is the Celsius-to-Fahrenheit conversion. Also, forgetting the parentheses around f - 32 changes the order of operations: f - 32 * 5 / 9 subtracts a small number from F instead of scaling the difference.
Rounding is another subtle point. The exact conversion of 100 °F is 37.777… °C. Printing with %.2f gives 37.78, but if you store the result in an integer variable, you get 37. For scientific work, keep the double and round only at display time.
Choosing Between float and double
In C, float uses 32 bits and double uses 64 bits. For temperature conversion, both are far more precise than needed: the difference between them appears only beyond the 7th significant digit. Use double by default because scanf with %lf and printf with %f handle it naturally, and it avoids any precision concerns if the program later chains multiple conversions. Use float only if you are writing for a microcontroller with limited memory, where saving 4 bytes per variable matters.
Reverse Conversion and Kelvin
The inverse formula, Celsius to Fahrenheit, is F = C × 9/5 + 32. Both formulas come from the same linear relation. For scientific contexts you may also need Kelvin: K = C + 273.15, so the direct Fahrenheit-to-Kelvin formula is K = (F − 32) × 5/9 + 273.15. In C, the same floating-point rules apply: use 5.0/9.0 and store results in a double.
Absolute zero is −459.67 °F, which equals 0 K. Any input below that is physically meaningless; the calculator above rejects such values. When writing your own program, you may add a check like if (fahrenheit < -459.67) and print an error message.
When to Use This Conversion
This conversion appears in weather applications, cooking recipes (oven temperatures are often given in °F in the US), and engineering data sheets. In C, the conversion is a one-line arithmetic expression, so there is no standard library function for it; you write the formula yourself. If you are processing many temperatures, put the conversion in a function:
double f_to_c(double f) {
return (f - 32) * 5.0 / 9.0;
}
This keeps the code readable and lets you reuse it without repeating the formula.
Frequently Asked Questions
Why does my C program return 0 for any Fahrenheit input?
Because you are using integer division. In C, 5 / 9 evaluates to 0 since both operands are integers. Change the literals to 5.0 / 9.0 or cast one operand to double: (double)5 / 9.
What is the difference between %f and %lf in printf?
In printf, %f and %lf behave identically for printing a double; %lf is only required in scanf to read a double. Using %f in scanf for a double variable causes undefined behavior.
Can I use the formula with integers if I only need whole degrees?
Yes, but you must do the division last and accept truncation. For example, (f - 32) * 5 / 9 with integer variables gives the floor of the result for positive values. For negative temperatures, C truncates toward zero, so −40 °F becomes 0 °C instead of the correct −40 °C. Use floating point unless you are certain inputs are positive.
Is there a built-in conversion function in C?
No. The C standard library does not include temperature conversion functions. You write the formula yourself, which is simple enough that a function is unnecessary.
How do I convert Fahrenheit to Kelvin directly in C?
Use kelvin = (fahrenheit - 32) * 5.0 / 9.0 + 273.15;. This combines the Fahrenheit-to-Celsius step with the Celsius-to-Kelvin offset.
Related
Formula used
in f | Temperature in Fahrenheit | °F | 98.6 | -459.67 out c | Temperature in Celsius | °C | (f - 32) * 5 / 9 | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation with the constants and conversion factors written on the page, rounded as each result states. See method and constants, the disclaimer or report an error.
More in this section
- ug to ppb Calculator: Convert Micrograms per Liter1.00 ppb
- Grams to Moles Chemistry: How to Convert0.2000 mol/L
- Convert ug/l to ppb Calculator
- CPP Calculator Ontario: How CPP Is Calculated162.12 $/month
- Grams to Moles to Molecules: How to Convert0.2000 mol/L
- Organic Chemistry Reaction Calculator: How It Works