ADVERTISEMENT
ADVERTISEMENT

gotoxy(), clrscr(), getch() and getche() functions for GCC Linux.

gotoxy(), clrscr(), getche() and getch() in GCC Linux:
In this section you will learn how gotoxy(), getche(), getch() and clrscr() function can be used in GCC Linux. In TurboC compiler you can use that functions by including conio.h header file, but in Linux library these function are not available, so we are providing the function definitions for GCC linux just use them into your program and call where they needed.

gotoxy() for GCC Linux:

gotoxy() move the cursor at specified location in the output screen.

// gotoxy() function definition

void gotoxy(int x, int y)
{
    printf("%c[%d;%df", 0x1B, y, x);
}

Consider the example:

#include <stdio.h>

// gotoxy() function definition
void gotoxy(int x, int y)
{
    printf("%c[%d;%df", 0x1B, y, x);
}

int main()
{
    int x = 10, y = 20;
    gotoxy(x, y); //move cursor position
    printf("Hello World!!!"); //print message
    return 0;
}

Output:

    Compile:
    sh-4.3$ gcc -o main *.c

    Run:
    sh-4.3$ ./main


            Hello World!!!

clrscr() for GCC Linux:

We can implement clrscr() by using clear command of the linux terminal, It is used to Clear Output Screen.

// clrscr() function definition

void clrscr(void)
{
    system("clear");
}

Consider the example:

#include <stdio.h>

// clrscr() function definition
void clrscr(void)
{
    system("clear");
}

int main()
{
    clrscr(); //clear output screen
    printf("Hello World!!!"); //print message
    return 0;
}

Output:

    Compile:
    sh-4.3$ gcc -o main *.c

    Run:
    sh-4.3$ ./main
    Hello World!!! [on cleared screen]

getch() and getche() for GCC Linux:

getch() function is used to get (read) single character from standard input device (keyboard) without echoing i.e. it does not display the input character & it does not require [return] key after input. getch() is declared in conio.h header file.

getche() function is used to get (read) single character from standard input device (keyboard) with echoing i.e. it displays the input character & it does not require [return] key after input. getche() is declared in conio.h header file.

Consider the example:

#include <termios.h>
#include <stdio.h>

static struct termios old, new;

/* Initialize new terminal i/o settings */
void initTermios(int echo)
{
    tcgetattr(0, &old); //grab old terminal i/o settings
    new = old; //make new settings same as old settings
    new.c_lflag &= ~ICANON; //disable buffered i/o
    new.c_lflag &= echo ? ECHO : ~ECHO; //set echo mode
    tcsetattr(0, TCSANOW, &new); //apply terminal io settings
}

/* Restore old terminal i/o settings */
void resetTermios(void)
{
    tcsetattr(0, TCSANOW, &old);
}

/* Read 1 character - echo defines echo mode */
char getch_(int echo)
{
    char ch;
    initTermios(echo);
    ch = getchar();
    resetTermios();
    return ch;
}

/* 
Read 1 character without echo 
getch() function definition.
*/
char getch(void)
{
    return getch_(0);
}

/* 
Read 1 character with echo 
getche() function definition.
*/
char getche(void)
{
    return getch_(1);
}

int main(void)
{
    char c;

    printf("(getche example) Please enter a character: ");
    c = getche();
    printf("\nYou entered: %c\n", c);

    printf("(getch example) Please enter a character: ");
    c = getch();
    printf("\nYou entered: %c\n", c);

    return 0;
}

Output:

Compile:
sh-4.3$ gcc -o main *.c

Run:
sh-4.3$ ./main
(getche example) Please enter a character: X
You entered: X
(getch example) Please enter a character:
You entered: Y

C Advance Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.