scanf在前getchar后一起使用,发现getchar没有被执行,为什么?请大家指教。 scanf后用getchar缓存问题.
\u4e3a\u4ec0\u4e48\u8f93\u5165\u5b57\u7b26\u578b\u5b57\u6bcd\u8981\u540c\u65f6\u8fd0\u7528scanf\u548cgetchar\u624d\u80fd\u6210\u529f\uff1f\u56e0\u4e3ascanf("%c", &ch);\u53ea\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\uff0c\u4f1a\u628a\u8f93\u5165\u7684'\n'\u7559\u5728\u8f93\u5165\u7f13\u51b2\u533a\uff1b\u800c'\n'\u4e5f\u662f\u5b57\u7b26\uff0c\u540e\u9762\u7684\u8f93\u5165\u8bed\u53e5\u4f1a\u628a\u5b83\u63a5\u6536\u800c\u6536\u4e0d\u5230\u4f60\u8f93\u5165\u7684\u6709\u7528\u5b57\u7b26\uff0c\u6240\u4ee5\u4f1a\u51fa\u9519\u3002\u5199\u6210scanf("%c", &ch);getchar();\u540egetchar();\u4f1a\u628a\u9057\u7559\u5728\u7f13\u51b2\u533a\u4e2d\u7684\u90a3\u4e2a'\n'\u63a5\u6536\u6389\uff0c\u5c31\u4e0d\u4f1a\u51fa\u9519\u4e86\u3002\u4f46\u4f60scanf("%c", &ch);ch=getchar();\u8fd9\u6837\u5199\u4e5f\u4e0d\u884c\uff0cch\u5c31\u53ea\u662f\u4e2a'\n'\u4e86\uff0c\u540e\u9762\u5c31\u7528\u4e0d\u6210ch\u4e86\uff0c\u628ach=\u53bb\u6389\u5c31\u53ef\u4ee5\u4e86\u2026\u2026
\u4f60\u8f93\u5165 \u5230\u7f13\u51b2\u533a\u7684\u6709 "1\n" \u4f46\u662f scanf("%d",&n); \u53ea\u4f1a\u8bfb\u53d6 "1"
\u56de\u8f66\u8fd8\u5728\u7f13\u51b2\u533a\u4e2d\uff0c \u63a5\u7740\u4f60\u7684 gets() \u51fd\u6570 \u5c31\u4f1a \u8bfb\u53d6\u7f13\u51b2\u533a\u76f4\u5230\u9047\u5230 \u56de\u8f66 \u4e3a\u6b62 \uff0c\u6240\u4ee5\u7b2c\u4e00\u4e2a gets() \u521a\u597d\u628a\u7559\u4e0b\u7684 \u56de\u8f66 \u8bfb\u8d70\u4e86\u3002\u3002\u3002
\u7136\u540e\u5c31\u662f\u540e\u9762\u53d1\u751f\u7684\u4e8b\u4e86\u3002
\u8fd9\u4e2a\u548c\u5177\u4f53\u7684\u8f93\u5165\u5185\u5bb9\u76f8\u5173
%s \u7684\u8bdd\uff0c\u8bfb\u53d6\u7f13\u51b2\u533a\u76f4\u5230 \u7a7a\u683c\u56de\u8f66\u7b49\u7a7a\u767d\u5b57\u7b26\uff0c\u540c\u65f6\u4f1a\u628a\u8fd9\u4e2a\u7a7a\u767d\u5b57\u7b26\u6e05\u9664
%d\uff0c%f\u7684\u8bdd\uff0c\u8bfb\u53d6\u7f13\u51b2\u533a \u76f4\u5230 \u975e\u6570\u5b57\uff0c\u4f46\u662f\u4e0d\u4f1a\u6e05\u9664\u540e\u9762\u7684\u8fd9\u4e9b\u5b57\u7b26
其实getchar执行了,它读取缓存中的回车符,输入时scanf只读了3 4,回车留在缓存中了.输出时也应该输出了回车
再来一个c=getchar();
看行不行 回车影响
#include <stdio.h>
#include <stdlib.h>
main()
{ int a,b;
char c;
clrscr();
scanf("%d%d",&a,&b);
printf("test\n");
fflush(stdin);
c=getchar();/* scanf("%d",&c); */
printf("a=%d,b=%d,c=%c\n",a,b,c);
}
绛旓細绗竴涓scanf璇诲叆瀛楃涓插悗锛屾崲琛岀鐣欏湪杈撳叆缂撳啿鍖轰腑锛岃绗簩涓猻canf璇诲叆锛坰canf鐨勬牸寮忓瓧绗︿覆涓%c鏃朵細璇诲彇涓鍒囧瓧绗﹀寘鎷崲琛屽拰鍥炶溅锛夛紝鍥犺岄犳垚閿欒銆傚彲鍦ㄧ涓涓猻canf缁撴潫鍚庡姞鍏ヤ竴琛寃hile(getchar()!='\n');娓呯┖杈撳叆缂撳啿鍖恒
绛旓細getchar()鐢ㄦ潵璇诲彇涓涓换鎰忓瓧绗︺scanf()鐢ㄦ潵璇诲彇鏁版嵁锛屼綘杩欓噷鍐欎簡"%d %d"锛屾墍浠ヨ鐨勬槸涓や釜鏁存暟銆傝鍙栨暣鏁扮殑鏃跺欙紝浣犺緭鍏ヤ簡瀛楁瘝锛屽綋鐒惰涔辫窇浜嗐
绛旓細1閭d釜鍥炶溅绗︽病鏈変即闅忕潃scanf璇彞鐨勭粨鏉熻屾秷澶憋紱2scanf璁や负鍥炶溅绗︼紝绌烘牸绗︽槸鍒嗛殧绗︼紝浼氳嚜鍔ㄨ烦杩囷紝鍥犺屼笉浼氬奖鍝嶄笅涓涓猻canf銆3getchar鏄鍏ヤ竴涓瓧绗︼紝涓嶇鏄笉鏄垎闅旂锛岄兘涓嶄細璺宠繃銆
绛旓細scanf鍜getchar鐨勫尯鍒湁鍥涚鍒嗗埆鏄細鍑芥暟鏍煎紡涓嶅悓銆佽鍙栨柟寮忎笉鍚屻佺粨鏉熻緭鍏ョ殑鏂瑰紡涓嶅悓鍜岃垗寮冨洖杞︾鐨勬柟娉曚笉鍚屻備竴銆佸嚱鏁版牸寮忎笉鍚 scanf鍑芥暟鏄牸寮忚緭鍏ュ嚱鏁帮紝鍗虫寜鐢ㄦ埛鎸囧畾鐨勬牸寮忎粠閿洏涓婃妸鏁版嵁杈撳叆鍒版寚瀹氱殑鍙橀噺涓俫etchar鍑芥暟鏄敭鐩樿緭鍏ュ嚱鏁帮紝鍏跺姛鑳芥槸浠庨敭鐩樹笂杈撳叆涓涓瓧绗︺備簩銆佽鍙栨柟寮忎笉鍚 scanf鍑芥暟鍦...
绛旓細scanf鍙互杈撳叆鎵鏈夌被鍨嬬殑鏁版嵁锛岃実ets鍙兘杈撳叆瀛楃涓诧紱褰scanf鍦ㄥ墠闈鐨勬椂鍊欒緭鍏ユ暟鎹拰鍚庨潰鐨勫瓧绗︿覆涓棿浼氱敤绌烘牸鍒嗗紑锛岃繖鏃秅ets灏辨棤娉曞尯鍒嗚繖涓┖鏍兼槸鍚﹀睘浜巊ets鐨勮緭鍏ャ備綘鍦scanf鍚庨潰鍔犱竴涓getchar锛堬級锛涗細瑙e喅闂鐨勩
绛旓細濡傛灉鏄笅闈㈢殑杩欑鎯呭喌锛屽氨闇瑕getchar()浜 int a;char c;scanf("%d", &a);scanf(%c", &c);鑰岃緭鍏ユ槸锛1001 鏁插洖杞 @ 鏁插洖杞 鍏堣緭鍏ユ暣褰紝鐒跺悗杈撳叆瀛楃锛屽鏋滃湪绗竴涓scanf涔嬪悗锛屾病鏈塯etchar鐨勮瘽锛岄偅涔坈鎺ユ敹鐨勫埌鏄洖杞﹀瓧绗︼紝鑰屼笉鏄瓧绗'@'scanf("%d", &a);getchar();scanf(%c", &...
绛旓細浣犲鏋滃湪scanf鍓嶉潰鍔犱釜gerchar(),閭h偗瀹氭槸绗竴涓瓧绗︿笉鏄剧ず鍟婏紝鍥犱负浣犵涓涓瓧绗﹀埌浜getchar()閲岄潰浜嗐傚彲鏄垜涓嶆槑鐧戒綘鏈変粈涔堝繀瑕侀潪瑕佸湪scanf鍓嶉潰鍔爂etchar()锛屽嵆浣夸綘鐢╣etchar()锛岄偅涔熷簲璇ユ妸getchar寰楀埌鐨勫煎瓨鍦ㄤ竴涓彉閲忛噷闈紝姣斿a=getchar()鎵嶆湁鎰忎箟锛屽惁鍒欏氨鏄崟绾殑鍚炴帀涓涓瓧绗 ...
绛旓細鍓嶄袱涓棶棰樿緭鍑虹粨鏋滄槸涓鏍风殑銆傚綋鐒讹紝ch=getchar()锛涘簲鏀逛负ch[i]=getchar();甯︾┖鏍兼椂锛岃鎶婃帶鍒跺惊鐜彉閲忔敼涓20锛鍚庨潰鐨勯棶棰樺簲璇ユ槸scanf锛堚%s鈥濓紝str锛夛紱涓巊ets锛坰tr锛夛紱鐨勫尯鍒紝姝ゆ椂锛宻canf涓┖鏍笺佸洖杞︺佽烦鏍奸兘琛ㄧず缁撴潫锛岃実ets涓负涓涓瓧绗 ...
绛旓細鐢变簬鍓嶉潰鎵ц杩 scanf("%d",&pg->e); 杩欑璇彞锛宻canf 璇讳竴涓 int 鍚庯紝杈撳叆缂撳啿鍖洪噷娈嬬暀浜 Enter 閿紝getchar(); 璇诲彇鍒 杩欎釜 娈嬬暀 鐨 Enter 閿傛墍浠ョ湅璧锋潵鍍忔槸璺宠繃浜嗐傛竻闄よ緭鍏ョ紦鍐插尯鍙互 鐢 fflush(stdin);渚嬪锛歴canf("%d",&pg->e); fflush(stdin); printf("璇疯緭鍏ュ浘鐨勯《鐐逛俊鎭...
绛旓細鍥犳锛屽湪涓嬩竴涓鈥滃瓧绗︹濇搷浣滃嚱鏁帮紙getchar, scanf("%c"), gets()绛夛級杩愯鏃讹紝浼氳鍒拌繖涓瓧绗︺傝屽湪璇绘暟鍊煎瀷鏁版嵁鎴栧瓧绗︿覆鏃讹紝scanf()浼氫粠绗竴涓潪绌虹櫧瀛楃锛堢┖鐧藉瓧绗︽寚锛氬洖杞︼紝绌烘牸锛孴AB閿級寮濮嬭鍙栵紝鑷姩蹇界暐鍓嶉潰鐨勭┖鐧藉瓧绗︼紝鑰岄亣鍒扮┖鐧藉瓧绗︾粨鏉熻绫诲瀷鏁版嵁鐨勮緭鍏ャ傚洜姝わ紝鏄惁涓瀹氳鍦scanf鍚庨潰璺...