×

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function.

What is gotoxy() function?

In C language, gotoxy() is a pre-defined function that helps in moving a text by placing the cursor at any desired location on the output screen. Using the gotoxy() function, one can quickly move or change the cursor position anywhere. The cursor position is also known as the cursor location. The gotoxy() function prints the required text wherever the cursor moves. The cursor is driven based on the x and y coordinates mentioned in the gotoxy() function.

Syntax

gotoxy(int x, int y);

Here (x,y) is where the user wants to print the text on the output screen.

C program to print the “king” message on the output screen without using the gotoxy() function :

// C program without using gotoxy() function
#include <stdio.h>
void main()
{
    printf("king"); //printing the text “king”
}

Output:

How to move a text in C

This is the output for printing the text "king" without using the gotoxy() function.

The text "king" is displayed on the coordinates (0,0) of the output screen. It shows the text on the top left side, which is at coordinates (0,0) by default. The top left side is the default position for a text to be displayed.

gotoxy() in Code::Blocks

In Code::Blocks, the gotoxy() pre-defined function is not provided. Hence, a function “SetConsoleCursorPosition()” is used to replace the gotoxy() function. SetConsoleCursorPosition() is used for the same procedure as gotoxy().

A header file named “#include<windows.h>” is added to use the SetConsoleCursorPosition() function.

The function SetConsoleCursorPosition() has some arguments such as :

  • Handle
  • COORD

Handle: The handle is used to get the values. A pre-defined function "GetStdHandle(STD_OUTPUT_HANDLE)” is called.

COORD: The X and Y coordinates are available by using the pre-defined function COORD.

Note: A screen usually has 80 columns and 25 lines.

C program to print the “king” message on the output screen using the SetConsoleCursorPosition() function instead of using gotoxy() function:

#include <stdio.h>
#include <windows.h>
void main()
{
    COORD c; 
    c.X = 50; //x coordinate as 50
    c.Y = 12; // y coordinate as 12
   // using the SetConsoleCursorPosition() function
    SetConsoleCursorPosition(
        GetStdHandle(STD_OUTPUT_HANDLE), c); 
    printf("king"); //printing the text “king”
    getch();
}

Output:

How to move a text in C

This is how the output looks at the position x=50 and y=12 using the COORD. The text “king” can be displayed anywhere on the output screen by changing the coordinates x and y, where x is the horizontal axis, and y is the vertical axis.

C program to print the “king” message on the output screen using the gotoxy() function by defining it :

#include<windows.h>
void gotoxy(int x,int y)
{
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
main()
{
int x=50,y=12;
char ch;
gotoxy(x,y); //function call
printf("king");
while(1){
ch=getch();
switch(ch)
{
case 'a':
x--; //horizontal decrement in position
break;
case 'd':
x++; //horizontal increment in position
break;
case 'w':
y--; //vertical increment in position
break;
case 's':
y++; //vertical decrement in position
break;
case 'p':
exit(0);
}
system("cls"); //acts like a clrscr() in turbo c/c++
gotoxy(x,y);
printf("king");
}
}

Output 1:

How to move a text in C

This is the output displayed without using any keys.

Output 2:

How to move a text in C

This is the output displayed when the key 'd' is used five times. This moves the text 5 coordinates to the right from x=50. “_” is the cursor whose position can be changed by pressing keys.

Note: COORD holds the coordinates x and y. The COORD is named after coordinates. The GetStdHandle function returns a handle as input, output, or error. Windows kernel object can be accessed to a handle that is an index in a system table. SetControlCursorPosition() function sets the cursor location.

 In the gotoxy() function, the x and y coordinates are row and column numbers.

In Turbo C/C++, the gotoxy() function is available. This function moves a text on the output screen using the coordinates ( parameters passed to the function). The top compilers, GCC and Visual Studio, do not have a gotoxy() function. The text alignment can be maintained using the gotoxy() function.

Program using gotoxy() function (turbo c/c++):

The following program is compatible only with the turbo c/c++ compiler.

#include<stdio.h>
#include<conio.h>
int main() 
{
   int x, y;
   x = 10;
   y = 10;
   gotoxy(x, y);
   printf("king”); // printing the text “king”
   getch();
   return 0;
}

Output:

How to move a text in C

Related Topics

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.