Skip to main content
Engineering LibreTexts

4.4.4: Literals and Constants - Strings and Booleans

  • Page ID
    29035
  • \( \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}}\)

    1. String Literals: String literals are similar to that of the character literals, except that it can store multiple characters and uses a double quote to store the same. It can also accommodate the special characters and escape sequences mentioned in the table above.

      Example:

      // For C++
      string stringVal = "GeeksforGeeks"
      

      Example:

      #include <iostream>
      using namespace std;
        
      int main()
      {
          const string str = "Welcome\nTo\nGeeks\tFor\tGeeks";
          cout << str;
          return 0;
      } 
    2. Output:
      Welcome
      To
      Geeks    For    Geeks
      
    3. Boolean Literals: This literal is provided only in C++ and not in C. They are used to represent the boolean datatypes. These can carry two values:
      • true: To represent True value. This must not be considered equal to int 1.
      • false: To represent False value. This must not be considered equal to int 0.

    Example:

    #include <iostream>
    using namespace std;
      
    int main()
    {
        const bool isTrue = true;
        const bool isFalse = false;
      
        cout << "isTrue? "
             << isTrue << "\n";
        cout << "isFalse? "
             << isFalse << "\n";
      
        return 0;
    } 

    Output:

    isTrue? 1
    isFalse? 0
    

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


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

    • Was this article helpful?