[Courses] C Programming For Absolute Beginners, Lesson 2: Fun With Printf, Scanf, Puts, and Variables

Jacinta Richardson jarich at perltraining.com.au
Wed Feb 29 04:19:34 UTC 2012


On 29/02/12 13:06, Damián wrote:
>
> Hi Jacinta, thanks for your answer. I understood all of that (I think), but I 
> still don't get why in the original code:
>
> ====
> scanf( "%d", &a );
> puts( "Please enter another number up to three digits: " );
> scanf( "%d", &b );
> c = a + b;
> printf("%d + %d = %d\n", a, b, c);
> ====
>
> If you enter an invalid value in the first scanf, all the other lines are run, 
> except for the second 'scanf'. It doesn't wait for user input. I don't see the 
> relation between the first scanf returning 0 and the second not running 
> because of that condition.
>

Oh!  I'm sorry, yes, that is much more clear.

scanf() reads a value in from the STDIN input buffer.  Essentially this contains 
all of the stuff you type in, and that gets passed to the program each time you 
hit enter.  When scanf() runs, it starts at the start of the buffer and tries to 
find something there that matches what it is looking for.  It'll skip 
whitespace, but not other characters.  So if we type in:

         "          123<newline>"

(without the quotes, and where we hit the enter key to create a newline (also 
represented as "\n")) the first scanf() will skip all of that whitespace, see a 
number (123) and read that in.  At the end of this call to scanf(), the position 
in the buffer that we are up to is just before that newline/enter.  When the 
second scanf() runs, it sees that there is no data for it (there's only the 
newline) so it waits for further input.

On the other hand, if we typed in:

         "123 456\n"

(without the quotes, and where \n is our newline (hitting the enter key)) then 
the first scanf() will skip any leading whitespace (of which there isn't any) 
then then see a number (123) and read that in.  At the end of this call to 
scanf(), the position in the buffer that we are up to is the space before after 
the 3.  When we run the second scanf() it sees that there is still data in the 
buffer and it reads the next number 456 without waiting for further input.

Something similar happens when we type in garbage data.  If we type in:

         "no thanks"

then the first scanf() sees that there isn't a number there for it, so it 
doesn't read anything in (and returns 0 for how many things it read in).  So the 
position of in the input buffer is still at that letter "n" in "no".  When the 
next scanf() happens it sees that there is still data in the buffer, so it reads 
from that and also fails to match a number, therefore also returning 0.

This is actually really helpful.  It means that you can do the following:

         if( scanf( "%d", &number) ) {
                 printf("You gave me a number!\n");
         }
         else if( scanf( "%s", &string) ) {
                 printf("You gave me a string!\n");
         }

I hope this helps.

     J



More information about the Courses mailing list