How do I define string constants in C++?

C++ProgrammingObject Oriented Programming

To define a string constant in C++, you have to include the string header library, then create the string constant using this class and the const keyword.

Example

#include<iostream>
#include<string>
int main() {
const std::string MY_STRING = "Hello World!";
std::cout << MY_STRING;
return 0;
}

Output

This will give the output −

Hello World!

Note that if you try to reassign a value to this variable it'll cause an error.

raja
Published on 16-Feb-2018 14:17:09
Advertisements