printf("\nab"); printf("\bsi"); printf("\rha");

output =hai

could you please explain me how output came out to be "hai"?

Recommended Answers

All 2 Replies

\n is an escape character that moves the cursor to a new line
\b is backspace
\r moves the cursor back to the beginning of the current line

So if you use pencil & paper to draw the characters on paper and follow the above escape characters you will see how the result is hai.

Escape sequences :

\n means newline.
\b means backspace.
\r means carriage return.

Now, at first a line is printed followed by ab. Then with backspace (deleting one character, here it is b ) si is printed. So now you have asi. The carriage return moves the cursor to the starting of this line. Then it prints ha. So you get hai.

Try commenting this printf()'s to see the intermediate steps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.