Skip to main content
Engineering LibreTexts

4.4.2: Literals and Constants - Floating Point

  • Page ID
    29033
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)

    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


    4.4.2: Literals and Constants - Floating Point is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.