一段小程序理解getchar和putchar

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
   char c,d,e,f; 

    printf("please input two characters:
"); 

    c=getchar(); 
    putchar(c); 
    putchar('
'); 
    d=getchar(); 
    putchar(d); 
    putchar('
'); 
    e=getchar(); 
    putchar(e); 
	putchar('
'); 
    f=getchar(); 
    putchar(f); 
    putchar('
'); 

	printf("c= %c
",c); 
    printf("d= %c
",d); 
    printf("e= %c
",e); 
    printf("f= %c
",f);

    return 0;
}

输出截图

解释如下:

函数每次从缓冲区中得到一个字符,
putchar函数每次输出一个字符。
首先输入了两个字符12,然后回车,注意这时写入缓存中的有3个字符1,2,回车。
程序中有四个getchar(),于是c='1',d='2',e=' '
这里输入了34 于是f='3',4和后面的回车没有被利用。
这便是整个流程。 

原文地址:https://www.cnblogs.com/clc2008/p/3739054.html