C简介:连续输入提示

And*_* Hu 0 c loops prompt

我想在ANSI C中创建一个提示用户输入的代码.首先,我想打印提示.然后,我想回显提示和循环,仅在键入和回显字符时.

我知道要创建一个无限循环,我必须将while()的条件设置为始终为true的东西,就像while(1)

但是,当我测试我的代码时,我似乎一遍又一遍地循环打印"输入一个字符".有关C新手的任何提示吗?

#include <stdio.h>

int main()
{

char mychar;

while(1)
    printf("Enter a characater:\n");
    scanf("%c", &mychar);
    printf("%c", &mychar)
}
Run Code Online (Sandbox Code Playgroud)

Kir*_*rov 8

C中的块不是基于缩进(例如在python中).Block必须以#开头{}以此结束,如下所示:

while(1)
{
    printf("Enter a characater:\n");
    scanf("%c", &mychar);
    printf("%c", mychar);// NOTE the missing `&` in front of `mychar` here
}
Run Code Online (Sandbox Code Playgroud)