0

"'a''b''c'" is it a string literal?

6th Feb 2017, 2:02 PM
Suptajoy Barua
Suptajoy Barua - avatar
3 Answers
+ 1
Yes, indeed it is. The string literal will be: 'a''b''c' Unfortunately the 2 single quotes next to each other in the SoloLearn font looks like a double quote - which can be confusing (I had to copy & paste it into a different editor to check). But, since there are only double quotes on the "outside", they are all interpreted as single quotes. So I'm wondering: Are you asking based on the way a character literal is assigned: char c = 'a'; ...and that made you wonder what happens to a character literal inside a string literal? In fact, there are no such thing - the single quote (') inside a string literal ("..say 'hi' to him..") is simply interpreted as single quotes i.e. a character like any other. That leaves the question: what if you want a double quote inside a string to be shown, and not just function to delimit a string literal as in C/C++ syntax? Maybe where you want to output something like this: She sang "Obladi Oblada" by the Beatles. In this case you have to "escape" the double quotes like this: cout << "She sang \"Obladi Oblada\" by the Beatles." << endl; There are a number of characters that are escaped this way to give them special meaning. For example "This string has a <carriage return> and <line feed> at the end\r\n". there are others as well. One last trick which is very useful: The compiler will concatenate string literals into a single string literal if they follow each other like this: string s = "This will be" " a single string literal"; This is very useful to duplicate multi-line text: string poem = "To be or not to be..\n" "That is the question!\n" "Hamlet said...\n"
6th Feb 2017, 3:38 PM
Ettienne Gilbert
Ettienne Gilbert - avatar
+ 1
Hi @CodeRunner, Your answer is technically right in the sense that escaping the single quotes inside the string like you have done will work. However, it is not necessary; all c++ compilers that I am familiar with can handle single quotes inside double-quote delimited string literals. So his "'a''b''c" statement is in fact not invalid and will work fine - including on the SoloLearn C++ compiler.
6th Feb 2017, 3:52 PM
Ettienne Gilbert
Ettienne Gilbert - avatar
0
well... you need to work a little more see... there are certain keys that you can't directly put in the cout statements (e.i ', ", return carriage, etc). So these elements are added in a cout statements using ESCAPE SEQUENCES. ESCAPE SEQUENCES ARE. \n - new line \\ - backslash \' - single quote \" - double quote . . . etc. therefore your statement "'a''b''c" is invalid and will cause compiler error.. it must be " \'a\'\'b\'\'c\' " now this is a string!
6th Feb 2017, 3:10 PM
GeekyShacklebolt
GeekyShacklebolt - avatar