delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/09/12/08:15:19

From: "David Thompson" <david DOT thompson1 AT worldnet DOT att DOT net>
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
References: <p2m2rsg34070tpo2vvia7bdlugv6h3g107 AT 4ax DOT com> <nns4rssto1eudtvktejqkhjkee3vdtje5e AT 4ax DOT com> <39b38189 DOT 258387457 AT news DOT worldonline DOT nl>
Subject: Re: hex calculation routine!?
Lines: 46
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2615.200
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
Message-ID: <3Thv5.4492$M37.347857@bgtnsc07-news.ops.worldnet.att.net>
Date: Tue, 12 Sep 2000 03:59:59 GMT
NNTP-Posting-Host: 12.78.110.231
X-Complaints-To: abuse AT worldnet DOT att DOT net
X-Trace: bgtnsc07-news.ops.worldnet.att.net 968731199 12.78.110.231 (Tue, 12 Sep 2000 03:59:59 GMT)
NNTP-Posting-Date: Tue, 12 Sep 2000 03:59:59 GMT
Organization: AT&T Worldnet
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Richard Bos <info AT hoekstra-uitgeverij DOT nl> wrote :
...
> > unsigned long foo = 0x800f0f0f
> > unsigned long bar = 0x8f0
(missing semicolons)

> >
> > printf("%lx - %lx = %lx\n", foo, bar, foo - bar);
> >
> > IIRC, %x prints an int as hexadecimal, and %lx prints a long as
> > hexadecimal.  Crossposted to comp.lang.c for further comment on this
> > portable language issue.
>
To be absolutely precise, %x prints an unsigned int
and %lx an unsigned long.  Which is what you wanted.

For gcc, including djgpp, int and unsigned int are always 32 bits,
but if you want "portable" you can't rely on that.  And gcc
-Wformat does warn about such (potential) mismatches.

> Yup. Note also that in C, you have hex constants (in the source), but no
> hex _values_. You have integer values; by the time printf() or any other
> function gets to deal with them, representation is irrelevant. You could
> just as easily do this:
>
True.
You do have signed and unsigned integer values of different widths
(though in most contexts those narrower than int are widened).

>   printf("%ld - %ld = %lx\n", foo, bar, foo - bar);
>   printf("%lx - %lx = %ld\n", foo, bar, foo - bar);
>
> (%ld specifies long decimal output)
>
and technically long and unsigned long are required to have the same
representation (hence be compatible for variadic args) only in their
common range (0..LONG_MAX).  For large unsigned numbers or
negative signed ones, this might fail; though on mainstream platforms,
probably including all gcc (much less djgpp) runs on, it will produce
the "obvious" twos-complement results.

--
- David.Thompson 1 now at worldnet.att.net



- Raw text -


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