Page 1 of 1

Something's terribly wrong with long long ints

Posted: Sun Nov 17, 2002 9:22 am
by ec3_limz
To any kind soul,

I have a very wierd problem when handling long long ints. Consider the following code.

[cpp]#include <stdio.h>
int main() {
long long int a,b;
scanf("%d%d",&a,&b);
if (a>b)
printf("%d",a-b);
else
printf("%d",b-a);
return 0;
}[/cpp]

Now suppose I input: 2147483648 0

The output is: -2147483648

??!!!!!! The output's supposed to be positive! There is really something very wrong with comparing long long ints.

Pls help me.

Posted: Sun Nov 17, 2002 1:53 pm
by Jordan Gordeev
You should use
[cpp]scanf("%lld%lld", &a, &b);[/cpp]
or
[cpp]scanf("%Ld%Ld", &a, &b);[/cpp]
when reading long long ints and
[cpp]printf("%lld %lld\n", a, b)[/cpp]
or
[cpp]printf("%Ld %Ld\n", a, b)[/cpp]
when writing them.