Floating and Boolean Data Types
Last Updated : Jan 2026
In the previous lesson, we learned about integers:
int age = 20;
But many real-world values contain a fractional part:
99.5
36.6
3.14159
0.75
These are floating-point values. C mainly provides:
float (%f)
double (%lf)
long double (%Lf)
Why Can't We Just Use int?
Consider:
int price = 99.99;
The value contains a fractional part, but int represents integers.The conversion to int discards the fractional part in this case:
99.99
↓
99
So if you need fractional values, use an appropriate floating-point type. For example:
float temperature = 36.6f;
float price = 99.5f;
float percentage = 87.5f;
The f suffix tells C that the literal is a float rather than the default double type for a decimal floating literal. Compare:
float a = 10.5f;
double b = 10.5;
double price = 99.99;
double pi = 3.141592653589793;
A double normally provides more precision than a float. For many general-purpose programs, double is the better default when you need floating-point calculations.
C also provides a third type of floating-point number:
long double
Example:
long double value = 3.141592653589793238L;
It provides at least as much precision/range as double, although the exact representation and precision are implementation-dependent. On some systems it provides extra precision; on others, it may behave similarly to double.
Conceptually:
float
↓
less precision
double
↓
more precision
long double
↓
at least as much precision/range as double
But don't interpret this as exact byte sizes or precision values on every system. "Precision" Mean :
Suppose we want to represent:
3.141592653589793
A floating-point type has a finite number of bits available to represent the value. Therefore, it cannot represent every possible real number exactly. Precision refers roughly to how many significant digits can be represented accurately. For example,
float generally has less precision than double.
Examples of Floating Data Types
#include <stdio.h>
int main() {
float temperature = 36.6f;
double price = 99.5;
long double pi = 3.141592653589793238L;
printf("Float: %f\n", temperature);
printf("Double: %lf\n", price);
printf("Long Double: %Lf\n", pi);
// Sub-specifier
printf("Float: %.1f\n", temperature);
printf("Double: %.1lf\n", price);
printf("Long Double: %.2Lf\n", pi);
return 0;
}
Output:
Float: 36.599998
Double: 99.500000
Long Double: 3.141593
Float: 36.6
Double: 99.5
Long Double: 3.14
typedef
The typedef keyword in C provides a mechanism to create user-defined type aliases (nicknames) for existing data types. It does not introduce new data types; rather, it creates new labels for existing ones to enhance code readability, maintainability, and portability across different compiler platforms.
typedef existing_type AliasName
#include <stdio.h>
// Creating shorthand aliases for primitive types
typedef unsigned long long int uint64;
typedef float byte_percentage;
int main(void) {
uint64 total_bytes = 18446744073709551615ULL;
byte_percentage memory_usage = 87.5f;
printf("Bytes: %llu\n", total_bytes);
printf("Usage: %.1f%%\n", memory_usage);
return 0;
}
Boolean Data Type
Bool is a special data type that is used to represent boolean values in C. It is a data type that has two possible values: true (1) and false (0).
#include <stdio.h>
int main() {
_Bool flag = 1;
_Bool status = 42; // Stored as 1
_Bool is_even = -5; // Stored as 1
_Bool is_odd = 0; // Stored as 0
printf("flag: %d\n", flag);
printf("status: %d\n", status);
printf("is_even: %d\n", is_even);
printf("is_odd: %d\n", is_odd);
return 0;
}
Output:
flag: 1
status: 1
is_even: 1
is_odd: 0
