If for the following code:
printf(\"HEllo\\n\"); // do not change this line.
printf(\"\\b\\bworld\");
I need an output: Helloworld (In a
How about simply:
printf("Helloworld");
\n
is an escape sequence for a new line. Since you want everything to appear on the same line, there's no reason to specify \n
.
The problem is you can't reliably move back up a line (using \b
) after you've printed a new line. But if you require that there be two lines of code in your source, you can simply omit both escape sequences:
printf("HEllo");
printf("world");
If you're writing a Win32 console application, you can take advantage of the Console Screen Buffer API. The following code will move 1 line up:
printf("HEllo\n");
CONSOLE_SCREEN_BUFFER_INFO coninfo;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &coninfo);
coninfo.dwCursorPosition.Y -= 1; // move up one line
coninfo.dwCursorPosition.X += 5; // move to the right the length of the word
SetConsoleCursorPosition(hConsole, coninfo.dwCursorPosition);
printf("world");
Output:
HElloworld