user-0 提问: scanf和getchar的区别

回复:
user: user-0
content: 引用 15 楼 赵4老师的回复:[quote=引用 13 楼 qq_36496856 的回复:] [quote=引用 5 楼 zhao4zhong1 的回复:] //请今后要用 int c; scanf("%c",&c); //时，都改为 char s[2]; int c; scanf("%1s",s); c=s[0];为什么啊[/quote] Format Specification Fields: scanf and wscanf Functions A format specification has the following form:  % [width] [{h | l | I64 | L}]type  The format argument specifies the interpretation of the input and can contain one or more of the following:   White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.   Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates.   Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.  The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read.  When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.  An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.  Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.   The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.  An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored. [/quote] 是哪里找的啊?是某本书上的吗?

user: user-1
content: MSDN98_1.ISO http://pan.baidu.com/s/1dDF41ix,  MSDN98_2.ISO http://pan.baidu.com/s/1bnGo0Vl

user: user-1
content: 引用 13 楼 qq_36496856 的回复:[quote=引用 5 楼 zhao4zhong1 的回复:] //请今后要用 int c; scanf("%c",&c); //时，都改为 char s[2]; int c; scanf("%1s",s); c=s[0];为什么啊[/quote] Format Specification Fields: scanf and wscanf Functions A format specification has the following form:  % [width] [{h | l | I64 | L}]type  The format argument specifies the interpretation of the input and can contain one or more of the following:   White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.   Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates.   Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.  The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read.  When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.  An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.  Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.   The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.  An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.

user: user-2
content: 我说的读入是指程序不能获取并判断用户输入的是回车、空格还是换行符，因为当输入这三个字符时scanf便会去缓冲区取数据，同时当遇到这三个字符时便停下来了，同时不会取这三个字符。  而getchar是能够取这三个字符的，也即用getchar是能判断用户输入的是回车还是空格和tab，而用scanf则就只会把这三个字符当做判断输入结束的标志

user: user-3
content: getchar()重点在于char，你可以通过该函数得到stream中的下一个character，无论这个字符是空白还是控制字符还是常规的字符； 而scanf()重点在于f，这个f指的是format，这就意味着它按照你给的“格式”，从stream中提取出你需要的东西，但往往出现的情况是你的format和实际的stream其实是不匹配的，然后你就感觉scanf工作起来“怪怪”的。

user: user-4
content: http://blog.csdn.net/Ziv2086581691/article/details/51979841?locationNum=3&fps=1

user: user-2
content: scanf不可读入回车和空格，getchar可以

user: user-5
content: 出错估计是由于你缓冲区中留下的\n造成

user: user-5
content: scanf读取不了空白符（如\n \r \t ‘ ’），getchar()可以

user: user-6
content: scanf %c和getchar()都是读一个字符（而且都可以读回车符之类的） 楼主建议贴一下代码。

user: user-1
content: //请今后要用 int c; scanf("%c",&c); //时，都改为 char s[2]; int c; scanf("%1s",s); c=s[0];

user: user-0
content: 引用 1 楼 取个名字这么麻烦的回复:scanf不可读入回车和空格，getchar可以 说错了，，scanf是可以读空格的，在输入字符时。     你说的忽略是在输入别的东西时，比如字符串，整数

user: user-0
content: 引用 1 楼 取个名字这么麻烦的回复:scanf不可读入回车和空格，getchar可以 还有tab,还是谢谢了

user: user-0
content: 引用 5 楼 zhao4zhong1 的回复://请今后要用 int c; scanf("%c",&c); //时，都改为 char s[2]; int c; scanf("%1s",s); c=s[0];为什么啊

user: user-0
content: 题目：Arithmetic expression can be defined recursively as following:  If a is a positive integer number, then a is an arithmetic expression. If a is a negative integer number, then (a) is an arithmetic expression. If e and f are arithmetic expressions, then (e+f), (e*f),(e-f),and (e/f) are all arithmetic expressions. The expression ((6*(-7))+(-9)) is an example of arithmetic expression, and its value is -51. Write a program that accepts an arithmetic expression represented as a string, output its value as an integer. Input  A string representing an arithmetic expression on one line. Output  The value of the arithmetic expression on one line. Sample Input  ((6*(-7))+((-9)/3)) Sample Output  -45 我代码：#include <stdio.h>   int main(void) {     int i, j, str[100];// i代表')'出现的位置， j代表'('出现的位置      char ch;          while( ( str[0]= getchar() ) && str[0] != EOF )//这里改成scanf就不可以，他测评的时候。总在它输出最后一个例子的时候     {     	/* 开始输入剩余的字符 */        if( str[0] != '(' )       {       	continue; 		}     	for( i = 1;( str[i] = getchar() ) != '\n' ;i++ )     	{	      	   /* 寻找‘）’的字符 ， 并且进行一定的操作 */ 			ch = str[i];     	   if( ch == 26 || ch == EOF)     	      break; 			if( str[i] == ')' )  			{ 				for( j = i - 3;j >= 0;j-- ) 				{ 					if( str[j] == '(') 					   break; 				}// 从出现')'的地方逆回去寻找'('  				 			/* 对'(' 和')'之间的数字进行计算 ，并且将计算的结果赋值给str[j],然后i = j */   				if( i - j == 3) 				{ 					str[j] = -(str[j + 2] - 48) + 48; 				} 				else 				{ 					switch( str[j + 2] ) 					{ 						case '+': 							str[j] = ( str[j + 1] - 48) + ( str[j + 3] - 48) + 48;break; 						case '-': 							str[j] = ( str[j + 1] - 48) - ( str[j + 3] - 48) + 48;break;	 						case '*': 							str[j] = ( str[j + 1] - 48) * ( str[j + 3] - 48) + 48;break; 						case '/': 							str[j] = ( str[j + 1] - 48) / ( str[j + 3] - 48) + 48;break;		 					} 				} 				 				i = j;	 	    		if( i == 0 ) 			      printf( "%d\n", str[i] - 48 ); 			 } 		  		 } 		  	 if( ch == EOF ) 	    break;	  	 }     return 0; } // 有些代码是无用的

user: user-0
content: [code=c][/code ]#include<stdio.h>  int main() {    char ch;        while( scanf( "%c", &ch) != EOF )    {    	printf( "%c\n", ch); 	} 	return 0;  // scanf可以读空格的  }