I want to get a string (with space) from user but I don't want it to display on the console. Is there any way in the C language that makes it possible?
Asked
Active
Viewed 2,969 times
3
-
3Not in standard C. For all you know, there might not even be a console. – chris Jul 18 '14 at 17:09
-
Maybe have a look at (n)curses, or give the platforms, where your program should run. – mafso Jul 18 '14 at 17:28
-
The closest you'll get to anything standard is `getpass`. See this question on possible alternatives: http://stackoverflow.com/questions/1196418/getting-a-password-in-c-without-using-getpass-3 – Ross Ridge Jul 18 '14 at 17:31
-
[graphics] seems a rather inappropriate tag here. Found nothing related to console input? – Jongware Jul 18 '14 at 17:50
2 Answers
4
I guess you want to disable echoing characters typed by user. You can do that by setting the terminal attributes using functions from <termios.h>
like this:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#define MAX_LINE_SIZE 512
int main (int argc, char *argv[])
{
/* get terminal attributes */
struct termios termios;
tcgetattr(STDIN_FILENO, &termios);
/* disable echo */
termios.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &termios);
/* read a line */
char line[MAX_LINE_SIZE] = { 0 };
fgets(line, sizeof(line), stdin);
/* enable echo */
termios.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &termios);
/* print the line */
printf("line: %s", line);
return 0;
}
The example above reads one line (without echoing the characters back) and then it just prints the line back to the terminal.

Dmitry
- 1,995
- 1
- 11
- 10
-
-
Sorry, but it is for a Unix environment. The C language is rare for Windows at these days, so I thought you have a Linux environment or something similar. Windows doesn't have
(at least), perhaps you may find an equivalent here: http://stackoverflow.com/questions/933745/what-is-the-windows-equivalent-to-the-capabilities-defined-in-sys-select-h-and-t – Dmitry Jul 18 '14 at 18:08 -
-
it works properly if the original termios is stored and restored. This requires two termios structs. Your version fails to display my text afterward. – Barny Aug 31 '22 at 03:57
3
You can implement your own version of getch
like so, and mask it. The following example spits out the input just for logging/testing purposes. You can disable that by removing the printf
calls.
Good luck!
Code Listing
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
int getch();
int main(int argc, char **argv) {
int ch;
printf("Press x to exit.\n\n");
for (;;) {
ch = getch();
printf("ch = %c (%d)\n", ch, ch);
if(ch == 'x')
break;
}
return 0;
}
int getch() {
struct termios oldtc;
struct termios newtc;
int ch;
tcgetattr(STDIN_FILENO, &oldtc);
newtc = oldtc;
newtc.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newtc);
ch=getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldtc);
return ch;
}
Sample Run
gcc test.c && ./a.out
Press x to exit.
ch = a (97)
ch = b (98)
ch = c (99)
ch = 1 (49)
ch = 2 (50)
ch = 3 (51)
ch =
(10)
ch =
(10)
ch = x (120)
References
- Hide password input on terminal, Accessed 2014-07-18,
<https://stackoverflow.com/questions/6856635/hide-password-input-on-terminal>