The author has 5 posts.
Font size: Small - 100% (Default)  Content converter: No conversion
 
Clicks Replies
1409 5
【程序】C语言输入密码显示*
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 1 Posted at: 6/19/16 12:36
#include <conio.h>
#include <stdio.h>

void main()
{
    char buf[100];
    int i = 0;
    printf("请输入密码: ");
    while ((buf[i] = _getch()) != '\r')
    {
        putchar('*');
        i++;
    }
    buf[i] = '\0';

    printf("\n密码是: %s\n", buf);
}
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 2 Posted at: 6/19/16 12:41
【增加退格功能】
#include <conio.h>
#include <stdio.h>

void main()
{
    char buf[100];
    int i = 0;
    printf("请输入密码: ");
    while ((buf[i] = _getch()) != '\r')
    {
        if (buf[i] == '\b')
        {
            putchar('\b');
            putchar(' ');
            putchar('\b');
            i--;
        }
        else
        {
            putchar('*');
            i++;
        }
    }
    buf[i] = '\0';

    printf("\n密码是: %s\n", buf);
}
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 3 Posted at: 6/19/16 12:44
【增加了防止退格键把“请输入密码"清除的功能】
#include <conio.h>
#include <stdio.h>

void main()
{
    char buf[100];
    int i = 0;
    printf("请输入密码: ");
    while ((buf[i] = _getch()) != '\r')
    {
        if (buf[i] == '\b')
        {
            if (i == 0)
                continue;
            putchar('\b');
            putchar(' ');
            putchar('\b');
            i--;
        }
        else
        {
            putchar('*');
            i++;
        }
    }
    buf[i] = '\0';

    printf("\n密码是: %s\n", buf);
}
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 4 Posted at: 6/19/16 12:44
【运行效果】
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 6 Posted at: 12/29/25 10:51
windows系统下输入密码一般显示成星号✳️。
linux系统的终端下输入密码什么都不显示。
Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
(Shortcut key: Ctrl+Enter)
Post Information
Clicks: 1409 Replies: 5
Author: 巨大八爪鱼
Last reply: 巨大八爪鱼
Last reply time: 12/29/25 10:51
Announcements