Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!henry From: henry@utzoo.UUCP (Henry Spencer) Newsgroups: comp.lang.c Subject: Re: Long integers Message-ID: <8582@utzoo.UUCP> Date: Tue, 15-Sep-87 12:08:58 EDT Article-I.D.: utzoo.8582 Posted: Tue Sep 15 12:08:58 1987 Date-Received: Tue, 15-Sep-87 12:08:58 EDT References: <9266@brl-adm.ARPA> Organization: U of Toronto Zoology Lines: 26 > The format %ld is wrong when using the PRINTF family of functions. > The reason for this is that integer arguments are *always* widened to LONG > when passed in to a function, so the %d format is meant for LONGS. NO NO NO NO NO!!!! Arguments of type short or int are passed as int , not as long! On 32-bit machines, it usually happens that "int" and "long" are synonymous. But this is not guaranteed to be true everywhere! %d is the correct format for shorts and ints in printf; %ld should always be used for longs. In scanf, it's %hd for short, %d for int, %ld for long, no mistakes allowed. > This is also true for %f. Here we have slightly different rules: floats are passed as doubles, so in printf it is always correct to use %f. In scanf, once again, it is necessary to be fussier: %f for float and %lf for double. The ANSI C people have added some minor complications to this but have not changed the basic rules. If code that follows these rules malfunctions, either the code or the compiler (or its library) is broken. -- "There's a lot more to do in space | Henry Spencer @ U of Toronto Zoology than sending people to Mars." --Bova | {allegra,ihnp4,decvax,utai}!utzoo!henry