Back to C Programming
2026-07-125 min read

Float Input (C Programming)

Learn Float Input (C Programming) step by step with clear examples and exercises.

Why This Matters

Float input is a crucial aspect of C programming that allows you to accept floating-point numbers from the user, which can be used for various mathematical calculations and data analysis tasks. In this lesson, we will explore how to take float input using the scanf() function and learn about common mistakes and best practices.

Why This Matters

In real-world programming scenarios, you often need to work with floating-point numbers that represent decimal values such as 3.14 or 0.0005. To read these numbers from the user, we use the scanf() function in C. Understanding how to take float input is essential for solving problems related to mathematics, physics, engineering, and other fields where decimal numbers are commonly used.

Prerequisites

Before diving into float input, you should have a good understanding of the following topics:

  • Variables and data types in C
  • Input/Output (I/O) using printf() and scanf() functions
  • Basic arithmetic operations with floating-point numbers

Core Concept

To take float input from the user, we use the scanf() function along with the format specifier %f. Here's a simple example:

#include <stdio.h>

int main() {
float num;

printf("Enter a floating-point number: ");
scanf("%f", &num);

printf("You entered: %.2f\n", num);

return 0;
}

In this example, we first declare a variable num of type float. The printf() function is used to display a message asking the user for input. The scanf("%f", &num) line reads the floating-point number entered by the user and stores it in the num variable. Finally, we print the input using another printf() statement with the format specifier %.2f, which rounds the number to two decimal places.

Common Mistakes

  1. Forgetting to declare the variable: If you forget to declare the variable before taking input, you will get a compiler error.
// Incorrect code
scanf("%f", num); // Compiler error: 'num' undeclared (first use in this function)
  1. Not using & with the variable: When using the scanf() function, you must pass the address of the variable to it by using the address-of operator (&).
// Incorrect code
scanf("%f", num); // Compiler error: lvalue required as unary operand
  1. Using the wrong format specifier: If you use the incorrect format specifier, such as %d for a floating-point number, you will get unexpected results or compiler errors.
// Incorrect code
scanf("%d", &num); // Compiler error: conversion specifies type 'int' but the argument has type 'float'
  1. Not checking for input errors: It is a good practice to check if the input was entered correctly and handle any potential errors. For example, you can check if the user enters non-numeric input or presses Ctrl+C.

Worked Example

Let's create a program that takes two floating-point numbers as input, calculates their sum, and displays the result:

#include <stdio.h>

int main() {
float num1, num2, sum;

printf("Enter the first floating-point number: ");
scanf("%f", &num1);

printf("Enter the second floating-point number: ");
scanf("%f", &num2);

sum = num1 + num2;

printf("The sum of %.2f and %.2f is %.2f\n", num1, num2, sum);

return 0;
}

In this example, we take two floating-point numbers as input using the scanf() function. We then calculate their sum and display the result with three decimal places using the format specifier %.3f.

Common Mistakes

  1. Not checking for input errors: If the user enters non-numeric input or presses Ctrl+C, the program will crash or produce incorrect results. To handle these cases, you can use a loop to prompt the user for correct input until they provide valid numbers.
  1. Incorrect rounding: When displaying the result, make sure to use the appropriate format specifier to get the desired number of decimal places. For example, if you want four decimal places, use %.4f.

Practice Questions

  1. Write a program that takes three floating-point numbers as input and calculates their average. Display the result with two decimal places.
  2. Modify the worked example to handle cases where the user enters non-numeric input or presses Ctrl+C.
  3. Create a program that reads a floating-point number from the user and checks if it is greater than 10. If it is, display "The number is greater than 10." Otherwise, display "The number is less than or equal to 10."
  4. Write a program that calculates the area of a circle given its radius (a floating-point number). Use printf() to display the result with two decimal places.
  5. Modify the worked example to calculate and display the difference between the larger and smaller input numbers.

FAQ

Q: Why do I get unexpected results when taking float input?

A: Make sure you are using the correct format specifier (%f) for floating-point numbers, and don't forget to pass the address of the variable using &. Also, check if the user enters non-numeric input or presses Ctrl+C.

Q: How can I handle cases where the user enters non-numeric input or presses Ctrl+C?

A: You can use a loop to prompt the user for correct input until they provide valid numbers. Make sure to check for these cases and display an error message if necessary.

Q: Why do I need to declare variables before taking input using scanf()?

A: When you declare a variable, you create memory space for it. Without declaring the variable, there is no memory allocated for it, so you cannot store the input in that variable.

Q: How can I display floating-point numbers with a specific number of decimal places using printf()?

A: You can use the format specifier %.nf, where n is the desired number of decimal places. For example, to display a floating-point number with two decimal places, use %.2f.