C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program Swap Numbers in cyclic order Using Call by Reference C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++ C Function Argument and Return Values Keywords in C Bank management system in C Calendar application in C Floor() Function in C Free() Function in C How to delete a file in C How to move a text in C Remove an element from an array in C Unformatted input() and output() function in C What are linker and loader in C fork() in C GCD program in C Branching Statements in C Comma Operator in C Control statement in C Double Specifier in C How to create a binary file in C Long int in C Palindrome Number in C Pure Virtual Function in C Run Time Polymorphism in C Types of Array in C Types of Function in C What is a buffer in C What is required in each C Program Associativity of Operators in C Bit Stuffing Program in C Actual and Formal Parameters Addition of two Numbers in C Advantages of function in C Arithmetic Progression Program in C Binomial Coefficient Program in C Difference between Array and List in C Diffie-Hellman Algorithm in C How to convert a number to words in C How to convert a string to hexadecimal in C Difference between If and Switch Statement in C

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



ADVERTISEMENT
ADVERTISEMENT