Login Register






scanf character filter_list
Author
Message
RE: scanf character #11
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

Reply

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

RE: scanf character #13
Thanks everyone for having opinions.

Reply

RE: scanf character #14
(12-21-2012, 06:28 AM)H4R0015K Wrote: %c format specifier requires a blank space before it because it start from a blank space or whitespace it does not store the blank space if it is not there it will skip the scanf fuction to solve this you can use %s which is string format specifier or leave a blank space before %c.

i just did know this, do you have an explanation links?
[Image: ap8g35.jpg]

Reply

RE: scanf character #15
(12-30-2012, 07:52 PM)chipp Wrote:
(12-21-2012, 06:28 AM)H4R0015K Wrote: %c format specifier requires a blank space before it because it start from a blank space or whitespace it does not store the blank space if it is not there it will skip the scanf fuction to solve this you can use %s which is string format specifier or leave a blank space before %c.

i just did know this, do you have an explanation links?

I gave one in my post.
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