C Program to Convert Celsius to Fahrenheit

C Program to Convert Celsius to Fahrenheit

Temperature conversion is a common task in programming and real-life applications. Converting Celsius to Fahrenheit helps beginners understand how to work with variables, mathematical operations, and formatted output in C. In this tutorial, we will write a complete C program to convert a temperature entered in Celsius to Fahrenheit. We will explain each part of the code in detail and discuss common mistakes beginners make. By the end of this post, you will know how to perform simple arithmetic operations and display results in C.

The Celsius to Fahrenheit conversion formula is:

Fahrenheit = (Celsius × 9/5) + 32

This simple formula provides an excellent opportunity for beginners to learn variable handling, arithmetic operations, and how to take input from the user.

C Program to Convert Celsius to Fahrenheit

We will start by writing a simple program that asks the user to enter a temperature in Celsius and then displays the corresponding Fahrenheit value.

#include <stdio.h>

int main() {

    float celsius, fahrenheit;

    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);

    fahrenheit = (celsius * 9 / 5) + 32;

    printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);

    return 0;

}

In this program, we declare two variables celsius and fahrenheit of type float. This allows us to work with decimal numbers, which are common in temperature calculations. Using printf(), we prompt the user to enter the temperature in Celsius. The scanf() function reads the input and stores it in the celsius variable. We then calculate the Fahrenheit value using the formula (celsius * 9 / 5) + 32 and store it in fahrenheit. Finally, printf() displays both the original Celsius value and the converted Fahrenheit value, formatted to two decimal places for clarity.

Using a Function for Conversion

For better structure and reusability, we can create a separate function to convert Celsius to Fahrenheit. This approach is useful when the conversion needs to be done multiple times in a program.

#include <stdio.h>

float celsiusToFahrenheit(float c) {
    return (c * 9 / 5) + 32;
}

int main() {

    float celsius;

    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);

    float fahrenheit = celsiusToFahrenheit(celsius);

    printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);

    return 0;

}

Here, we define a function celsiusToFahrenheit() that takes a float as input and returns the converted Fahrenheit value. Inside main(), we call this function with the user-provided Celsius temperature. Using functions makes the program cleaner, easier to read, and more modular. It also demonstrates how to pass arguments to a function and return a value.

Common Beginner Mistakes

When converting temperatures in C, beginners often make these mistakes:

  • Using int variables for calculations. Integer division truncates decimals, which can lead to inaccurate results. For example, celsius * 9 / 5 with int variables will drop the fractional part. Always use float or double for decimal values.
  • Implementing the wrong formula. For instance, forgetting to add 32 after multiplying by 9/5 will produce incorrect Fahrenheit values.

To avoid these issues, use the appropriate data type for decimal calculations and double-check the conversion formula.

FAQs

Q1: Can this program handle negative Celsius values?
Yes, the program works for both positive and negative temperatures. For example, -40°C converts correctly to -40°F, which is a unique point where Celsius equals Fahrenheit.

Q2: How can I round the Fahrenheit value to the nearest integer?
You can use the round() function from math.h to round the result. For example: fahrenheit = round((celsius * 9 / 5) + 32);

Q3: Can we extend this program to also convert Fahrenheit to Celsius?
Yes, you can write another function using the formula Celsius = (Fahrenheit - 32) * 5 / 9 and allow the user to choose the conversion direction.

Conclusion

Converting Celsius to Fahrenheit is a simple yet practical exercise in C programming. It teaches you how to take input, perform arithmetic operations, use functions, and display formatted output. By practicing this program, you will gain confidence in handling variables, mathematical expressions, and modular programming in C. Try modifying the program to handle multiple temperatures or reverse conversion to enhance your skills.

References & Additional Resources

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
  2. GeeksforGeeks: Celsius to Fahrenheit in C – Examples and explanations.
  3. Programiz: C Functions – Learn how to use and create functions in C.
  4. TutorialsPoint: Input and Output Functions in C – Basics of scanf() and printf().
Scroll to Top