I am reading the Kernigan and Ritchie manual on C.
The following example arises:
printf("hello, world
");
The book states that the C compiler will produce an error message. How exactly does the compiler detect this, and why is this an issue? Shouldn't it just read the newline (presumably at the end of world) as no space?
You are not allowed to have a newline in a string literal, we can see this by looking at the grammar from the C99 draft standard section 6.4.5 String literals:
string-literal:
" s-char-sequenceopt "
L" s-char-sequenceopt "
s-char-sequence:
s-char
s-char-sequence s-char
s-char:
any member of the source character set except
the double-quote ", backslash \, or new-line character
We can see that s-char allows any character except ", \ and new-line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With