www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/09/01/04:17:24

Xref: news2.mv.net comp.os.msdos.djgpp:8141
From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Scanf doesn't work -- sure it does
Date: Sun, 01 Sep 1996 02:42:02 -0700
Organization: Three pounds of chaos and a pinch of salt
Lines: 44
Message-ID: <32295A6A.5A5@cs.com>
References: <01bb974a$24b5f820$24c5b7c7 AT platko DOT ix DOT netcom DOT com> <509lhu$1t0 AT vidar DOT diku DOT dk>
NNTP-Posting-Host: ppp212.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Morten Welinder wrote:
> 
> "Bob Platko" <platko AT ix DOT netcom DOT com> writes:
> 
> >My code works fine in Borland, but not in DJGPP!  What's wrong?
> 
> Your program.

[snip]

> So you are passing a pointer to the wrong kind of object to scanf().
> Therefore scanf() overwrites memory you did not mean to.
> 
> Nuke the "short" and you will have a correct program.

I'd like to mention, just in case you were going to ask, that the reason
for this behavior is that Borland is a 16-bit compiler while djgpp is a
32-bit compiler.  Thus, in BC sizeof(int) == sizeof(short), while in
djgpp sizeof(int) == sizeof(long).  The reason your code worked in BC
is that %u and %hu both refer to similarly sized objects, while in
djgpp they do not.  You should always use the correct format specifier
even if a given implementation makes it unnecessary.  Compiling with
-Wall as mentioned above will guarantee that the compiler will warn you
when you forget this.

> Morten
> 
> PS: There are some problems with scanf() if I remember things
>     right.  They have to do with the return value and are being
>     worked on or have already been solved for the next release.

I think most of these have been solved; check out the djgpp bug tracking
system for more details.  However they mostly only show up when reading
from files as opposed to from the console.

-- 
John M. Aldrich, aka Fighteer I <fighteer AT cs DOT com>

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS d- s+:- a-->? c++>$ U@>++$ p>+ L>++ E>+ W+>++ N++ o+ K? w(---) O-
M-- V? PS+ PE Y+ PGP- t+(-) 5- X- R+ tv+() b+++ DI++ D++ G e(*)>++++
h!() !r !y+()
------END GEEK CODE BLOCK------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019