C-style string literals are hard to deal with so C++ has it’s own string data type. C++ strings have several features than C-style strings:

  1. supports dynamic memory allocation. Which means we can assign any size of string to a string variable.
  2. reassign value to a string variable.

We can create a string as follows:

#include <iostream>
#include <string>
#include "../helpers/stdout.h"
 
int main(int argc, char const *argv[])
{
    std::string fullName{"Nitin sharma"};
    print(fullName);
    
    return 0;
}

Header string requires to use std::string type.

Note

Initialization of string requires copy of the string. Copy of strings are expensive, so care should be taken.

std::string cannot be declared using constexpr as it cannot be used in constant expressions.

#include <iostream>
#include <string>
#include "../helpers/stdout.h"
 
int main(int argc, char const *argv[])
{
    constexpr std::string name{"Nitin"};
 
    print(name);
    return 0;
}

Compiling this program would throw an error.

References

  1. https://www.learncpp.com/cpp-tutorial/introduction-to-stdstring/