Which escape sequence will move the cursor to the beginning of the current line?

I am trying to move the cursor in a terminal program using escape sequences. In the below C program, it seems like the first three commands are successful (clear screen, move cursor home, print some reference text), however the last command where I try to move the cursor to an arbitrary position fails (2,2) instead moves the cursor to the beginning of the fourth line and also clears the fourth line. What am I doing wrong?

#include <stdio.h>
#include <stdlib.h>

int main()
{
  printf("\x1b[2J"); // clear screen
  printf("\033[H"); // move cursor home
  printf("1111\n2222\n3333\n4444"); // add some text to screen for reference
  printf("\033[2;2H"); // move cursor to 2,2
  while (1); // keep program running
}

asked Oct 16, 2021 at 16:24

1

You need to call fflush:

printf("\033[2;2H"); // move cursor to 2,2
fflush(stdout);
while (1); // keep program running

Notice that while (1); is not the correct way to keep the program running, an endless loop without a sleep will consume 100% of the CPU, instead:

while (1) sleep(1); // #include <unistd.h> for sleep()

or better yet:

getchar();

answered Oct 16, 2021 at 16:39

Which escape sequence will move the cursor to the beginning of the current line?

David RanieriDavid Ranieri

39.1k7 gold badges49 silver badges93 bronze badges

Which escape sequence will move the cursor to the beginning of the current line?

Escape sequences are special characters used in control strings to modify the format of the output. These specific characters are translated into another character or a sequence of characters that may be difficult to represent directly. For example, if you want to put a line break in the output of a C++ statement then you will use the “\n” character which is an escape sequence itself.

An escape sequence consists of two or more characters. For all sequences, the first character will be ”\” i.e. backslash. The other characters determine the interpretation of the escape sequence. For example, “n” of “\n” tells the cursor to move to the next line.

  • New Line (\n):
  • Tab (\t):
  • Alert Bell (\a):
  • Backspace (\b):
  • Single Quote (\’):
  • Carriage Return (\r):
  • Form Feed (\f)
  • Conclusion

New Line (\n):

When a new line is necessary for the output, then this escape sequence is used. For example:

cout<<”COMPUTER\nSCIENCE”;

First of all, “COMPUTER” is printed and”\n” shifts the cursor to the next line. Then “SCIENCE” is printed on the second line. A screenshot of the output is shown below:

Which escape sequence will move the cursor to the beginning of the current line?
C++ New Line (\n)

Tab (\t):

A TAB is equal to eight spaces. Whenever the TAB button is pressed from the keyboard, then 8 spaces are left blank. This escape sequence performs the functionality of the TAB key in the output stream. The code given below will insert a TAB between two words.

cout<<”COMPUTER\tSCIENCE”;

Which escape sequence will move the cursor to the beginning of the current line?
C++ Tab (\t)

As you can see after printing “COMPUTER”, 8 spaces are left blank, and then “SCIENCE” is printed on the screen.

Alert Bell (\a):

This escape sequence is used to play beep during execution. For example:

cout<<”COMPUTER\aSCIENCE”;

First of all, “COMPUTER” is printed then a beep is played and after that “SCIENCE” is printed.

Backspace (\b):

Whenever we want to delete a single character, we press the button “backspace” from our keyboard. The same functionality can be achieved in C++ output with this escape sequence. For example:

cout<<”COMPUTER\bSCIENCE”;

Which escape sequence will move the cursor to the beginning of the current line?
C++ Backspace (\b)

First of all, “COMPUTER” is printed and after that “\b” comes which deletes the last character i.e. “R”. After that, “SCIENCE” is printed.

Single Quote (\’):

To insert a single quote in the output, this escape sequence is used. Look at the code written below:

cout<<”\’COMPUTER SCIENCE\’”;

This code prints single quotes at the start and end of the string.

Which escape sequence will move the cursor to the beginning of the current line?
C++ Double Quote (\”)

Carriage Return (\r):

This escape sequence moves the cursor at the beginning of the current line. For example:

cout<<”COMPUTER\rSCIENCE”;

First of all, “COMPUTER” is printed and after that “\r” comes which moves the cursor at the beginning of the line and “SCIENCE” is printed which overwrites the word written before.

Which escape sequence will move the cursor to the beginning of the current line?
Carriage Return (\r)

Form Feed (\f)

This escape sequence is used in printing. It is used to insert blank paper into the printed output. For example:

cout<<”COMPUTER\fSCIENCE”;

After printing “COMPUTER”, the computer will include a blank paper and then print “SCIENCE”.

Conclusion

This article helped us understand some of the important escape sequences in C++. This is the kind of information a C++ beginner is expected to understand and know.

Post navigation

Which escape sequence can move the cursor to the beginning of the same line?

\r (Carriage Return) – We use it to position the cursor to the beginning of the current line.

What does an escape sequence begin with?

An escape sequence contains a backslash (\) symbol followed by one of the escape sequence characters or an octal or hexadecimal number. A hexadecimal escape sequence contains an x followed by one or more hexadecimal digits (0-9, A-F, a-f).

Which escape character can be used to display a beginning of a new line in C?

In C, all escape sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence. For example, \n is an escape sequence that denotes a newline character.

What does \r mean in C?

'\r' is the carriage return character.