与fgets
一起使用时,stdin
的行为未被cout
阻止。
/* Understanding fgets behaviour */
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main() {
char A[20], B[100];
cin>>A;
cout<<strlen(A)<<" = Length of A\n";
// In this case programme stops for taking input for B
//cin>>B;
//cout<<strlen(B)<<" = Length of B\n";
/* But in this case , programme will
* not stop for taking input for B and
* exit with below output.(Mentioned end of the programme.)
*/
fgets(B,100,stdin);
cout<<strlen(B)<<" = Length of B\n";
return 0;
}
结果:
input:
asdf
output:
4 = Length of A
1 = Length of B