Finding a Substitute for getch() on Linux | C Programming & Linux

  • Thread starter i.mehrzad
  • Start date
  • Tags
    Linux
In summary, the conversation discusses the use of the Linux operating system for programming in C and the lack of the getch() command on Linux. Alternative commands such as getpass() and getchar() are mentioned, with the suggestion to use termios to set the terminal mode for non-canonical input. The conversation also references the book "The C Programming Language" by K&R and discusses the operating system dependency for setting the terminal mode.
  • #1
i.mehrzad
84
0
I use the linux operating system. And at present programming in C.
The getch() doesn't seem to exist on Linux. I know that it is a DOS based command. What is the substitute for getch() on Linux.


I mean is there any command out there which can take my input without displaying the output. I tried the getpass() command but it doesn't work fine

Can someone please help.
 
Technology news on Phys.org
  • #2
getchar(), although if you need each character at a time without waiting for the enter key you might have to set the terminal mode.
Remember unix is written in C and C was invented for unix so any good C book (such as K&R) will apply directly to unix/linux
 
  • #3
Can you please repeat the name of the book. I mean i don't know the acronym
 
  • #4
http://en.wikipedia.org/wiki/The_C_Programming_Language_(book )
It isn't the easiest intro to programming book, but if you have a background it is a good way to learn C.
 
Last edited by a moderator:
  • #5
I don't have my example at hand. You have to use termios to set the terminal (tty) to non-canonical mode, read a keystroke, then reset the tty to canonical mode.

In other words it is OS dependent - the example I gave was unix-based. Windows has a call - _getche() as I remember. You can set the tty mode in windows as well.
 

FAQ: Finding a Substitute for getch() on Linux | C Programming & Linux

What is getch() and why is it not available on Linux?

getch() is a function in the C programming language that retrieves a single character from the keyboard. It is not available on Linux because it is a non-standard function that is specific to the Windows operating system.

What is the best alternative to getch() on Linux?

The best alternative to getch() on Linux is the getchar() function, which is a standard C function that also retrieves a single character from the keyboard.

How do I use getchar() as a substitute for getch() on Linux?

To use getchar() as a substitute for getch() on Linux, you can simply replace the getch() function with getchar() in your code. They have the same functionality and syntax, so no other changes are necessary.

Are there any other alternatives to getch() on Linux?

Yes, there are other alternatives to getch() on Linux, such as fgetc() or scanf(). However, these functions may have slightly different functionality or syntax compared to getch() and may require some modifications to your code.

Can I use a different programming language besides C to find a substitute for getch() on Linux?

Yes, you can use a different programming language besides C to find a substitute for getch() on Linux. For example, in Python, you can use the getpass() function from the getpass module to retrieve user input from the keyboard without displaying it on the screen.

Back
Top