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.
 

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

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

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

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

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

Similar threads

  • Programming and Computer Science
Replies
1
Views
495
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
Replies
3
Views
322
Replies
6
Views
1K
Replies
10
Views
2K
  • Computing and Technology
Replies
18
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top