Recently I tried to write a code that searches for a certain number in a given n.
The code worked perfectly but when I tried to insert n with more than 10 digits it got realy bad.
Apparently it had nothing to do with the code, I just couldn’t insert more than 10 digits.
I must be missing something…
For example this simple code
#include <stdio.h>
int main()
{
long int n;
scanf("%ld", &n);
printf("%ld", n);
return 0;
}
If I feed it 1111111111 it would print the same thing becuase its less than 11 digits
If I try to feed it 11111111111 it would give me something like -1773790777
Why is this happening to me? what am I doing wrong…
I’m guessing you are on a 32-bit machine, which means that the
long intis only 32 bits, which means it can hold values between minus 2 billion to plus 2 billion. When you try to enter much more than that, the value wraps over.You should start using a 64-bit value like
long long intif your compiler supports it.