string-literals

What does the symbol \0 mean in a string-literal?

有些话、适合烂在心里 提交于 2019-11-26 07:25:27
问题 Consider following code: char str[] = \"Hello\\0\"; What is the length of str array, and with how much 0s it is ending? 回答1: sizeof str is 7 - five bytes for the "Hello" text, plus the explicit NUL terminator, plus the implicit NUL terminator. strlen(str) is 5 - the five "Hello" bytes only. The key here is that the implicit nul terminator is always added - even if the string literal just happens to end with \0 . Of course, strlen just stops at the first \0 - it can't tell the difference.

How to get double quotes into a string literal?

孤街浪徒 提交于 2019-11-26 06:39:40
问题 I have the following output created using a printf() statement: printf(\"She said time flies like an arrow, but fruit flies like a banana.\"); but I want to put the actual quotation in double-quotes, so the output is She said \"time flies like an arrow, but fruit flies like a banana\". without interfering with the double-quotes used to wrap the string literal in the printf() statement. How can I do this? 回答1: Escape the quotes with backslashes: printf("She said \"time flies like an arrow, but

C++ warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

爱⌒轻易说出口 提交于 2019-11-26 05:54:44
问题 I am using gnuplot to draw a graph in C++. The graph is being plot as expected but there is a warning during compilation. What does the warning mean? warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] This is the function I am using: void plotgraph(double xvals[],double yvals[], int NUM_POINTS) { char * commandsForGnuplot[] = {\"set title \\\"Probability Graph\\\"\", \"plot \'data.temp\' with lines\"}; FILE * temp = fopen(\"data.temp\", \"w\"); FILE * gnuplotPipe

Why are string literals l-value while all other literals are r-value?

蹲街弑〆低调 提交于 2019-11-26 05:28:26
问题 C++03 5.1 Primary expressions §2: A literal is a primary expression. Its type depends on its form (2.13). A string literal is an lvalue; all other literals are rvalues. What is the rationale behind this? As I understand, string literals are objects, while all other literals are not.And an l-value always refers to an object. But the question then is why are string literals objects while all other literals are not? This rationale seems to me more like an egg or chicken problem. I understand the

Computing length of a C string at compile time. Is this really a constexpr?

偶尔善良 提交于 2019-11-26 05:25:26
问题 I\'m trying to compute the length of a string literal at compile time. To do so I\'m using following code: #include <cstdio> int constexpr length(const char* str) { return *str ? 1 + length(str + 1) : 0; } int main() { printf(\"%d %d\", length(\"abcd\"), length(\"abcdefgh\")); } Everything works as expected, the program prints 4 and 8. The assembly code generated by clang shows that the results are computed at compile time: 0x100000f5e: leaq 0x35(%rip), %rdi ; \"%d %d\" 0x100000f65: movl $0x4

Where do Java and .NET string literals reside?

◇◆丶佛笑我妖孽 提交于 2019-11-26 03:33:42
问题 A recent question about string literals in .NET caught my eye. I know that string literals are interned so that different strings with the same value refer to the same object. I also know that a string can be interned at runtime: string now = DateTime.Now.ToString().Intern(); Obviously a string that is interned at runtime resides on the heap but I had assumed that a literal is placed in the program\'s data segment (and said so in my answer to said question). However I don\'t remember seeing

Single quotes vs. double quotes in C or C++

时光怂恿深爱的人放手 提交于 2019-11-26 03:13:47
问题 When should I use single quotes and double quotes in C or C++ programming? 回答1: In C and in C++ single quotes identify a single character, while double quotes create a string literal. 'a' is a single a character literal, while "a" is a string literal containing an 'a' and a null terminator (that is a 2 char array). In C++ the type of a character literal is char , but note that in C, the type of a character literal is int , that is sizeof 'a' is 4 in an architecture where ints are 32bit (and

What is the backslash character (\\)?

你。 提交于 2019-11-26 01:47:40
问题 What is the string literal \\\\ backslash ? What does it do? I have thought about it but I do not understand it. I also read it on wikipedia. When I try to print the following: System.out.println(\"Mango \\\\ Nightangle\"); the output is: Mango \\ Nightangle What is the significance of this string literal? 回答1: \ is used as for escape sequence in many programming languages, including Java. If you want to go to next line then use \n or \r , for tab use \t likewise to print a \ or " which are

Unicode encoding for string literals in C++11

岁酱吖の 提交于 2019-11-26 01:27:16
问题 Following a related question, I\'d like to ask about the new character and string literal types in C++11. It seems that we now have four sorts of characters and five sorts of string literals. The character types: char a = \'\\x30\'; // character, no semantics wchar_t b = L\'\\xFFEF\'; // wide character, no semantics char16_t c = u\'\\u00F6\'; // 16-bit, assumed UTF16? char32_t d = U\'\\U0010FFFF\'; // 32-bit, assumed UCS-4 And the string literals: char A[] = \"Hello\\x0A\"; // byte string, \

How to add percent sign to NSString

自作多情 提交于 2019-11-26 00:40:25
问题 I want to have a percentage sign in my string after a digit. Something like this: 75%. How can I have this done? I tried: [NSString stringWithFormat:@\"%d\\%\", someDigit]; But it didn\'t work for me. 回答1: The code for percent sign in NSString format is %% . This is also true for NSLog() and printf() formats. 回答2: The escape code for a percent sign is "%%", so your code would look like this [NSString stringWithFormat:@"%d%%", someDigit]; Also, all the other format specifiers can be found at