9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] another compiler bug (another try to send this coherently)
@ 2001-07-14  3:26 Mike Haertel
  2001-07-16  9:05 ` Douglas A. Gwyn
  0 siblings, 1 reply; 14+ messages in thread
From: Mike Haertel @ 2001-07-14  3:26 UTC (permalink / raw)
  To: 9fans

Argh, the wrong stuff got pasted on the last try and it included a ^D.

This program illustrates that the integral promotions
of types smaller than int will incorrectly promote unsigned
char to unsigned int, rather than signed int as required
by ANSI C.

On a conforming implementation with 32-bit ints, this
program should print "-1 -1".

#include <stdio.h>
#include <stdlib.h>

int
main()
{
	static unsigned char tab[1] = { 0x80 };
	static int x = 0x80;

	printf("%d %d\n", tab[0] << 24 >> 31, x << 24 >> 31);
	exit(EXIT_SUCCESS);
}


^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: [9fans] another compiler bug (another try to send this coherently)
@ 2001-07-16 11:13 rog
  2001-07-16 12:51 ` Boyd Roberts
  0 siblings, 1 reply; 14+ messages in thread
From: rog @ 2001-07-16 11:13 UTC (permalink / raw)
  To: 9fans

> How do you figure that?  The promoted tab[0] (signed int) or x
> left-shifted 24 places sets the sign bit, and right-shifting a
> negative value is implementation-defined.

implementation-defined implies the result is at least consistent across
a particular implementation.  the example that was given shows that
that isn't the case (if the unsigned char is indeed being promoted to
signed int, which it isn't, as the compiler error messages for the
following code imply:)

#include <u.h>
#include <libc.h>

int
main(void)
{
         static unsigned char tab[1] = { 0x80 };
	char *z;
	z = tab[0] << 24 >> 31;
}



^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: [9fans] another compiler bug (another try to send this coherently)
@ 2001-07-17 15:51 forsyth
  2001-07-18  8:43 ` Douglas A. Gwyn
  0 siblings, 1 reply; 14+ messages in thread
From: forsyth @ 2001-07-17 15:51 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 140 bytes --]

i thought that was one of the areas where the rules had changed with ANSI C
and assumed ken thompson had implemented the rules he knew!


[-- Attachment #2: Type: message/rfc822, Size: 1852 bytes --]

To: 9fans@cse.psu.edu
Subject: Re: [9fans] another compiler bug (another try to send this coherently)
Date: Tue, 17 Jul 2001 15:16:38 GMT
Message-ID: <3B544A97.5F5EA6E1@null.net>

Mike Haertel wrote:
> The unsigned char is _supposed_ to be promoted to signed int.

That is true, and I'm surprised that the compiler would get it
wrong.  One wonders where else the integer promotions aren't
done right.

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2001-07-18  8:43 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-14  3:26 [9fans] another compiler bug (another try to send this coherently) Mike Haertel
2001-07-16  9:05 ` Douglas A. Gwyn
2001-07-16  9:38   ` Boyd Roberts
2001-07-16 18:34   ` Mike Haertel
2001-07-16 18:52     ` Boyd Roberts
2001-07-16 19:21       ` Mike Haertel
2001-07-16 19:47         ` Boyd Roberts
2001-07-17 15:16         ` Douglas A. Gwyn
2001-07-16 11:13 rog
2001-07-16 12:51 ` Boyd Roberts
2001-07-16 13:41   ` Boyd Roberts
2001-07-16 13:44   ` Boyd Roberts
2001-07-17 15:51 forsyth
2001-07-18  8:43 ` Douglas A. Gwyn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).