Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why exactly does printf not compile?

Tags:

c

printf

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?

like image 865
pyrrhic Avatar asked Dec 28 '25 15:12

pyrrhic


1 Answers

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.

like image 97
Shafik Yaghmour Avatar answered Dec 31 '25 06:12

Shafik Yaghmour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!