Conio.h in C and C++ is one of the libraries that have a set of functions allowing for interacting with the console and, therefore, expanding the opportunities of console applications. Among these functions, gotoxy
is essential for cursor control, allowing precise placement of text on the console screen. This article will explain the gotoxy
function’s syntax, usage, and practical applications with examples.
Overview of conio.h
conio.h
stands for console input/output. Among C and C++, it is widely used for functions suited to DOS-based systems, as well as early Windows environments. It contains functions for handling console I/O, including reading keyboard input, manipulating text, and controlling the cursor.
The gotoxy()
Function
The gotoxy
function is used to move the cursor to a specified position on the console screen.
Syntax of gotoxy()
void gotoxy(int x, int y);
Parameters
- x : The horizontal position (column) where the cursor should be moved.
- y : The vertical position (row) where the cursor should be moved.
Example
#include <conio.h>
#include <stdio.h>
int main() {
// Clear the screen
clrscr();
// Move the cursor to position (5, 10)
gotoxy(5, 10);
// Print text at the specified position
cprintf("This text is at position (5, 10).");
// Move the cursor to position (20, 15)
gotoxy(20, 15);
// Print more text
cprintf("And this text is at position (20, 15).");
// Wait for a key press
getch();
return 0;
}
Output: