EmbLogic's Blog

issue in basic scanf condition

//just go thru the simple prog

#include
#include
int main()
{
char str[5];
int i;
for(i=0;i<5;i++)
{
printf("\n enter the character :");
scanf("%c",str+i);

}
printf("\n %s\n ", str);

return 0;
}

predict what is the output

if u think output is
"
enter the character :a
enter the character :b
enter the character :c
enter the character :d
enter the character :e
abcde
"

then u r wrong.. :P

output is
enter the character :a
enter the character :
enter the character :b
enter the character :
enter the character :c

a
b
c
"
this is the original output..

can someone please help and elaborate..

2 Responses to issue in basic scanf condition

  1. The newline left in the input buffer from the first scanf call is still there and gets read by the second. One of the easiest ways to deal with this might be to just put a space in front of the %c specifier OR you can flush the input stream using fflush(stdin) just before scanf();

  2. sumit.ahuja says:

    dont htink..

    222 much..:)

    thinking is such a waste of tym…

    evn i got the same o/p…:)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>