Learning C-Style C++


by Direct Approach Mentoring



printf(
"String"
)

5. Screen Output

To write the word
"Hello"
on the screen, use the

printf

function, like this:

printf(
"Hello"
);



That is an example of the simplest use of

printf

. You can replace "Hello" with any string you want.

But what if you want to print a line-break (Enter key) or a double quote or other special characters? Then you have to use escape sequences. This is when you use 2 characters to really mean one character, but that you can't type by itself.

Let's say you want a line-break at the end of the word
"Hello"
, just do this:

printf(
"Hello\n"
);



Whenever \n appears inside a string (or text inside these: "") it will be replaced with a line-break when your program is running. This is called an escape sequence. Here are more escape sequences:
Sequence  What You Get
\nLine-break
\tTab
\"" (because otherwise it would end the string too early)
\'' (same thing but for single characters, not strings)
\0NULL (means end-of-string)
\\\ (so you can get a backslash without escaping the next character)