Issue
With the following code, what's the output of this code, and why?
#include <stdio.h>
int main() {
printf("Hello world\n"); // \\
printf("What's the meaning of this?");
return 0;
}
Solution
The backslash at the end of the 4th line is escaping the following new line so that they become one continuous line. And because we can see the // beginning a comment, the 5th line is commented out.
That is, your code is the equivalent of:
#include <stdio.h>
int main() {
printf("Hello world\n"); // \printf("What's the meaning of this?");
return 0;
}
The output is simply "Hello world" with a new line.
Edit: As Erik and pmg both said, this is true in C99 but not C89. Credit where credit is due.
It is defined in the 2nd phase of translation (ISO/IEC 9899:1999 §5.1.1.2):
Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines.
Answered By - Joseph Mansfield Answer Checked By - Marilyn (WPSolving Volunteer)