×

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

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 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.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.