簡體   English   中英

C 語言:Scanf

[英]C Language: Scanf

這是一個簡單的 C 程序,它解釋了 do while 循環。在循環結束之前有兩個 scanf。/我真的對一個“scanf(”%d",&dummy);“感到困惑。 沒有這一行,程序將無法按預期運行。 所以,我相信這條線作為某種占位符來創建一個空間來接受聊天輸入。 但我不確定這一點以及它實際上是如何工作的

#include<stdio.h>  
#include<stdlib.h>  
void main ()  
{  
    char c;  
    int choice,dummy;    
    do{  
    printf("\n1. Print Hello\n2. Print Javatpoint\n3. Exit\n");  
    scanf("%d",&choice);  
    switch(choice)  
    {  
        case 1 :   
        printf("Hello");   
        break;  
        case 2:    
        printf("Javatpoint");  
        break;  
        case 3:  
        exit(0);   
        break;  
        default:   
        printf("please enter valid choice");      
    }  
    printf("do you want to enter more?");   
    scanf("%d",&dummy);  //This part confuses me
    scanf("%c",&c);  
    }while(c=='y');  
}  

一旦用戶輸入一個選項並按下Enter鍵,該數字將被讀取到行尾字符,但不包括行尾字符。 沒有這個scanf("%d", %dummy); ,該行尾字符仍然存在並被讀入c 由於這不等於'y' ,因此您的循環終止。

scanf("%d", dummy); 調用時,空格會被消耗以尋找數字。 請注意, scanf 文檔說:“除 [、c 和 n 之外的所有轉換說明符,在嘗試解析輸入之前都會消耗並丟棄所有前導空白字符(就像通過調用 isspace 一樣確定)。” 當遇到非空白、非數字字符(例如“y”或“n”)時,integer 的讀取失敗。 沒關系,反正你無視。 但是現在“y”或“n”可以讀入c

更簡單的方法是更改scanf("%c", &c); scanf(" %c", &c); (注意%c之前的空格)。 這告訴scanf在將字符讀入c之前消耗任何空格。 它更加地道。

因為變量dummy用於捕獲選擇數之后的return值。

如果我們添加一行來打印虛擬

    printf("[%c]\n", dummy); // We should change dummy's type to char, omit here

這是對結果的解釋:

1. Print Hello                
2. Print Javatpoint
3. Exit
1                                     # This is input: 1 and an invisible return
Hellodo you want to enter more?[      # return is captured by scanf and printed
]
y                                     # This is input: y and an invisible return, return is ignored by scanf("%d", &choice), because it need numbers

1. Print Hello
2. Print Javatpoint
3. Exit
2                                     # This is input: n and an invisible return
Javatpointdo you want to enter more?[
]
n

發生這種情況是因為 scanf 留下了一個 '\n' 字符,該字符由下一個 scanf 讀取,使用 %c。

解決方案:將報告程序中的 %c 替換為 %1s 以解決問題(要讀取下一個非空白字符,請使用 %1s 代替 %c)。

由於換行符,請參考下面的程序來查看問題。

[root@localhost 程序]# cat -n scan.c

 #include<stdio.h>  
 #include<stdlib.h>  
 int main ()  
 {  
     char c = 0;  
     int choice,dummy;    
     do{  
     printf("\n1. Print Hello\n2. Print Javatpoint\n3. Exit\n");  
     scanf("%d",&choice);  
     switch(choice)  
     {  
         case 1 :   
         printf("Hello\n");   
         break;  
         case 2:    
            printf("Javatpoint\n");  
            break;  
            case 3:  
            exit(0);   
       break;  
            default:   
            printf("please enter valid choice\n");      
        }  
        printf("say 'y' if you want to enter more\n");   
        scanf("%c",&c);
        printf("%d\n",c);
        scanf("%c",&c);
        printf("%d\n",c);
        }while(c=='y');  
        return 0;
    }  

您可以看到值 10,它是早期 scanf 留下的字符 '\n' 的值。

  • 問題由以下程序解決(使用 %1s 代替 %c ):

    #include<stdio.h>
    #include<stdlib.h>
    主函數()
    {
    字符 c = 0;
    int 選擇,假人;
    做{
    printf("\n1. Print Hello\n2. Print Javatpoint\n3. Exit\n");
    scanf("%d",&choice);
    開關(選擇)
    {
    情況1:
    printf("你好\n");
    休息;
    案例2:
    printf("Javatpoint\n");
    休息;
    案例3:
    退出(0);
    休息;
    默認:
    printf("請輸入正確的選擇\n");
    }
    printf("如果你想輸入更多就說'y'\n");
    scanf("%1s",&c); }while(c=='y');
    返回0; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM