More Lines
printf prints exactly the text you give it, and nothing more. It doesn't start a new line by itself, so the next text continues right after it, on the same line:
printf("Good");
printf("morning");This prints:
GoodmorningTo end a line, write \n in the text. The backslash \ followed by the letter n (new line) isn't printed: together, the two characters mean the end of the line. That's why \n usually goes at the end of the text, just before the closing quote:
printf("Good\n");
printf("morning\n");This prints:
Good
morning\n can be anywhere in the text, even several times. Each one ends a line, so this single printf prints two lines:
printf("Roses are red,\nviolets are blue.\n");This prints:
Roses are red,
violets are blue.Two \n in a row make an empty line, because there is nothing between them:
printf("Chapter 1\n\nIt was a dark and stormy night.\n");This prints:
Chapter 1
It was a dark and stormy night.Usually, though, it's clearer to print each line with its own printf, so that the code looks like the output.
A common mistake is a forgotten \n: the program runs, but two lines stick together, like Goodmorning above. When your output looks like that, check the ends of your texts. And watch out: \n is written with a backslash \, not with a slash /. /n is just two ordinary characters, and they get printed.
Instructions
Run the program: it's a postcard, but both sentences are on one line. Add \n at the end of the first text, inside the quotes, so that the program prints:
Greetings from the mountains!
The weather is great.With another printf, add a third line. End the second text with \n too, so that the third line starts on a new line. The program then prints:
Greetings from the mountains!
The weather is great.
Tomorrow we're going skiing.Finish the postcard with an empty line and a signature: Love, and your name, such as Love, Anna. One printf is enough: the third line already ends with \n, so if you start the text of the signature with one more \n, you get an empty line. End the signature with \n as well. The program then prints:
Greetings from the mountains!
The weather is great.
Tomorrow we're going skiing.
Love, AnnaStart programming for free
2/7