C Decimal (Floating-Point) Numbers
In this page:
float variable_name = decimal_valuef;
double variable_name = decimal_value;
What are Decimal (Floating-Point) Numbers?
Decimal numbers, also called floating-point numbers, represent values with a fractional part and require a type like float or double, since int and other integer types can only store whole numbers.
Example: What are Decimal (Floating-Point) Numbers?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare price and set it to 9.99f
float price = 9.99f;
// Print formatted text to the screen
printf("%.2f", price);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
float Precision
float is a single-precision floating-point type that typically stores about 6 to 7 significant decimal digits accurately, using 4 bytes of memory, making it more compact but less precise than double.
Example: float Precision
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare f and set it to 1.0f / 3.0f
float f = 1.0f / 3.0f;
// Print formatted text to the screen
printf("%.7f", f);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
double Precision
double is a double-precision floating-point type that typically stores about 15 to 16 significant decimal digits, using 8 bytes of memory, and is the default choice for decimal literals and most floating-point math in C.
Example: double Precision
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare d and set it to 1.0 / 3.0
double d = 1.0 / 3.0;
// Print formatted text to the screen
printf("%.15f", d);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Formatting Decimal Output
printf's %f specifier can be customized with a precision, like %.2f for exactly two decimal places, and a field width to control how the formatted decimal number is aligned and padded in the output.
Example: Formatting Decimal Output
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare value and set it to 3.14159
double value = 3.14159;
// Print formatted text to the screen
printf("%.2f", value);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Comparing Floating-Point Numbers
Because floating-point numbers are stored in binary and can't represent every decimal value exactly, comparing two floating-point values for exact equality is unreliable, and checking whether their difference is smaller than a small tolerance is the standard approach instead.
Example: Comparing Floating-Point Numbers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
// Declare a and set it to 0.1 + 0.2
double a = 0.1 + 0.2;
// Declare b and set it to 0.3
double b = 0.3;
// Check whether fabs(a - b) < 1e-9
if (fabs(a - b) < 1e-9) {
// Print formatted text to the screen
printf("Equal enough");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Comparing floats with
==, such as0.1 + 0.2 == 0.3, which is false because of rounding. - Using
%fwithscanffor adouble, whenscanfneeds%lffor doubles. - Expecting a
floatto hold many digits exactly, when it only keeps about 6 to 7 significant digits.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers