[comp.lang.c] Type of expression sizeof

penneyj@servio.UUCP (D. Jason Penney) (09/20/89)

Consider the following simple program?
#include <stdio.h>

main()
{
if (3 - sizeof(long) < 0)
  printf("passed\n");
else
  printf("failed\n");
}

On our DECStation-3100 running Ultrix, this program returns "failed" instead
of "passed".  It seems that the sizeof() is promoting the expression to be
unsigned.  By the way, changing 3 to 3L does NOT make the problem disappear.

I am posting this quirk to comp.lang.c because I would be VERY interested to
hear (by e-mail, please) from anyone who thinks that this is acceptable 
behavior.

I am posting this to comp.unix.ultrix because I think you poor implementors
ought to be forewarned.

Our conversation with DEC re this bug was extremely unhelpful.  They claim that
the return type of a sizeof() expression is system-dependent.
-- 
D. Jason Penney                  Ph: (503) 629-8383
Beaverton, OR 97006              uucp: ...uunet!servio!penneyj
STANDARD DISCLAIMER:  Should I or my opinions be caught or killed, the
company will disavow any knowledge of my actions...

rang@cs.wisc.edu (Anton Rang) (09/21/89)

In article <256@servio.UUCP> penneyj@servio.UUCP (D. Jason Penney) writes:

>  It seems that the sizeof() is promoting the expression to be
>unsigned.
>
>Our conversation with DEC re this bug was extremely unhelpful.  They claim that
>the return type of a sizeof() expression is system-dependent.

On my uVAX 3200 with BSD 4.3, the 'cc' compiler defines sizeof() to
return a signed value.  'gcc' defines sizeof() to return an unsigned
value.  Is this defined in the ANSI draft standard?
   
+----------------------------------+------------------+
| Anton Rang (grad student)        | rang@cs.wisc.edu |
| University of Wisconsin--Madison |                  |
+----------------------------------+------------------+

"You are in a twisty little maze of Unix versions, all different."

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/22/89)

In article <256@servio.UUCP> penneyj@servio.UUCP (D. Jason Penney) writes:
>Our conversation with DEC re this bug was extremely unhelpful.  They claim that
>the return type of a sizeof() expression is system-dependent.

Why was that unhelpful?  They're right.  It's not DEC's bug.

AT&T UNIX C compilers changed the type of sizeof from int to unsigned
int many years ago.

The ANSI standard for C will require that the type of sizeof be an
unsigned integral type (so it might be unsigned long on some systems).

scott@bbxsda.UUCP (Scott Amspoker) (09/22/89)

In article <256@servio.UUCP> penneyj@servio.UUCP (D. Jason Penney) writes:
>I am posting this quirk to comp.lang.c because I would be VERY interested to
>hear (by e-mail, please) from anyone who thinks that this is acceptable 
>behavior.
>
>Our conversation with DEC re this bug was extremely unhelpful.  They claim that
>the return type of a sizeof() expression is system-dependent.

Somewhere along the way sizeof() became unsigned.  We had few loops
that blew up but it was easy enough to fix.  I'm not sure where we
got the notion that sizeof() was an int - but apparently a lot of
developers got the same notion.  I figured that part of the
problem was those 64K segmented, 16-bit integer processors.
In order to allow an array greater than 32k, sizeof() had to be
unsigned (or long).

I hope the ANSI draft doesn't leave this as "implementation dependent".
Some idiot implementor somewhere will make sizeof() return float.

-- 
Scott Amspoker
Basis International, Albuquerque, NM
(505) 345-5232

doug@jhunix.HCF.JHU.EDU (Douglas W O'neal) (09/22/89)

In article <256@servio.UUCP> penneyj@servio.UUCP (D. Jason Penney) writes:
> [ example deleted ]
>It seems that the sizeof() is promoting the expression to be
>unsigned.  By the way, changing 3 to 3L does NOT make the problem disappear.
> [...]
>Our conversation with DEC re this bug was extremely unhelpful.  They claim that
>the return type of a sizeof() expression is system-dependent.

Quoting from _The C Programming language_, 2nd edition, page 135:
"Strictly, sizeof produces an unsigned integer value whose type, size_t,
is defined in the header <stddef.h>."

From this statement, I do not think that in is unreasonable for DEC
to make sizeof of type unsigned since they are required for ANSI C
to return a unsigned int.

-- 
Doug O'Neal                  doug@jhuvms.hcf.jhu.edu
Homewood Academic Computing  doug@jhuvms.bitnet
Johns Hopkins University     mimsy!aplcen!jhunix!doug

henry@utzoo.uucp (Henry Spencer) (09/23/89)

In article <145@bbxsda.UUCP> scott@bbxsda.UUCP (Scott Amspoker) writes:
>Somewhere along the way sizeof() became unsigned...
>I hope the ANSI draft doesn't leave this as "implementation dependent".
>Some idiot implementor somewhere will make sizeof() return float.

The Holy Writ (Oct 88 draft) says that the type of sizeof is an unsigned
integral type.
-- 
"Where is D.D. Harriman now,   |     Henry Spencer at U of Toronto Zoology
when we really *need* him?"    | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

inst182@tuvie (Inst.f.Techn.Informatik) (10/02/89)

In article <145@bbxsda.UUCP> scott@bbxsda.UUCP (Scott Amspoker) writes:
>
>I hope the ANSI draft doesn't leave this as "implementation dependent".
>Some idiot implementor somewhere will make sizeof() return float.
							    ^^^^^
Why not? If someone would write a C compiler for the HP28 calculators,
which use 8 bit characters and 20 bit pointers, sizeof (void *) would be
2.5 :-).
>
>-- 
>Scott Amspoker
>Basis International, Albuquerque, NM
>(505) 345-5232

 _______________________________________________________________
|	__   |							|
|  |   |  \  | Peter J. Holzer					|
|  |___|__/  | Technische Universitaet Wien			|
|  |   |     |							|
|  |   |     | ...!uunet!mcvax!tuvie!asupa!honey!hjphr		|
|  ____/     |--------------------------------------------------|
|	     | Think of it as evolution in action -- Tony Rand	|
|____________|__________________________________________________|