·¢ÏÖÊäÈëÁËÎå¸öÊý¾Ýºó³ÌÐò²¢Ã»ÓнáÊø£¬¶øÊÇÒªÔÙÊäÈëµÚÁù¸öÊý²Å»áÖ´ÐÐÏÂÃæµÄ´òÓ¡Êä³ö´úÂ룬ÕâÊÇÔõô»ØÊ£¿ËäÈ»´Ó×îºóµÄÊä³ö½á¹ûÀ´¿´£¬Êý¾Ý±»È·ÕýȷдÈëÁË£¬µ«Õâ¶àµÄÒ»´ÎÊäÈëÊÇ´ÓÄÄð³öÀ´µÄÄØ£¿
scanf() ÓëÊäÈ뻺³åÇø
ÎÒÃDz»Ê±»áÌýµ½¡¸»º³åÇø¡¹µÄ¸ÅÄ˵°×ÁË£¬ÊäÈ뻺³åÇø¾ÍÊǼüÅÌ°´Ïºó¼üÖµµÄÖ±½ÓÈ¥´¦£¬Ò²ÊÇ scanf() ¡¢getchar() µÈº¯ÊýµÄÊý¾ÝÀ´Ô´¡£¶øÇÒ¡¸º¯Êý´Ó»º³åÇøÖжÁ×ß×Ö·û¡¹ºÍ¡¸¼üÅ̰ѼüÖµ´æµ½»º³åÇø¡¹ÕâÁ½¸ö¹ý³ÌÏ໥¶ÀÁ¢¡¢»¥²»¸ÉÈÅ£¬¼´¡¸Äã´æÄãµÄ£¬ÎÒÈ¡Îҵġ¹£¬µ±»º³åÇøΪ¿Õʱ£¬scanf() ´¦ÓڵȴýÊäÈë״̬£¨¼´µ÷Óà scanf() º¯ÊýºóɶҲûÊäʱ£©¡£
scanf() ¹¤×÷Á÷³Ì
scanf() º¯Êý¿´ËÆÓë printf() º¯ÊýµÄÓ÷¨²î²»¶à£¬µ«Êµ¼ÊÉÏ scanf() Òª¸üΪ¸´ÔÓ¡£scanf() º¯ÊýµÄ´óÖ¹¤×÷Á÷³ÌÈçÏ£º
´Ó×óµ½ÓÒ´¦Àí¸ñʽ×Ö·û´®
Óöµ½×ª»»ËµÃ÷·û£¨Èç %d£©ºó£¬scanf() ³¢ÊÔ´ÓÊäÈëµÄÊý¾Ý£¨À´×ÔÊäÈ뻺³åÇø£©Öж¨Î»ÏàÓ¦ÀàÐ͵ÄÏ²¢ÇÒÌø¹ý±ØÒªµÄ¿Õ¸ñ
¶ÁÈ¡Êý¾ÝÏ²¢ÇÒÔÚÓöµ½²»¿ÉÄÜÊôÓÚ´ËÊý¾ÝÏîÀàÐ͵Ä×Ö·ûÍ£Ö¹
ÈôÊý¾ÝÏî¶ÁÈ¡³É¹¦£¬½«ÆäдÈë¶ÔÓ¦±äÁ¿£¨Í¨¹ýÖ¸Õë·ÃÎʱäÁ¿¿Õ¼ä£©
¼ÌÐø´¦Àí¸ñʽ×Ö·û´®µÄÊ£Óಿ·Ö
¸ñʽ×Ö·û´®´¦ÀíÍê±Ï£¬½áÊøµ÷ÓÃ
×¢Òâµ½ scanf() ¶ÁÈëÊý¾Ýʱ»áÌø¹ý±ØÒªµÄ¿Õ°×·û£¨Èç¿Õ¸ñ¡¢»»ÐС¢ÖƱí·ûµÈ£©£¬×¼È·À´ËµÊÇÔÚ¶ÁÈ¡·Ç×Ö·ûÐÍÊý¾Ýʱ²ÅÌø¹ý¡£Ò²¾ÍÊÇ˵ÔÚ¶à¸öÊäÈëÊý¾ÝÖ®¼ä±¾À´¾Í¿ÉÒÔËæ±ã¼Ó¿Õ°××Ö·û£¬¶ÔÓÚÏÂÃæµÄ³£¼û´úÂ룺
#include <stdio.h>
int main()
{
int i, j;
scanf("%d%d", &i, &j);
printf("i = %d, j = %d\n", i, j);
return 0;
}
¸´ÖÆ´úÂë
ÔÚÊäÈëÁ˵ÚÒ»¸öÊýºó£¬¿ÉÒÔÏÈËæ±ãÂÒÇÃÈÎÒâ¸ö¿Õ¸ñ¡¢»Ø³µ»ò»»ÐмüºóÔÙÊäÈëµÚ¶þ¸öÊý£¬³ÌÐòÕÕÑùÕý³£ÔËÐУ¬¼´Ê¹¸ñʽ×Ö·û´®ÖÐµÄ %d%d Êǽô°¤×ŵġ£Õâô˵À´ÎÒÔÚ¸ñʽ»¯×Ö·ûÖÐдµÄ \n ´¿Êô»ÉßÌí×ã¡¡
Õ𾪣¡¸ñʽ´®ÖеĿհ׷û¾¹È»¡¡
ιιιµÈһϣ¬²»¶Ô°¡£¬ÎÒÃ÷Ã÷¼û¹ý scanf("%d\n%d", &i, &j); »òÕß scanf("%d %d", &i, &j); ÕâÖÖ¸ñʽ´®Öдø¿Õ°×·ûµÄд·¨°¡£¡Êǵģ¬µ±È»¿ÉÒÔ£¬µ«ÕâÀïµÄ¿Õ°×·û²¢Ã»ÓÐÏëÏóÖеÄÄÇô¼òµ¥£º
¿ÉÄÜÁîÈ˳Ծª£¬\nÔÚscanf¸ñʽ´®Öв»±íʾµÈ´ý»»Ðзû£¬¶øÊǶÁÈ¡²¢·ÅÆúÁ¬ÐøµÄ¿Õ°××Ö·û¡££¨ÊÂʵÉÏ£¬scanf¸ñʽ´®ÖеÄÈκοհ××Ö·û¶¼±íʾ¶ÁÈ¡²¢·ÅÆú¿Õ°××Ö·û¡£¶øÇÒ£¬ÖîÈç%dÕâÑùµÄ¸ñʽҲ»áÈÓµôÇ°±ßµÄ¿Õ°×£¬Òò´ËÄãͨ³£¸ù±¾²»ÐèÒªÔÚscanf¸ñʽ´®ÖмÓÈëÏÔʽµÄ¿Õ°×¡££© ¡ª¡ª ¡¶Äã±ØÐëÖªµÀµÄ495¸öCÓïÑÔÎÊÌâ¡·- µÚ12Õ±ê×¼ÊäÈëÊä³ö¿â
ºÎÖ¹ÁîÈ˳Ծª£¬¼òÖ±»ÙÈý¹ÛÄÅ£¡ÔÀ´ scanf() ¸ñʽ´®ÖеĿհ׷û²¢²»±íʾƥÅä´Ë×Ö·û£¬¶øÊÇÖØжÁÈ¡Ò»¸ö·Ç¿Õ°××Ö·û¡£scanf("%d\n%d", &i, &j); »òÕß scanf("%d %d", &i, &j); ÕâÖÖд·¨Öпհ׷ûλÓÚÁ½¸öÊý¾Ýת»»·ûÖ®¼ä£¬ËùÒÔÌåÏÖ²»³öÕâ¸öÆæÝâÌØÐÔ£¨¿Õ°×·ûºó±¾À´¾ÍÒª¶ÁÈ¡Ò»¸ö·Ç¿Õ°×µÄ %d Êý¾Ý£©¡£µ«ÊÇÈç¹û¿Õ°×·ûλÓÚ¸ñʽ´®µÄĩ⣬ÎÊÌâ¾ÍÀ´ÁË ¡ª¡ª ÔÚ°´Ö¸¶¨·½Ê½¶ÁÈ¡ºóÒªÇó¶à¶ÁÒ»¸ö·Ç¿Õ°××Ö·û£¬ÏÂÃæÕâ¶Î¼òµ¥µÄ´úÂë¾ÍչʾÁËÕâÖÖÏÖÏó£º
#include <stdio.h>
int main()
{
int i;
scanf("%d\n", &i);
printf("i = %d\n", i);
return 0;
}
¸´ÖÆ´úÂëÔËÐнá¹û£º
ÔÚÊäÈëµÚÒ»¸öÊý¾Ý 520 ºó£¬¸ñʽ´®Óöµ½ÁË¿Õ°×·û \n£¬´Ëʱscanf() ÈÔÈ»´¦Óڵȴý״̬£¬¼ÌÐø¶ÁÈ¡ÏÂÒ»¸ö·Ç¿Õ°×·ûºó²Å»á½áÊø¡£
½øÒ»²½ÑéÖ¤
ÔÚÁíÒ»±¾Ê顶CÓïÑÔ³ÌÐòÉè¼ÆÏÖ´ú·½·¨¡·ÖÐÒ²Ìáµ½ÁËÀàËƵÄ×¢ÒâÊÂÏ
ËäÈ» printf ¸ñʽ´®¾³£ÒÔ \n ½á⣬µ«ÊÇÔÚ scanf ¸ñʽ´®Ä©Î²·ÅÖû»Ðзûͨ³£ÊÇÒ»¸ö»µÖ÷Òâ¡£¶Ô scanf º¯ÊýÀ´Ëµ£¬¸ñʽ´®ÖеĻ»ÐзûµÈ¼ÛÓÚÒ»¸ö¿Õ¸ñ£¬Á½Õ߶¼»áÒý·¢ scanf º¯ÊýÌáÇ°½øÈëµ½ÏÂÒ»¸ö·Ç¿Õ°×µÄ×Ö·û¡£ÀýÈ磬Èç¹ûÓиñʽ´® "%d\n"£¬ÄÇô scanf º¯Êý½«Ìø¹ý¿Õ°××Ö·û£¬¶ÁÈ¡Ò»¸öÕûÊý£¬È»ºóÌøµ½ÏÂÒ»¸ö·Ç¿Õ°××Ö·û´¦¡£ÏñÕâÑùµÄ¸ñʽ´®¿ÉÄܻᵼÖ½»»¥Ê½³ÌÐòÒ»Ö±¡¸¹ÒÆð¡¹Ö±µ½Óû§ÊäÈëÒ»¸ö·Ç¿Õ°××Ö·ûΪֹ¡£ ¡ª¡ª ¡¶CÓïÑÔ³ÌÐòÉè¼ÆÏÖ´ú·½·¨¡·- 3.2.3 »ìÏý printf º¯ÊýºÍ scanf º¯Êý
Óе㲻¿É˼ÒéÊÇ°É¡¡°ëÐÅ°ëÒɵÄÎÒÓÖдÁËÒ»¸ö³ÌÐò£¬¸üÖ±¹ÛÓÐÁ¦µØÖ¤Ã÷ÁËÕâÒ»µã£º
#include <stdio.h>
int main()
{
int i = 0, j = 0;
scanf("%d\n", &i);
printf("Pause...\n");
printf("1: i = %d, j = %d\n", i, j);
scanf("%d", &j);
printf("2: i = %d, j = %d\n", i, j);
return 0;
}
¸´ÖÆ´úÂëÔËÐнá¹û£º
520
1314
Pause...
1: i = 520, j = 0
2: i = 520, j = 1314
¸´ÖÆ´úÂë¿É¼ûÔÚÊäÈëÍêµÚ¶þ¸öÊý¾Ý 1314 ֮ǰ£¬³ÌÐòһֱͣÔÚµÚÒ»Ìõ scanf() Óï¾ä´¦£¬ÕýÊÇÒòΪ¸ñʽ´®ÖÐµÄ \n µ¼Öµġ¸¹ÒÆ𡹣¬µ±·Ç¿Õ°××Ö·û 1314 ÊäÈëÍê³ÉºóµÚÒ»Ìõ scanf() ²Å½áÊø£¬²¢½«µÚÒ»¸öÊý¾Ý 520 дÈë±äÁ¿ i£¬Ëæºó´òÓ¡ÌáʾÐÅÏ¢ Pause£¬ ±äÁ¿ j Ò²»¹Ã»ÓÐдÈëÖµ¡£Õâʱ»º³åÇøÖÐÊ£ÏÂ×Ö·û 1314£¬ËùÒÔµ±µÚ¶þ¸ö scanf() Ö´ÐÐʱ¿ÉÒÔÖ±½Ó´Ó»º³åÇø¶ÁÈ¡Êý¾Ý²¢Ð´Èë±äÁ¿ j£¬²»ÐèÒªÔÙÊÖ¶¯ÊäÈë¡£
ÔÙ»ØÍ·¿´ÎÄÕ¿ªÊ¼´¦µÄÊý×éÁ¬Ðø¸³Öµ´úÂ룬½« for Ñ»·ÖÐµÄ 5 ´Î scanf() ¸ñʽ´®×éºÏ£¬µÈ¼ÛÓÚ "%d\n%d\n%d\n%d\n%d\n"£¬¸ù¾ÝÇ°ÃæµÄ·ÖÎö£¬Öм䱻 %d ¼Ð×ÅµÄ \n ¶ÔÊäÈëûÓÐÓ°Ï죬¶ø×îºóµÄ \n »á¶àµÈ´ýÒ»¸ö·Ç¿Õ°×·û£¬µ¼ÖÂÊäÈëÊý¾Ýʱ¶àÁËÒ»´Î£¬ÕæÏà´ó°×¡£
ҪʹÕâ¸ö³ÌÐòÕý³£Ö´ÐУ¬¿É½« for Ñ»·ÖÐÊäÈëÓï¾ä¸Ä³É scanf("\n%d", &a);£¬Ö»ÒªÈ·±£¸ñʽ´®×îºóÒ»¸ö×Ö·û²»ÊÇ¿Õ°×·û¼´¿É¡£
×ܽ᣺ÀûÓà scanf() º¯Êý¶ÁÈ¡¶à¸öÊý¾Ýʱ£¬ÈôÒÔ¿Õ°×·û·Ö¸ô£¬¿ÉÔÚ¸ñʽ´®ÖÐÊ¡ÂÔ²»Ð´£¨Èç¹ûÊÇÆäËû¶ººÅʲôµÄµ±È»ÒªÐ´À²£©£¬¾ÍËãҪдҲ²»ÒªÐ´ÔÚ¸ñʽ´®×îºó°¡°¡°¡£¡£¡£¡
¶Áȡʧ°Ü¡¢»º³åÇø²ÐÁôÎÊÌâ
ÔÚÉÏÃæ×îºóÒ»¸öÀý×ÓÖУ¬ ÊäÈ뻺³åÇøÖÐµÄ 520 ºÍ 1314 ·ÖÁ½´Î±» scanf() º¯Êý¶Á×ߣ¬¼´ scanf() º¯Êý²»ÊÇÖ±½Ó¶Á¼üÅÌ£¬¶øÊǼä½Ó´ÓÊäÈ뻺³åÇøÖжÁÈ¡Êý¾Ý£¬²¢ÇÒÒ»´Îû¶ÁÍê¿ÉÒÔÁô×ÅÏÂÒ»´Îµ÷ÓÃʱÔÙ¶Á£¬ÕâÈÃÎÒÏëÆð֮ǰÓöµ½µÄÁíÒ»¸öÎÊÌâ ¡ª¡ª scanf() ·Ç·¨ÊäÈëµ¼ÖÂËÀÑ»·£¬¼òµ¥·ÖÏíÏ¡£
ÃÜÂëËø³ÌÐò
¹¦ÄÜ£ºÅжÏÓû§ÊäÈëµÄÃÜÂ루ÕûÐÍ£©ÊÇ·ñÕýÈ·£¬²¢ÊµÏÖÊä´íÁ¬ÐøÖØÊ䣺
#include <stdio.h>
int main()
{
int password;
printf("ÇëÊäÈëÃÜÂ룺");
scanf("%d", &password);
while (password != 123)
{
printf("ÃÜÂë´íÎó£¬ÇëÖØÐÂÊäÈ룺");
scanf("%d", &password);
}
printf("ÃÜÂëÕýÈ·£¬³É¹¦½âËø£¡");
return 0;
}
¸´ÖÆ´úÂëÔËÐнá¹û£º
ËÀÑ»·ÎÊÌâ
ÊäÈëÊý×Öµ¹Ã»Ê²Ã´ÎÊÌ⣬µ«Èç¹ûÊäÒ»¸ö×Öĸ£¬Ø¬ÃξͿªÊ¼ÁË¡¡ÂúÆÁµÄÃüÁîÐб»¡¸ÃÜÂë´íÎó£¬ÇëÖØÐÂÊäÈ룺¡¹ËùÑÍû¡£ºÜÃ÷ÏÔ£¬while ËÀÑ»·ÁË¡£ÎªÊ²Ã´»áÕâÑùÄØ£¿ÉÏÃæ˵¹ý£¬µ± scanf() º¯ÊýÓöµ½²»¿ÉÄÜÊôÓÚ´ËÊý¾ÝÏîÀàÐ͵Ä×Ö·ûʱֹͣ£¬ÓÉÓÚÎÒÃÇÊäÈëµÄÊÇ×Öĸ£¬ÏÔÈ»²»ÄÜÆ¥Åä¸ñʽ´®ÖÐµÄ %d£¬ÓÚÊÇ scanf() º¯Êý¶ÁÈëʧ°Ü£¬É¶Ò²Ã»¸É¾ÍÖ±½Ó½áÊø£¬±äÁ¿ password µÄֵû±ä¡£¸üΪÖÂÃüµÄÊÇ£¬·Ç·¨µÄ×Ö·ûһֱͣÁôÔÚ»º³åÇøÖУ¬ÏÂÒ»´Î½øÈëÑ»·Ê± scanf() ÔٴζÁµ½µÄ½á¹ûÒÀÈ»·Ç·¨¡¢É¶Ò²²»¸É£¬ÓÚÊÇ scanf() º¯ÊýÐÎͬÐéÉ裬password µÄÖµÒ»Ö±±£³Ö²»±ä£¬Ôì³ÉËÀÑ»·´òÓ¡¡£
½â¾ö·½·¨
½â¾ö°ì·¨Ò²²»ÄÑ£¬Ö»ÒªÉÔ×÷Åжϼ´¿É¡£Æäʵ scanf() º¯ÊýÊÇÓзµ»ØÖµµÄ£¬±íʾ³É¹¦¶ÁÈëµÄÊý¾ÝÏî¸öÊý¡£¿ÉÒÔÀûÓÃÕâÒ»µãÅжÏÊý¾Ý¶ÁÈ¡ÊÇ·ñ³É¹¦£¬ÔÙͨ¹ý getchar() º¯Êý°Ñ»º³åÇø¶Á¿Õ£¨Çå³ý²ÐÁô×Ö·û£©£¬¾ÍÄÜʵÏÖ·Ç·¨ÊäÈëµÄ´¦Àí£¬Ìá¸ß³ÌÐòµÄ¿É¿¿ÐÔ¡£Ð޸ĺó´úÂëÈçÏ£º
#include <stdio.h>
int main()
{
int password;
printf("ÇëÊäÈëÃÜÂ룺");
while (password != 123)
{
if (scanf("%d", &password) == 0)
{
printf("·Ç·¨×Ö·û£¬ÇëÖØÐÂÊäÈ룺");
while (getchar() != '\n'); // Çå¿Õ»º³åÇø£¬»òÕßÓà fflush(stdin);
}
else if (password != 123)
printf("ÃÜÂë´íÎó£¬ÇëÖØÐÂÊäÈ룺");
}
printf("ÃÜÂëÕýÈ·£¬³É¹¦½âËø£¡");
return 0;
}
¸´ÖÆ´úÂëÔËÐнá¹û£º
ÇëÊäÈëÃÜÂ룺43
ÃÜÂë´íÎó£¬ÇëÖØÐÂÊäÈ룺345
ÃÜÂë´íÎó£¬ÇëÖØÐÂÊäÈ룺adf
·Ç·¨×Ö·û£¬ÇëÖØÐÂÊäÈ룺asdg
·Ç·¨×Ö·û£¬ÇëÖØÐÂÊäÈ룺234
ÃÜÂë´íÎó£¬ÇëÖØÐÂÊäÈ룺123
ÃÜÂëÕýÈ·£¬³É¹¦½âËø£¡
¸´ÖÆ´úÂë
С½á
´ó¶àÊýÈ˶¼¶®µÃ scanf() º¯ÊýµÄ³£¹æÓ÷¨£¬µ«ÊÇÔÚÓöµ½¡¸·ÇÖ÷Á÷¡¹µÄÀäÃÅÆæÝâÇé¿öʱ£¬¾ÍÒªÇóÎÒÃÇ¶Ô scanf() º¯ÊýµÄÖ´ÐÐÁ÷³ÌÒÔ¼°ÊäÈ뻺³åÇøÓÐÒ»¶¨µÄÁ˽⣬²ÅÄܽâÊÍһЩ¹îÒìµÄÏÖÏó¡£ÊÂʵÉÏ scanf() º¯Êý²¢²»³£Óã¬ÉõÖÁ»¹ÓеijÌÐòÔ±½¨Òé¸É´àÍêÈ«±ÜÃâʹÓÃËü£¬µ«×÷Ϊѧϰ£¬ÎÒÃǶàÁ˽âÒ»µãÒ²ÎÞ·Á£¬×îºó¸½ÉÏ¡¶C Primer Plus¡·ÖÐ¶Ô scanf() ˵Ã÷µÄÒ»¶Î»°¡£
Actually, scanf() is not the most commonly used input function in C. It is featured here because
of its versatility (it can read all the different data types), but C has several other input functions,
such as getchar() and gets(), that are better suited for specific tasks, such as reading single
characters or reading strings containing spaces. ¡ª¡ª ¡¶C Primer Plus, Fifth Edition¡·- The scanf() View of Input
²Î¿¼ÎÄÕÂ