
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Define String Constants in C++
In C++, a string constant also called a string literal is a fixed sequence of characters enclosed in double quotes (" "). For example: "This is a string" and it is used to read-only memory, and its value cannot be changed during the program.
Defining C++ Strings Constants
You can define your own named string constants by using:
- String Literals
- The const keyword
- The #define preprocessor directive
- The constexpr keyword(in modern C++)
Let us understand each type of String Constant in C++ by giving examples step by step:
Defining String Constants Using String Literal
A string constant using a string literal is the way to directly use the text (inside double quotes) as a constant value in your program.
It is stored in read-only memory and you cannot change it during program execution.
Syntax
Following is the syntax for string constants using string literal:
"This is a string"
Example
This program prints a string constant(string literal) directly as the output by using "cout":
#include<iostream> using namespace std; int main() { cout<<"This is a string constant!"<<endl; return 0; }
Following is the output to the above program:
This is a string constant!
Defining String Constants Using const Keyword
The const keyword is the constant where the value cannot be changed after it is set. When we use strings, it helps to create string constants(fixed strings). So, we can say that it can store a string in variable and make it unchangeable using the const keyword.
There are two common ways to do this:
- Using C-style strings: const char*
- Using C++ strings: const std::string
Syntax
Following is the syntax for string constants using Const Keyword:
const char* greeting = "Hello"; const std::string message = "World!";
Example 1: Using C-style strings: const char*
This program stores a string constant in a 'const char*' pointer and prints the output.
#include<iostream> using namespace std; int main() { const char* greeting = "Hello, World!"; cout<<greeting<<endl; return 0; }
Following is the output to the above program:
Hello, World!
Example 2: Using C++ strings: const std::string
In this program, you can creates a constant string named 'name' with the value "Revathi" and then prints a greeting message using that name.
#include<iostream> #include<string> using namespace std; int main() { const string name = "Revathi"; cout<<"Hello, "<<name<< "!"<<endl; return 0; }
Following is the output to the above program:
Hello, Revathi!
Defining String Constants Using #define Preprocessor Directive
#define is a preprocessor directive that tells the compiler to replace a word with a fixed value before the program runs. This is used to define constant values by including string constants.
When you want to use #define to give a name to a string literal, it becomes a string constant.
Syntax Using #define preprocessor
Following is the syntax for string constants using #define preprocessor:
#define GREETING "Good Morning"
Example
This program uses #define preprocessor to create a string constant with the value and result the output.
#include<iostream> #define GREETING "Good Morning!" using namespace std; int main() { cout<<GREETING<<endl; return 0; }
Following is the output to the above program:
Good Morning!
Defining String Constants Using constexpr (C++11 and later)
The constexpr stands for "constant expression", It is a value that is guaranteed to be known at compile time(before the program runs).
A string constant using constexpr is a string (usually a pointer to a string literal) that is set at compile time and cannot be changed during the program.
Syntax
Following is the syntax for string constants Using constexpr:
constexpr const char* greeting = "Hi!";
Example
This program defines a compile-time string constant called appName using constexpr, sets it to "MyApp" and prints the message:
#include<iostream> using namespace std; int main() { constexpr const char* appName = "MyApp"; cout<<"Launching"<<appName<< "..."<<endl; return 0; }
Following is the output to the above program:
LaunchingMyApp...