简体   繁体   中英

C Language: Scanf

This is a simple C program that explains do while loop.Before the loop ends there two scanf./ I am really confused with the one " scanf("%d",&dummy); ". The program will not run as expected wihtout this line. So, I believe this line act as a some sort of placeholder to create a space to take input for chat. But I am not sure about this and how actually it works

#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');  
}  

Once the user has entered a choice and hit Enter key, the number is read up to, but not including, the end-of-line character. Without this scanf("%d", %dummy); , that end-of-line character is still there and is read into c . Since that is not equal to 'y' , your loop terminates.

With the scanf("%d", dummy); call, the whitespace gets consumed looking for digits. Note that scanf documentation says, "All conversion specifiers other than [, c, and n consume and discard all leading whitespace characters (determined as if by calling isspace) before attempting to parse the input." When a non-whitespace, non-digit character is encountered (such as 'y' or 'n'), the reading of the integer fails. That's ok, you're ignoring it anyway. But now the 'y' or 'n' is there to read into c .

An easier way would be to change scanf("%c", &c); to scanf(" %c", &c); (note the space before %c ). This tells scanf to consume any whitespace before reading a character into c . It's much more idiomatic.

Because variable dummy is used to capture the return following choice number.

If we add one line to print dummy

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

This is an explanation of the result:

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

This is happening as scanf leaves a '\n' character which is read by next scanf is %c is used.

Solution: replace %c by %1s in the reported program to solve the issue (to read the next non-white space character, use %1s instead of %c).

Kindly refer below program to see issue because of newline character.

[root@localhost Programs]# 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;
    }  

you can see value 10 which is the value of character '\n' left by earlier scanf.

  • Issue is resolved by below program (%1s is used in place of %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("%1s",&c); }while(c=='y');
    return 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM