Open In App

How to use gotoxy() in codeblocks?

Improve
Improve
Like Article
Like
Save
Share
Report

The gotoxy() function places the cursor at the desired location on the screen. This means it is possible to change the cursor location on the screen using the gotoxy() function. It is basically used to print text wherever the cursor is moved. Below is the C program to print the “hello” message on the screen without using the gotoxy() function:

C




// C program for the above approach
#include <stdio.h>
 
// Driver Code
void main()
{
    printf("hello");
}


Output:

Output#1

Explanation: The message “hello” is print on the top left side of the screen by default. So to print text at a particular coordinate use the gotoxy() function.

gotoxy() In Code::Blocks:

Code blocks don’t have a gotoxy() predefined function. Therefore, “SetConsoleCursorPosition()” can be used to carry out the same procedure. To use this function add a header file called #include<windows.h>. The arguments for SetConsoleCursorPosition() are:

  • Handle: To get the value of handle, call a predefined function “GetStdHandle(STD_OUTPUT_HANDLE)”.
  • Coord: The predefined function used to get X and Y coordinates.

Note: A screen has 25 lines and 80 columns.

Below is the implementation of the above-discussed function to print the “hello” message in the center of the screen:

C




// C program for the above approach
 
#include <stdio.h>
#include <windows.h>
 
// Driver Code
void main()
{
    // Input
    COORD c;
    c.X = 40;
    c.Y = 16;
 
    SetConsoleCursorPosition(
        GetStdHandle(STD_OUTPUT_HANDLE), c);
 
    printf("hello");
    getch();
}


Output:

Note: Any value for X and Y can be used to print the desired text at any location on the screen. Here, X is used for the vertical axis and Y is used for the horizontal axis.



Last Updated : 02 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads