L
Lew Pitcher
All solutions I've seen on this newsgroup (at least all trivial
solutions) have something wrong. Many of these solutions were
discussed in non-homework threads.
Here is a solution that doesn't suffer from overflow issues. It is neither
very efficient, nor very elegant, but does solve the OP's problem without
encountering chance overflows, depending on specific values for sizeof(int)
or a specific format for negative integers, or invoking additional
functions (either user-coded or C standard library). And, of course, it
does not use any of the OP's "forbidden" operations.
Pick away, pedants ;-)
/*
** count downward from INT_MAX until
** the count matches either a or b
** the count will be the maximum of a or b
*/
int MyMax(int a, int b)
{
int c;
for (c = INT_MAX; (c-a) && (c-b); --c) ;
return c;
}
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------