1

In scanf space and \n is delimiter for the character i/p. The below program accepts only two input. I could not understand why does it accept two input.Please explain about this behaviour.

     char a,b,c;
    scanf("%c%c%c",&a,&b,&c);
    printf("%c%c%c",a,b,c);
    return 0;
1
  • It doesn't "accept two inputs", it accepts three consecutive chars. Commented Aug 14, 2011 at 8:59

2 Answers 2

5

It does accept 3 inputs if you don't put spaces between the input characters.

If you want to allow space(s) between inputs use scanf("%c %c %c",&a,&b,&c);.

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

4 Comments

Oooooohhhhhh, that's what the OP wanted! :-)
it accepts 2 inputs when given scanf("%c%c%c",&a,&b,&c) without space and it accepts three input when given scanf("%c %c %c",&a,&b,&c) with space.
@Beata: first one reads three consecutive characters (reads space too). Second one reads three non-whitespace characters (so both "123", "1 2 3" and any number of spaces between them works)
@yi_H:yeah it reads as you have said.I'm little late to understand it.Thanks.
1

If you enter the characters '123' without seperating them by a space or a carriage return then a is set to '1', b to '2' and c to '3'. If you seperate the characters by a space ('1 2 3') then a is set 1, b to ' ' and c to '3'. Note, a space is also handled as a input character!!

Comments

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.