【总结】关于scanf("%d %d\n")中的\n

今天遇到\n这个问题,结果我发现我并不十分明白啊,于是学习自这位博主的博客

先举个例子:

如果输入1 2 3 4 5 6,想一想得到的答案会是什么?

#include<cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    int c,d,x,y,z;
    scanf("%d %d\n",&c,&d);
    printf("c = %d d = %d\n",c,d);//1
    scanf("%d%d%d",&x,&y,&z);
    printf("c = %d d = %d x = %d y = %d z = %d\n",c,d,x,y,z);//2
    return 0;
}

输出: 

输完c,d不会马上显示出来,要等到再接收到一个非 空格、制表符、回车的东西才能显示,最后输入的那个非 空格、制表符、回车的东西是不会被显示的,而是保留在输入流中。

因此输完1,2的时候没有立刻输出1 2,再输入3以后得到第一个printf“1 2”,

又因为3保留在输入流中,所以第二个printf输出12345~

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/82864977