The problem is that scanf("%c" will take whatever character is next,
even if it is not the one you think you typed. When you typed your
first entry, the number "3" and then pressed "Return", the "\n"
character is left in the buffer, so your scanf gets that character
instead of the one you want. The space in front of the %c causes scanf
to skip whitespace and give you the next non-whitespace character.

In general, using scanf is not a reliable way to read input. There may
be some cases where it is adequate, but there are much more robust
ways. For example, use fgets to read an entire line of input and then
parse it using sscanf or just by looking at the characters one at a
time. In your case, you would read in the first line and use sscanf to
extract the numeric value, and then read in the second line and scan
through it until you find a valid character.

However you do it, make sure to have some error checking to verify
that you end up with something valid, in this case I think you would
want to verify that it was a valid variable name.

Don

On Aug 24, 9:19 am, Mehnaaz <[email protected]> wrote:
> @ vikas ..the space actually worked !!..is there any
> explanation ??..thanks a ton!

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to