### Why do I have this problem?

Here is the code for my program:

#include <stdio.h>
#include <conio.h>

int main(void)
{
int num1;
int num2;
printf("Enter 2 numbers");
scanf("%d%d", &num1, &num2);

if(num1 == num2){
printf("they are equal");

}
if(num1 < num2){
printf("%d is less than %d", num1, num2);

}
if(num1 > num2){
printf("%d is greater than %d", num1, num2);

}

getch();
}
--------------------------- END OF CODE ----------------------------------------

So It works for small numbers, but when I enter large numbers, it messes up.

here are examples

small number : http://postimage.org/image/7qvgf1mkn/

large number: http://postimage.org/image/r5f5yoju7/

how can i fix this?
Ints have a memory range. They can only store up to a certain number (the amount depends on the compiler your using.)

To be exact, 32 bit signed ints have a range from -2,147,483,648 - 2,147,483,647. 32 bit unsigned ints have a range from 0-4,294,967,295. 64 bit ints have a much larger possible range, signed goes from -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 and unsigned goes from 0-18,446,744,073,709,551,615. If you want to handle arbitrarily large numbers you'll have to look into arbitrary precision math libraries for C++.

Most likely what's happening is you're using a 32-bit signed (default) int, whose max value is 2,147,483,647. Add one and you actually get -2,147,483,648 (overflow).
Last edited on
Topic archived. No new replies allowed.