Skip to main content
Engineering LibreTexts

4.4.4: Literals and Constants - Strings and Booleans

  • Page ID
    29035
    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