1

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.

2
  • Please read some C books before asking beginners questions! Commented Feb 7, 2016 at 4:12
  • Remove fflush( stdin );. And main()int main(void) Commented Feb 7, 2016 at 5:32

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

"It was assigned 0 on declaration" No its not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.