delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/04/09/13:30:29

From: hhkraemer AT web DOT de (Horst Kraemer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: GCC 2.95.3 and C standard(s) + commandline switch problems
Date: Mon, 09 Apr 2001 17:18:55 GMT
Lines: 59
Message-ID: <3ad1e52a.70752817@news.cis.dfn.de>
References: <4830 DOT 986817066 AT www1 DOT gmx DOT net>
NNTP-Posting-Host: b3b89.pppool.de (213.7.59.137)
X-Trace: fu-berlin.de 986836713 7316683 213.7.59.137 (16 [27606])
X-Newsreader: Forte Free Agent 1.21/32.243
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Mon, 9 Apr 2001 13:51:06 +0200 (MEST), ahelm AT gmx DOT net wrote:

> Hi 
> I'm having troubles with a subtle ANSI C standard problem
> and maybe some bugs in the DJGPP port of GCC (commandline switches).
> 
> What type is the result of the multiplication of two unsigned short
> variables?

if int can hold all the values of unsigned short then it is int,
otherwise it is unsigned int. In DJGPP it is int because short is 16
bit and int is 32 bit.

 
> The problem is that there is a difference between old and new ANSI
> standard.

No, there is no difference.

 
> ANSI C (both standards) clearly state that the result of a binary
> arithmetic operation is of the same type as the inputs after
> applying the "usual arithmetic conversions" (to the inputs) when
> necessary.
> 
> Following the rules in 6.2.1.5 in the old standard
> (usual arithmetic conversions), none of the rules apply
> until reaching the last:
> "Otherwise both operands have type int"
> 
> So both operands are converted to int before multiplication.
> That explains the int (32bit) result of a 16x16bit multiplication
> when using GCC. Because actually 
> (16bit -> 32bit) x (16bit -> 32bit) = 32bit 
> is computed
> 
> The new standard however has a new rule in the "Ususal 
> arithmetic conversions":
> 
> "If both operands have the same type, then no further conversion 
> is needed."

No. It says

...otherwise the integer promotions are performed on both operands
[this includes the promotion unsigned short -> int on both operands]
Then the following rules are applied to the promoted operands:
If both operands have the same type, then no further conversion 
is needed.

This is exactly the same rule than in C90. The committee never whould
have dared to touch the promotion and implicit conversion rules. This
would break existing programs conformant to C90.



Regards
Horst

- Raw text -


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