getch

用getchar时,在键盘上按一个字符后,要按回车才能读取进去;用getch时,在键盘上按一个字符马上就被读取进去,不用按回车,因此可以作为“按任意键继续”的执行语句。
 
getchar有一个int型的返回值.当程序调用getchar时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中.直到用户按回车为止(回车字符也放在缓冲区中).getchar函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕.如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取.也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键.
getch与getchar基本功能相同,差别是getch直接从键盘获取键值,不等待用户按回车,只要用户按一个键,getch就立刻返回,getch返回值是用户输入的ASCII码,出错返回-1.输入的字符不会回显在屏幕上.getch函数常用于程序调试中,在调试时,在关键位置显示有关的结果以待查看,然后用getch函数暂停程序运行,当按任意键后程序继续运行.

简单的说,getch()是读取按键值常放在程序末尾起暂停作用而getchar()是从标准输入设备读取下一个字符~~所读字符若文件结束或出错则返回-1
 
getchar

    This is a standard function that gets a character from the stdin.

getch

    This is a nonstandard function that gets a character from keyboard, does not echo to screen.

 getche

    This is a nonstandard function that gets a character from the keyboard, echoes to screen. 

Use getchar if you want it to work on all compilers. Use getch or getche on a system that supports it when you want

keyboard input without pressing [Enter].And note that the return value of all three is int! You need this to properly check for EOF.

void main()
{
    int i;
    char tmp[20];
    for (i = 0; i<20; i++)
    {
        tmp[i] = _getch();      //关键点1
                               // tmp[i]=getchar();

                               // putchar ( '*' ) ;
        printf("%d=[%c]\n", i, tmp[i]);
        if (tmp[i] == '\r')
        {
            tmp[i] = '\0';     //关键点2
            break;
        }
    }

}

'\r' ,“shift+回车键” 就能输入手动换行符

posted @ 2016-07-11 11:44  JHarden  阅读(255)  评论(0编辑  收藏  举报