gotoxy in C
We can move the cursor using the gotoxy function, which places it at the desired spot on the screen.
Declarative expression: void gotoxy(int x, int y), where (x, y) is the desired cursor location.
Simple C program to Illustrate gotoxy in c:
// Works in Turbo C compiler only
#include <stdio.h>
#include <conio.h>
int main()
{
int x, y;
x = 10;
y = 10;
gotoxy(x, y);
printf("C program to change cursor position.");
getch();
return 0;
}
This code is a simple C program that uses the `gotoxy()` function to change the cursor position on the screen and then prints a message at that position. As mentioned in the comments, this code works specifically in the Turbo C compiler, which is an old and outdated compiler mainly used for DOS-based systems.
The code is explained in detail below:
1. The code uses two common C header files:'stdio.h' for standard input/output operations like 'printf()' and 'conio. h' for operations on the console like 'gotoxy()'.
2. The two integer variables 'x' and 'y' are declared and initialised in the'main()' function. These variables stand in for the terminal screen's horizontal and vertical coordinates (rows and columns), which are needed to move the cursor.
3. 'x' and 'y' are both set to 10.
4. 'x' and 'y' are used as arguments when calling the 'gotoxy()' method. Since the coordinates in this case are 1-based and 'x' and 'y' are both 10, the pointer will be moved to position (10, 10) on the console screen (11th column, 11th row).
5. The message "C programme to change cursor position." is printed at the appropriate position once the cursor has been moved there using the 'printf()' function.
6. Before the program ends, the 'getch()' function is used to wait for a key hit. This is done so that the user may view the output before the console window closes as soon as the command is executed.
7. The program exits after returning 0, which denotes a successful execution.
The text cursor is often positioned on the console or terminal screen using the C language's gotoxy() function in DOS or previous Turbo C environments. It enables you to place the cursor at particular locations (columns and rows) so that any ensuing text output will display there. In console-based applications, the 'gotoxy()' function can be used to build text-based menus, user interfaces, games, and rudimentary graphics.
Here are some common uses of the `gotoxy()` function in C:
1. Creating Text-Based User Interfaces: 'gotoxy()' can be used to place text at specific spots on the screen, allowing you to create straightforward text-based menus, forms, or interactive interfaces.
2. Text Animations: You may make straightforward text animations, such as scrolling or moving text, by combining the function "gotoxy()" with delays (using "sleep()" or related procedures).
3. Simple ASCII-based images, such as boxes, lines, and shapes, can be drawn on the terminal screen with the help of the limited 'gotoxy()' function.
4. Games: 'gotoxy()' can be used to design simple game interfaces, manage the positioning of game objects, and display scores in simple console-based games.
5. Presentations or Screen Layouts: 'gotoxy()' can assist in putting text or material at specified spots for a more organized display for instructional purposes or presentations in terminal environments.
6. Changing Console Output: You can make command-line tools or reports' output more aesthetically pleasing or organized by dragging the cursor to particular locations.
Although 'gotoxy()' can be helpful in some circumstances, it is typically not advised for modern C/C++ work. Console-based apps provide a more constrained user experience than modern graphical user interfaces (GUI), cross-platform applications, and other applications. For GUI apps in contemporary development, you might consider employing graphical libraries (like GTK, Qt, or SDL) or ncurses for more complex console-based interfaces.
Adverse effects of using gotoxy()
Difficult to Maintain: Excessive usage of gotoxy() in code can result in "spaghetti code," which makes it difficult to follow the control flow and reduces maintainability and readability. Code with a lot of gotoxy() calls might become complicated and difficult to fix.
The gotoxy() function, which is exclusive to DOS-based systems and older Turbo C compilers, is not a standard C function. Modern systems and non-DOS environments won't be compatible with it. Its lack of portability limits the code's interoperability with other systems.
Not Standardised: Since gotoxy() is not a part of the C standard library, its availability or behavior cannot be guaranteed in all compilers or operating systems. When compiled with a different compiler or run on a different system, code that uses gotoxy() may malfunction or yield unexpected results.
Limitations to Console Output: The gotoxy() function can only move the cursor on the console screen; it is unable to handle any other console input or output requests or communicate with the user in any way other than text output.