Checksum: 03293
Lines: 41
Path: utzoo!sq!msb
From: msb@sq.uucp (Mark Brader)
Date: Fri, 29-Jan-88 18:53:48 EST
Message-ID: <1988Jan29.185348.29491@sq.uucp>
Newsgroups: comp.lang.c
Subject: Re: lint diagnostic
References: <365@osupyr.UUCP> <5304@iuvax.UUCP> <1130@jenny.cl.cam.ac.uk>
Reply-To: msb@sq.UUCP (Mark Brader)
Organization: SoftQuad Inc., Toronto

> >+defmach(lo) int lo; [...]
> >+main()
> >+{ printf("sizeof(int) = %d\n",sizeof(int));
> >+  printf("sizeof(long) = %d\n",sizeof(long));
> >+  defmach(65535);
> >+  exit(0); }
> [Execution]
> >+sizeof(int) = 4; sizeof(long) = 4; 0000ffff
> [Lint]
> >+defmach, arg. 1 used inconsistently	x.c(6)  ::  x.c(13)

> I think a more correct explaination is that you have a non-matching
> cc and lint -- lint thinks sizeof(int) == 2 so that
> 65535 has type (long int) whereas cc thinks sizeof(int)==4 ...

Maybe, or maybe just a clever lint.

One of the purposes of lint is to detect *portability errors*.
The type of the constant 65535 may be int (on a machine with, say, 18-bit
ints) or long, and it must not be used where EITHER int OR long is
specifically required, as in a pre-ANSI function call.  One should always
write 65535L in such positions, and treat the thing as long.

To paraphrase Alan Mycroft's article, integer constants above 32767 should
be treated by lint as having an unknown one of two different types.

>   E.g. int x = 99999;      is dubious if sizeof(int)==2
>        printf("%ld", 99999) is dubious if sizeof(int)==4

I'd say they're dubious any time, and *wrong* in the cases called "dubious"
above.  Anyway, all this got me wondering, and I constructed the following:

	int a(x) int x; {}
	int b(x) long x; {}
	main() {a(65535); b(65535);}

and ran lint on it here on our Sun (version 3.2).  And, yes, it did NOT
complain about the second 65535.  All the world's a land of 16 or 32 bit ints?

Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb@sq.com
#define	MSB(type)	(~(((unsigned type)-1)>>1))
