Properties of string literals
1. A string literal is an array of characters. But, instead of delimiting it with curly braces {}, it is delimited by double-quotes “”. Also, they are not separated by commas(,).
Even if integers are included in between, they are taken as integers. For example, in “C1”, 1 is not an integer, it is a character like this: ‘N’.
2. In C++, it can be declared as following:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char example[]="Coding Ninjas";
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Since it is sort of an array of characters, square brackets [] have been used. In our case we have left it blank because we defined it while we declared it. In case you want to specify the size of the array, it should be 1 more than the number of characters. You will understand the reason behind this through point 4.
For example:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char example[]={'C','o','d','i','n','g'};
}
You can also try this code with Online C++ Compiler
Run Code
Here, ‘example’ is the character array but is not a string literal as it is delimited by curly braces{} and each character is separated by commas.
3. A string literal is a constant pointer to the start of the sequence of the characters similar to the fact that an array is a constant pointer to the start of a particular data type sequence.
The first element's memory address in the literal is the value of the literal identifier, and can never be altered.
The figure given below will make it more clear.
Therefore, a string literal can be considered as an object.
4. The compiler automatically appends a null character ‘\0’ at the end of the string literal, if not already done explicitly. Compiler assumes the end of the string when it encounters the null character. This also makes the actual size of the string one more than the number of characters in it.
For example:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char example[]="Coding Ninjas";
cout<<"Length of string literal "<<strlen(example)<<"\n";
cout<<"Size of the array "<<sizeof(example)/sizeof(example[0]);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
It has a length of 13. But the size of the array ‘example’ is 14, because of the insertion of null character at the end.
Also, in case you insert a null character in the middle of the string, the compiler will assume the dn of the string there itself, although it will store the rest of the characters in the memory if they appear after the null character.
For example:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char example[]="Coding\0Ninjas";
cout<<"Length of string literal "<<strlen(example)<<"\n";
cout<<"Size of the array "<<sizeof(example)/sizeof(example[0]);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Try and compile with online c++ compiler.
Here, although for the compiler the string ends after “Coding”, the rest of the characters are stored in the memory.
5. Re-assignment of the whole string is not allowed in string literal. Strings are allocated read-only memory. Although, individual characters can be changed.
For example:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char example[]="Coding Ninjas";
example="Ninjas Coding";
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
For example:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char example[]="Coding Ninjas";
example[6]='_';
cout<<example;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Fibonacci Series in C++
Operations on string literals
Although string literals are similar to arrays, but in C++, all the functions that are supported for arrays may not be supported for string literals.
In this section, we will discuss the operations that are allowed on string literals.
Equality Operation
In C++, == and != are the equality operators. They can be used to compare string literals. In case,both the string literals match, equality operation returns 1 otherwise 0.
But there’s a catch. If when we compare two identifiers with the same string literal, we are not coating string literals, we are actually comparing the addresses stored in them, which is inappropriate.
For example:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
if("Coding Ninjas"=="Coding Ninjas")
cout<<"Yes\n";
else
cout<<"No\n";
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Relational Operations
String literals do not support any kind of relational operators. Relational operators include >,<,>=,<+.
Assigning string literal to pointer
As mentioned earlier in this article, string literals are an array of characters and the identifier stores the address of the first character. So, when we assign a string literal to a pointer, it points to the first character.
For example:
char *example=”Coding Ninjas”;
You can also try this code with Online C++ Compiler
Run Code
Here, ‘example’ points to the character ‘C’.
Subscripts can be used to get some character from the string literal. For example, if we want the third character from the string, it can be accessed like this, “Coding Ninjas”[2] or example[2]. Both these notations produce the same output.
Concatenation of string literals
String literals can be concatenated implicitly if one string is written after another.
For example:
#include<iostream>
#include<string>
using namespace std;
int main()
{
char *example="Coding " "Ninjas";
cout<<example;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
One important thing here is that in memory it gets stored as “Coding Ninjas\0”. Null character(‘\0’) gets added automatically at the end.
For example:
#include<iostream>
#include<string>
using namespace std;
int main()
{
char *example="Coding"+"Ninjas";
cout<<example;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Now, look at the following C++ code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string example=string("Coding ")+string("Ninjas");
cout<<example;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
This leads to the conclusion that concatenation of string literals can not be achieved using the ‘+’ operator for the pointer type. But on the other hand, + operator is overloaded for std::string and can concatenate two string objects. So, at first strings need to be converted to string objects.
Frequently Asked Questions
Is the syntax of string literals the same in both C and C++?
Except for the use of single quotes, string literals have the same syntax as in C and C++.
Is it possible to change the value of a string literal?
Although, values of string literals are contents as a whole, but individual characters of the string literals can be altered.
Why are string literals immutable as a whole but their individual characters can be updated?
The reason why string literals are immutable as a whole is because they are allocated read-only memory, but pointer to the string literals has been allocated read-write memory.
Conclusion
This article extensively discusses string literals in detail. It also explains their properties and operations with appropriate examples.
Check out this article - C++ String Concatenation
We hope that this blog has helped you enhance your knowledge regarding the String literals and if you would like to learn more, check out our articles on Literals. Do upvote our blog to help other ninjas grow.
Happy Coding!