Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are string literals?
3.
Properties of string literals
4.
Operations on string literals
4.1.
Equality Operation 
4.2.
Relational Operations 
4.3.
Assigning string literal to pointer 
4.4.
Concatenation of string literals 
5.
Frequently Asked Questions
5.1.
Is the syntax of string literals the same in both C and C++?
5.2.
Is it possible to change the value of a string literal?
5.3.
Why are string literals immutable as a whole but their individual characters can be updated?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

String literals

Author Vidhi Singh
0 upvote

Introduction

You must be familiar with the variables, which basically store data under a name. It is this data that is called ‘literal’.

Literals are the data that represent the actual constant data. Examples of literals are 45, “C++”, ‘N”, etc. Note that literals are different from ‘const’. Literals are basically fixed data, while constants store the unchangeable data. 

In this article, we will discuss a specific type of literals - string literals. Also see, Literals in C.

What are string literals?

So by now you know that literals are just values. These values can be of any data type - integer, float, character or string. 
Talking specifically about string literals, it is a sequence of characters in double-quotes. 

For example: “Coding Ninjas”, “Ninjas”, etc. 
Even “ “ is a string literal. It has zero character but is enclosed in double-quotes, so it will be considered as a string literal. 

A string literal should not be confused with character literal. For instance, ‘C’ is a character literal whereas “C” is a string literal. 

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!    

Live masterclass