Login Register






scanf character filter_list
Author
Message
RE: scanf character #12
(12-20-2012, 06:31 PM)Xe.4 Wrote: Thanks, that solved the problem. but I want to know the reason aswell . Biggrin

There is still a newline in the buffer which is read into op as character. Flushing the buffer will help. Using a space in scanf triggers it to scan for whitespaces in order to ignore them. You can see this in the reference and the usage of the format parameter: http://www.cplusplus.com/reference/cstdio/scanf/

Quote:Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none).

That means putting a space into the format paramter will make it ignore the newline in the buffer.

(12-23-2012, 04:15 PM)h4r0015k Wrote: i have read somewhere that getchar() are vulnerable and is recommended not to use it.

scanf is vulnerable too (format string attacks).

(12-25-2012, 11:25 PM)chemicalrage Wrote: whenever you declare a char data type, use %c or %s as the format specifier.
int - %d
long int - %ld
float - %f which I believe you already know
char - %s / %c

Your answer has nothing to do with the problem or the question.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply





Messages In This Thread
scanf character - by Xe.4 - 12-20-2012, 05:43 PM
scanf character - by Xe.4 - 12-20-2012, 05:43 PM