Floating-Point Literals

The Floating-point literals in C++ are constants that represent numbers with the fractional or decimal part. These can be either single-precision or double-precision values.

Like integer literals, the floating point literals can also be defined as double or float by using suffix (no suffix) and f respectively. By default, all the fractional numbers are considered to be of type double by C++ compiler.

Example

double floatLiteral = 3.14;

Implementation

C++




// C++ program to illustrate the floating point literals
#include <iostream>
using namespace std;
 
int main()
{
    // float literal
    float e = 2.7f;
 
    // double literal
    double pi = 3.14;
 
    // long double literal
    long double g = 9.8L;
 
    cout << "The Value of pi: " << pi << endl;
    cout << "The value of e: " << e << endl;
    cout << "The value of g: " << g << endl;
    return 0;
}


Output

The Value of pi: 3.14
The value of e: 2.7
The value of g: 9.8

Literals In C++

In C++ programming language, we use literals to represent fixed values. C++ supports various types of literals including integer literals, floating-point literals, character literals, and string literals. In this article, we will discuss all the necessary information on C++ literals and their usage.

Similar Reads

What are Literals in C++?

Literals are fundamental elements used to represent constant values used in C++ programming language. These constants can include numbers, characters, strings, and more. Understanding and using the literals is essential in C++ for data assignment, calculations, and data representation. They are generally present as the right operand in the assignment operation....

Types of Literals in C++

There are five types of literals in C++ which are:...

1. Integer Literals

Integer literals in C++ are constants that represent whole numbers without the fractional or decimal part. They can be positive or negative and can have different bases such as decimal, octal, or hexadecimal....

2. Floating-Point Literals

...

3. Character Literals

The Floating-point literals in C++ are constants that represent numbers with the fractional or decimal part. These can be either single-precision or double-precision values....

4. String Literals

...

5. Boolean Literals

The Character literals in C++ are constants that represent individual characters or escape sequences. They are enclosed in single quotes and you can use them to represent the single character....

Contact Us