To enter password without displaying it

This is a discussion on To enter password without displaying it within the C Programming forums, part of the General Programming Boards category; In a password checking program,when the password is enterd it should not be displayed.Which function should be used for that? ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    12

    To enter password without displaying it

    In a password checking program,when the password is enterd it should not be displayed.Which function should be used for that? If scanf() and gets() are used the text entered will be displayed.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    I don't believe there is a way in standard C to do that. You can use a nonstandard function such as getch to do what you want.
    Last edited by kermit; 01-07-2009 at 09:09 AM.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    In Windows, you can turn off echo for any standard input function with SetConsoleMode().
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main()
    {
        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
        DWORD mode = 0;
        char buff[25];
    
        GetConsoleMode(hStdin, &mode);
        SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
    
        fgets(buff, sizeof(buff), stdin);
    
        SetConsoleMode(hStdin, mode);
    
        printf("You entered: %s", buff);
    
        return 0;
    }
    In *nix you can use the "termios" interface to disable echo.
    Code:
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    
    int main()
    {
        termios oldt;
        tcgetattr(STDIN_FILENO, &oldt);
        termios newt = oldt;
        newt.c_lflag &= ~ECHO;
        tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    
        fgets(buff, sizeof(buff), stdin);
    
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    
        printf("You entered: %s", buff);
    
        return 0;
    }
    gg

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    This is a "classic" question. Search the forum and/or FAQ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Replies: 2
    Last Post: 01-07-2009, 09:35 AM
  3. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2003, 11:23 PM
  4. how to enter a password with asterisks
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-06-2001, 11:38 AM
  5. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 06:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21