Trilok
Trilok

Reputation: 13

scanf() not taking multiple inputs

Hi I just started learning C programming in gcc compiler on my Debian system. Here is the code

main()
{
    fflush( stdin );
    int a,b;
    scanf("%d,%d",&a,&b);
    printf("%d,%d",a,b);
}

The scanf doesn't take input for the second variable. I press 2 and then return key and it displays

root@debian:/home/wis# ./test
2
2,0root@debian:/home/wis#

I have used space and tab key also. Please help me.

Upvotes: 1

Views: 604

Answers (1)

Harald Nordgren
Harald Nordgren

Reputation: 12381

You defined your scanf string as "%d,%d", so the program expect an input like 1,2.

If you give it only one digit and press Enter, it parses the first digit and leaves the second one untouched. It was assigned 0 on declaration, so that's what you are seeing when printing.

Your printf statement would benefit from an "\n" at the end, and your code snippet needs indentation. Please show your includes (#include <stdio.h>) next time, it makes it easier for us to compile and run the code.

Upvotes: 2

Related Questions