Skip to content
Advertisement

gotoxy() function using printf() ‘s position

Hello there
i am working a project which need the gotoxy() function
i have read gotoxy() implementation for Linux using printf

i wonder why the

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

need to change the x y order in printf, is that just to fit the coordinate system?
in my way, i change it to printf("%c[%d;%df",0x1B,x,y) to meet my needs

stil, during my using this gotoxy() in for loop like this:

for( int i = 0; i < 12; i++ ) {
        for( int j = 0; j < 12; j++ ) {
            gotoxy( i , j );
            usleep(500000);
        }
    }

when i = 0 and i = 0, the cursor are on the first row
i wonder why cursor does’t go to second row when i = 1?

Advertisement

Answer

The order of x and y matters because the names of the variables have no meaning to the operation of the gotoxy() function.

That function is outputing a terminal command sequence that moves to the specified coordinates. When the terminal sees that command sequence and processes it, y is expected first.

By the way, be careful with this solution as this is highly dependent on the type of terminal within which the program is run. In order to get wide terminal support with random movement and “drawing” on a terminal screen, ncurses or curses are your best bet. They are challenging to learn at first though.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement