Gaurav Pant
Gaurav Pant

Reputation: 952

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 ?

Upvotes: 1

Views: 1022

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140196

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, it's not compiler or compiler library dependent.

Upvotes: 1

Related Questions