[comp.os.minix] minix/ST cc peculiarity

meulenbr@cstw01.UUCP (Frans Meulenbroeks) (11/07/88)

Hi!

I noticed the following problem in the minix st C compiler.
Given the following program:
#define SOMETHING -32768
#include <stdio.h>

main()
{
	printf("%d\n", SOMETHING);
}

The result is, that -1 is printed.
Thing work well for -32767.
Also defining SOMETHING as (int)(-32768L) works fine.

This is obviously some conversion problem within the compiler.

I have not checked things for MINLONG etc.

by the way: don't use floats or doubles; they don't work.
-- 
Frans Meulenbroeks        (meulenbr@cst.prl.philips.nl)
	Centre for Software Technology
	( or try: ...!mcvax!philmds!prle!cst!meulenbr)

ceriel@cs.vu.nl (Ceriel Jacobs) (11/08/88)

To: meulenbr@cst.UUCP
From: Ceriel Jacobs <ceriel@cs.vu.nl>
Subject: Re: minix/ST cc peculiarity
Newsgroups: comp.os.minix
In-Reply-To: <257@cstw01.UUCP>
Organization: VU Informatica, Amsterdam
Cc: 
Bcc: 

In article <257@cstw01.UUCP> you write:
>Hi!
>
>I noticed the following problem in the minix st C compiler.
>Given the following program:
>#define SOMETHING -32768
>#include <stdio.h>
>
>main()
>{
>	printf("%d\n", SOMETHING);
>}
>
>The result is, that -1 is printed.
>Thing work well for -32767.
>Also defining SOMETHING as (int)(-32768L) works fine.
>

This is not a bug. The C reference manual(2.4.1) states:
	A decimal constant whose value exceeds the largest signed machine
	integer is taken to be long;
Notice that -32768 in this context is not a constant, but a constant
expression. The constant is 32768, which exceeds the largest signed machine
integer. So, the constant expression -32768 is a long, and should thus
be printed with %ld (or, in PC Minix 1.1, with %D).

--
Ceriel Jacobs, Vrije Universiteit Amsterdam	<ceriel@cs.vu.nl>

ZQ0018%DMSWWU1A.BITNET@cunyvm.cuny.edu (Kai Henningsen) (11/09/88)

|I noticed the following problem in the minix st C compiler.
|Given the following program:
|#define SOMETHING -32768
|#include <stdio.h>
|
|main()
|{
|    printf("%d\n", SOMETHING);
|}
|
|The result is, that -1 is printed.
|Thing work well for -32767.
|Also defining SOMETHING as (int)(-32768L) works fine.
|
|This is obviously some conversion problem within the compiler.
|
|I have not checked things for MINLONG etc.
|
|by the way: don't use floats or doubles; they don't work.
|--
|Frans Meulenbroeks        (meulenbr@cst.prl.philips.nl)
|    Centre for Software Technology
|    ( or try: ...!mcvax!philmds!prle!cst!meulenbr)


What is happening is this:
When the compiler sees "-32768", it parses this as two tokens,
"-" and "32768". The latter has obviously to be some sort of
an int; it's too large for a short, so it must be a long.
This is negated, yielding -32768L, or 0xffff8000.
As printf %d expects a (short) int, it uses the first word
of this number, which on a 68K machine is 0xffff. This
is -1. The rest will be ignored in this case.

The problem is your assumption that because the number
-32768 CAN be represented as a short int, the compiler
will do so; instead look at the number alone: 32768
definitely is NOT short.

Kai Henningsen
zq0018 @ dmswwu1a . bitnet

or what path do you use?