This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Remainder ( % ) operator and GCC
- From: Ian Lance Taylor <ian at airs dot com>
- To: Ladislav Mecir <lmecir at mbox dot vol dot cz>
- Cc: GCC Help <gcc-help at gcc dot gnu dot org>
- Date: 09 Nov 2005 11:19:20 -0800
- Subject: Re: Remainder ( % ) operator and GCC
- References: <4371F73D.30201@mbox.vol.cz>
Ladislav Mecir <lmecir@mbox.vol.cz> writes:
> Could you give me an advice how to make the following program (and
> programs using many times the % operator) run reliably when compiled
> by GCC on different operating systems even when the user gives -1 as
> the B value?
>
> #include <stdio.h>
> int main(int argc, char *argv[]) {
> int a = -2147483647;
> int b;
> printf ("The value of B: ");
> scanf ("%i", &b);
> printf ("%i %% %i: %i", a, b, (a - 1) % b);
> return 0;
> }
Don't use '%'. It is machine dependent when used with a negative
number.
This is not a gcc question.
Ian