Reputation: 33
I have a string that contains a 14-digit number.
I want to convert the string to an int.
When I use atoi(stringName), I get the max 32-bit in limitation.
Example:
String1 contains "201400000000"
long long tempNum1;
tempNum1 = atoi(String1);
printf("%d",tempNum1);
Output is: 2147483647
How can I turn this string into a number? I want to compare it to other strings of numbers and find which is the largest. (I have three strings of 14-digit numbers, I want to find which is the smallest, which is the largest).
Upvotes: 3
Views: 1623
Reputation: 5112
try
sscanf( String1, "%lld", &TempNum1);
Note that the format specifier %lld works for C99 , if not C99 please check the docs for the compiler
Upvotes: 1
Reputation: 234785
atoi
returns an int
.
That int
is assigned to a long long
, but by then the limit has already been reached.
You want to use atoll
which returns a long long
.
And your printf format specifier is incorrect for your type.
Upvotes: 3
Reputation: 172518
You can try like this using strol:
long strtol (const char *Str, char **EndPoint, int Base)
or
long long strtoll( const char *restrict str, char **restrict str_end, int base );
The atoi says:
The call atoi(str) shall be equivalent to:
(int) strtol(str, (char **)NULL, 10)
except that the handling of errors may differ. If the value cannot be represented, the behavior is undefined.
Upvotes: 1