首页 > 代码库 > while( c= getchar(c) &&c!='\n')为什么错误

while( c= getchar(c) &&c!='\n')为什么错误

运算顺序有关,详见 运算符优先级

代码1:

#include<iostream>using namespace std;int main(){	char c;	int m=0;	while( c= getchar(c) &&c!=‘\n‘)	{		m++;		cout<<"case:"<<m<<endl;		cout<<c<<endl;	}	return 0;}


 

代码2:

#include<iostream>using namespace std;int main(){	char c;	int m=0;	while( (c= getchar(c)) &&c!=‘\n‘)	{		m++;		cout<<"case:"<<m<<endl;		cout<<c<<endl;	}	return 0;}