Something's terribly wrong with long long ints

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

Something's terribly wrong with long long ints

Post 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.
Jordan Gordeev
New poster
Posts: 14
Joined: Tue Nov 12, 2002 6:04 pm
Location: Bulgaria

Post 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.
Post Reply

Return to “C++”