
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How do I define string constants in C++?
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.
- Related Articles
- How do I write constants names in Java?
- How to define constants in C++?
- How to define integer constants in JavaScript?
- How to define character constants in C#?
- What is a constant and how to define constants in Java?
- Difference between C++ string constants and character constants
- How do I do a case insensitive string comparison in Python?
- How do I compare String and Boolean in JavaScript?
- How do I determine if a String contains another String in Java?
- How do I modify a string in place in Python?
- Character constants vs String literals in C#
- How do I get the average string length in MySQL?
- How do I wrap a string in a file in Python?
- How do I make my string comparison case insensitive?
- How do I convert a double into a string in C++?

Advertisements