Skip to main content
Engineering LibreTexts

4.4.2: Literals and Constants - Floating Point

  • Page ID
    29033
  • Floating-Point Literals

     These are used to represent and store real numbers. The real number has an integer part, real part, fractional part and an exponential part. The floating-point literals can be stored either in decimal form or exponential form. While representing the floating-point decimals one must keep two things in mind to produce valid literals:

    • In the decimal form, one must include the decimal point, exponent part or both, otherwise, it will lead to an error.
    • In the exponential form, one must include the integer part, fractional part or both, otherwise, it will lead to an error.

    Few floating-point literal representations are shown below:

    Valid Floating Literals:

    10.125
    1.215-10L
    10.5E-3
    

    Invalid Floating Literals:

    123E
    1250f
    0.e879
    

    Example:

    #include <iostream> 
    using namespace std; 
    
    int main() 
    {   // Real literal 
        const float floatVal = 4.14; 
    
        cout << "Floating-point literal: " << floatVal << "\n"; 
        return 0; 
    }  

     Output:

    Floating point literal: 4.14
    

     Adapted from:
    "Types of Literals in C/C++ with Examples" by Chinmoy LenkaGeeks for Geeks