Console cursor coordinates in C

2

Taking the following code as an example:

#include <stdio.h>

int main(void)
{
    printf("Hello world");
    return 0;
}

Where "Hello World" will be written on line 1 and column 1 of the console. How do I change this, for example, type it in line 3, column 5?

    
asked by anonymous 02.09.2015 / 15:57

1 answer

2

There is no standard way of doing this, it depends on the console library you are using. In Linux it usually works fine:

void gotoxy(int x, int y) {
    printf("3[%d;%dH", x, y);
}

On Windows you can use SetConsoleCursorPosition .

void gotoxy(int x, int y) { 
    COORD pos = {x, y};
    HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(output, pos);
}
    
02.09.2015 / 16:10