Angus
Angus

Reputation: 12631

scanf() function in c

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;

Upvotes: 1

Views: 261

Answers (2)

Karoly Horvath
Karoly Horvath

Reputation: 96286

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);.

Upvotes: 5

Hans
Hans

Reputation: 13070

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!!

Upvotes: 1

Related Questions