![]() |
|
scanf character - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: scanf character (/Thread-scanf-character) Pages:
1
2
|
RE: scanf character - chemicalrage - 12-25-2012 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 RE: scanf character - Deque - 12-26-2012 (12-20-2012, 06:31 PM)Xe.4 Wrote: Thanks, that solved the problem. but I want to know the reason aswell . 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. Your answer has nothing to do with the problem or the question. RE: scanf character - Xe.4 - 12-26-2012 Thanks everyone for having opinions. RE: scanf character - chipp - 12-30-2012 (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? RE: scanf character - Deque - 12-30-2012 (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 gave one in my post. |