How are string literals represented and implemented in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How are string literals represented and implemented in C++?

11th Jun 2018, 10:48 AM
Meenakshi Dabas
Meenakshi Dabas - avatar
4 Answers
+ 2
According to the standard: "An ordinary string literal has type "array of n const char" and static storage duration." i.e. const char* IIRC, string literals are usually stored in read-only memory. What you get when you create a string literal in a program, is a constant character pointer which points to the first character of the string. "helloworld", for example, would be stored contiguously in read-only memory as 'h' 'e' 'l' 'l' 'o' 'w' 'o' 'r' 'l' 'd' '\0' A constant character pointer which points to the character 'h' is returned. The string is null-terminated (the end of the string is signified by the presence of the '\0' character at the end of the contiguous blocks.
11th Jun 2018, 10:59 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
string literals are long sequence of letters which are enclosed by double quote ( "") . Ex- " Hi I am James " ; all string literals are terminated by an escape sequence ' \0 ' (NULL) that's why the length of the string is 1 more than the letter it consists . string a = " Hi I am James " ; cout << a << endl; // Hi I am James
11th Jun 2018, 10:56 AM
Aveek Bhattacharyya
Aveek Bhattacharyya - avatar
+ 1
They are C-style strings (null-terminated).
11th Jun 2018, 10:57 AM
Vlad Serbu
Vlad Serbu - avatar
0
Null termination also happens in c++ ( string ) they are literally added by \0 at the end
11th Jun 2018, 10:59 AM
Aveek Bhattacharyya
Aveek Bhattacharyya - avatar