Greetings EveryOne
I have read this in a book:
What does that mean, how come scanf function handles end-of-line poorly?The function scanf is notorious for its poor end-of-line handling.
Thanks In Advance
Greetings EveryOne
I have read this in a book:
What does that mean, how come scanf function handles end-of-line poorly?The function scanf is notorious for its poor end-of-line handling.
Thanks In Advance
Last edited by Laythe; 03-25-2011 at 12:49 PM.
Because it leaves the CR/LF pair in the input buffer... But there's a real easy fix for that...
(No, don't ask us what it is.... look in your compiler documentation and see if you can find it yourself.)
Thank you for your reply
what does CR/LF stands for?
I'm using CodeBlocks and the version i downloaded is not supplied with documentation what do you advice me to do? change another IDE maybe?
Well... at the very least find library documentation for it... (It's MinGW by the way)
In the mean time here's a general C99 documentation page to get you started...
Dinkum C99 Library
Another IDE? Hmmm... if you are working only in C, I'd recommend Pelles C It's much more complete than Code:Blocks and unlike MinGW, it's fully documented. In fact it has one of the best help files I've ever seen.
Code::Blocks is not a compiler, it is an Integrated Development Environment. The compiler is probably gcc which is well documented. You can start here.
Also the CR/LF pair is only the end of line markers for DOS/Windows operating systems. Other operating systems may use the CR or LF by themselves.
Jim
can someone show an illustration of how does scanf function leaves the CR/LF pair in the input buffer, or more explanation please.
The scanf family of functions require specifically formatted input. That's what the f in scanf means. That means it will only read exactly what you tell it to read.
It will read the newline marker, if you tell it to expect it. Otherwise it won't.This says "read one character". That's all. So if you type 'Y' for a yes/no prompt, and hit enter, you've actually just input two keys. But you've only told it to read one. So it grabs the first key, the 'Y', and the remaining key is still in the input stream, waiting for you to do something with it.Code:scanf( "%c", &onechar );
Input streams in C are by default line buffered, which means that the function only fires when you hit enter. This means you can't by default make a program that reads every key stroke. You can't just press 'Y' and have it read that. It waits for you to hit enter, then it passes everything prior to your hitting enter to whatever you are using.
If you've told scanf to expect a space and then a character, it will check and see if there's a space waiting, if so, it will then grab the next character. If you tell it to expect a floating point number, it will start picking through the start of the stream to see if what's waiting there matches what a floating point number should look like.
It wants you to input exactly what you tell it to input. Anything that doesn't match makes it not pull that from the input stream.
Quzah.
Hope is the first step on the road to disappointment.
Thank you so very much Mr.quzah for the good explanation
the remaining key in this case is the enter key is that right, input stream is waiting for me to do something like what with the remaining key?This says "read one character". That's all. So if you type 'Y' for a yes/no prompt, and hit enter, you've actually just input two keys. But you've only told it to read one. So it grabs the first key, the 'Y', and the remaining key is still in the input stream, waiting for you to do something with it.
in this code example:It wants you to input exactly what you tell it to input. Anything that doesn't match makes it not pull that from the input stream.
at runtime, even if you don't enter a space before entering an integer the program run normally without any problem, so is there any problem occurred at unseen level?Code:i#include <stdio.h> int main() { int Integer = 0; printf("Please Enter An Integer: "); scanf(" %d", &Integer); printf("Twice %d Is %d\n", Integer, Integer*2); return 0; }
same thing if you enter multiple space key before entering the integer, the program run and ends normally, any comment about that too?
Thank you for your help, it's very appreciated ^_^
You should probably read the man page for the details. There's no point in me retyping the whole thing here.
Read the description and the section below the description which explains all of the format specifiers.
Quzah.
Hope is the first step on the road to disappointment.
thank you for the link it's very informative but still i couldn't find the answer to my previous question within that page or did i miss it!
You missed it, in the description:Each successive pointer argument must correspond properly with
each successive conversion specifier (but see the * conversion below).
All conversions are introduced by the % (percent sign) character. The
format string may also contain other characters. White space (such as
blanks, tabs, or newlines) in the format string match any amount of white
space, including none, in the input. Everything else matches only
itself. Scanning stops when an input character does not match such a
format character. Scanning also stops when an input conversion cannot be
made (see below).
Quzah.
Hope is the first step on the road to disappointment.
looks like i didnt miss it but i dindnt understood it well
does that means white space is ignored scanf?White space in the format string match any amount of white
space, including none, in the input.