delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/10/17/10:30:13.1

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Unsigned Int Problem
Date: 17 Oct 2000 14:29:04 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 37
Message-ID: <8shnng$bnb$1@nets3.rz.RWTH-Aachen.DE>
References: <39EC53CC DOT EAA6B85E AT email DOT com>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 971792944 12011 137.226.32.75 (17 Oct 2000 14:29:04 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 17 Oct 2000 14:29:04 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Derek Chew <sdchew AT email DOT com> wrote:

> most recently, I noticed that one of my old functions that I wrote using
> TC has been acting wierdly.. I did some further investigate a bit futher
> and found that this line of code executes differently in TC and GCC

Yes, and rightly so. The code behaves perfectly correctly, on both
architectures (assuming you #include <stdio.h> somewhere before that
main() function, as you should).

[...]
> on TC, I will get the desire result of 65535 as the 16-bit unsigned int
> will overflow into and display this value... but apparently, under
> DJGPP/GCC, an unsigned int is 32-bit and I get the unwanted value of
> 4294967295 ...

It's your idea of a 'desired' or 'wanted' result that is incorrect. To
cite an old computer lore:

	Computers hardly ever do what you want them to --- they do exactly
	what you *tell* them, instead. 

You expressed your desire of getting 65535 as the printed value in a
way that C compilers don't generally understand. TC happened to do
what you wanted, but that was pure coincidence.

The broken assumption in your piece of code is that 'unsigned int'
would always be a 16bit datatype. In DJGPP, and many other C
compilers, that's not the case. The only guarantee granted by the
language is that unsigned int will always be at least an effective 16
bits long. But it can easily be more.

The standard header file <limits.h> is your friend. Read up about it
in your TC docs, or in a textbook on C.
-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


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