Gaurav Pant Gaurav Pant - 3 years ago 141
C Question

How to implement gotoxy() using printf()

Can someone please tell me how gotoxy() is implemented using printf()

void gotoxy(int x,int y)
{ printf("%c[%d;%df",0x1B,y,x); }


What are the things written inside printf() supposed to mean to the compiler ?

Answer Source

This particular printf is using char then integer formatting to generate an ANSI escape sequence (recognizable by ESC (aka 0x1B) then [), then the coordinates to move cursor to. In your case:

Esc[Line;Columnf Cursor Position:

Moves the cursor to the specified position (coordinates). If you do not specify a position, the cursor moves to the home position at the upper-left corner of the screen (line 0, column 0).

When this sequence is issued to a capable terminal through standard output like printf does (not all terminals are compatible), the command is executed by the terminal.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download