type of long % int

guido at boring.UUCP guido at boring.UUCP
Fri Dec 28 09:10:35 AEST 1984


Since the result of a%b can never exceed abs(b) in absolute value, it would
be reasonable that a%b gets the type of b, not of a.  This is more reasonable
in an example like this:

	fiddle(diskaddr) long diskaddr; {
		int offset= diskaddr%BUFSIZ;
		...
	}

where which lint currently complaints about a long to int assignment.

I can see only one disadvantage (besides it being a strange exception to
an otherwise simple rule: "the type  of the result of any arithmetic
operation is the type of the 'larger' operand"): it breaks programs that
say, for instance,

	/* Assume int a, b; */
	foobar((long)a%b); /* foobar requires a 'long' argument */

instead of

	foobar((long)(a%b));

which is better anyway because it casts at the right moment; but the former
is currently also correct (passes lint).

Any comments?  Ansi committee?

	Guido van Rossum, "Stamp Out BASIC" Committee, CWI, Amsterdam
	guido at boring.UUCP



More information about the Comp.lang.c mailing list