[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 00:21:54 UTC 2012


On 29/02/12 11:04, Damián wrote:

> Ok, after the introduction, here is my doubt: I understand that the first 
> scanf fails if you enter an invalid value (I tried your portion of code), but 
> I don't get why that condition makes the second scanf automatically behave as 
> if I had entered a value.

I'm not quite sure I understand the question, so I'll start from the top.  The 
original code says:

    int main()
    {
         int a, b, c;

         puts( "Please enter any number up to three digits:" );
         scanf( "%d",&a );

         printf( "You entered %d. Now enter another number up to three digits:\n", a );
         scanf( "%d",&b );

         c = a + b;
         printf("%d + %d = %d\n", a, b, c);

         return 0;
    }


When we run this code, the first scanf() attempts to read a value from the 
user.  Whether or not is does actually read a value from the user (perhaps the 
user gives it something it doesn't expect, such as "no thanks", the next line of 
code prints out whatever is in the variable "a".

We don't know what is in the variable "a" because we never gave it a value to 
start with.  So it's something undetermined.  Other people found that it 
returned large negative numbers, but it could be a large positive number or even 
a small number.  No idea.  It depends on what is in that piece of memory at the 
time.

The changed code would look like this:

    int main()
    {
         int a, b, c;
         int nr = 0;

         puts( "Please enter any number up to three digits:" );
         nr = scanf( "%d",&a );
         if(nr<  1) {
             printf( "Invalid input.\n" );
             return 1;
         }

         printf( "You entered %d. Now enter another number up to three digits:\n", a );
         scanf( "%d",&b );

         c = a + b;
         printf("%d + %d = %d\n", a, b, c);

         return 0;
    }


This version is doing something special.  When we write:

     nr = scanf(...);

the return value of scanf() is being captured and recorded in to "nr".  Lots of 
functions have return values, and we can usually read the documentation to work 
out what we want to check for.  To find out what the return values are, we 
should read the documentation:

     man -s3 scanf

and if we wade through way too many lines of other stuff we'll eventually get to:

    RETURN VALUE
            These functions return the number of input items successfully
    matched and assigned, which can be fewer
            than  provided for, or even zero in the event of an early matching
    failure.

We're asking scanf() to get us one integer (that's what the %d) is matching.  If 
it successfully is able to find us one integer, it will have a return value of 
1.  If it matches no integers, it will have a return value of 0.  As 0 is less 
than 1, the condition

     if(nr < 1)

will be true, and we'll enter the block.  This prints out a message and then 
returns 1;.  The return() function in this case is essentially the same thing as 
what scanf() is using under the hood, and so our main() function is returning 1, 
which in this case counts as a failure.  Our program did not run successfully to 
completion.

> Thank you everyone, and particularly to Carla for taking the time to try to 
> teach us some good C programming.

All the best,

     J


More information about the Courses mailing list