Skip to content
Related Articles

Related Articles

Improve Article

How to use gotoxy() in codeblocks?

  • Difficulty Level : Basic
  • Last Updated : 18 May, 2021

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.

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



My Personal Notes arrow_drop_up
Recommended Articles
Page :