subsititue of getchar and clrscr() in c for linux

17,295

Solution 1

The getch() function can be found in curses.h (library "curses"). The same library offers functions to clear the screen. Check out these links:

http://linux.die.net/man/3/getch

http://linux.die.net/man/3/erase

Solution 2

# include <curses.h>

       int erase(void);
       int werase(WINDOW *win);
       int clear(void);
       int wclear(WINDOW *win);
       int clrtobot(void);
       int wclrtobot(WINDOW *win);
       int clrtoeol(void);
       int wclrtoeol(WINDOW *win);

DESCRIPTION
       The erase and werase routines copy blanks to every position in
       the window, clearing the screen.

I'm guessing this question was repeatedly downvoted because it implies a poor understanding of basic C language features and/or that OP is simply copying/pasting code into editor/IDE.

Similarly, just use system("exit"); within your code:

#include<stdlib.h>
main()
{
system("clear"); //clears the screen
} 

Checking the man-pages shows:

SYSTEM(3)                                 Linux Programmer's Manual   SYSTEM(3)

NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>

       int system(const char *command);

DESCRIPTION
       system() executes a command specified in command by calling /bin/sh -c 
command, and returns after the command has been completed.  
During execution of the command, SIGCHLD will be blocked, and SIGINT
and SIGQUIT will be ignored.

It could also be the case that this question is a possible duplicate of the following:

Finally, take a look at the following for more details and examples:

Solution 3

system("clear"); can be used in linux instead of clrscr();

Share:
17,295

Related videos on Youtube

Daarks Solutions
Author by

Daarks Solutions

Updated on September 15, 2022

Comments

  • Daarks Solutions
    Daarks Solutions over 1 year

    I am working on Ubuntu these days. When I compiled my C program using gcc, it is giving the error conio.h doesn't exists. I want to use clrscr() and getch() function. Can you please tell me the substitute of this header file in linux.

    • Some programmer dude
      Some programmer dude over 11 years
      Check out the ncurses library.
  • Keith Thompson
    Keith Thompson over 11 years
    I believe the curses getch() function can be used only after calling initscr(), which takes control of the (text) screen.
  • Daarks Solutions
    Daarks Solutions over 11 years
    I am trying for curses.h and it too is not found on my ubantu o.s
  • Axel
    Axel over 11 years
    Have you tried locating it in the repositories? If you don't know how to do that, try askubuntu.com.