[TriLUG] a C question

Tanner Lovelace lovelace at wayfarer.org
Wed Oct 23 12:37:54 EDT 2002


On Wed, 2002-10-23 at 10:20, Paul D. Boyle wrote:
> Tanner Lovelace wrote:
> > fav_num is declared as an integer.  You then specify to scanf a %d,
> > which stands for a "decimal" number.  Therefore, scanf is looking
> > for a decimal number which it converts from the string you entered
> > to the end format.  When you enter a string, it tries to convert
> > and gets an error.  Since it was looking for a number and didn't
> > find one, it just returns 0 which scanf then assigns to fav_num.
> 
> This paragraph contains a subtle error.  It is true that in Greg's
> code scanf() does return 0 (the prototype for scanf() is more fully:
> 'int scanf( const char *format, ... )'.  However, returned zero does
> not mean that the value of 'fav_num' is modified by being assigned zero.

Hi Paul,

Actually, when I said "returns 0" I was referring to the function
that scanf calls to convert the inputed string to a numeric value.
You can see that by noticing I mentioned scanf doing something
after mentioning the return value.

I was wrong, however, in saying scanf assigned the 0 value to
fav_num.  You are correct in that it doesn't do anything to that
value.  It leaves it at its original value, which in this case
wasn't defined.  The compiler must have defaulted it to be 0, which
is where the value comes from.

Good catch.

Thanks,
Tanner
-- 
Tanner Lovelace | lovelace at wayfarer.org | http://wtl.wayfarer.org/
--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--
GPG Fingerprint = A66C 8660 924F 5F8C 71DA  BDD0 CE09 4F8E DE76 39D4
GPG Key can be found at http://wtl.wayfarer.org/lovelace.gpg.asc
--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--
          Si hoc legere scis, nimium eruditionis habes.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://www.trilug.org/pipermail/trilug/attachments/20021023/f3da0f66/attachment.pgp>


More information about the TriLUG mailing list