Reputation:
How to convert string to long long
int main() {
char **ptr;
long long val1 = strtoumax("1234567890123456",ptr, 10);
printf("%ull\n",val1);
}
Its prints only 1015724736ll. How can I make it to print 1234567890123456
Upvotes: 1
Views: 2232
Reputation: 229204
Use strtoll , strtoumax is for converting to a uintmax_t, which is usually not a long long.
Note that your printf format specifier is wrong too, there's no such thing as "%ull". "%ull" will get interpreted as the format specfier "%u" and an ordinary string of "ll". Passing an long long to be formatted as an unsigned int("%u") will likely give you unpredictable results.
You need to use
printf("%lld\n", val1);
If you had an unsigned long long, you'd use "%llu"
Upvotes: 4
Reputation: 154305
There are a number of issues.
Instead of printf("%ull\n",val1);
use printf("%llu\n",val1);
. Put "ll" first.
ptr
should be char *ptr
and then foo(..., &ptr, 10)
. (this prevents bad things as this parameters needs to point to memory you own.)
long long val1
should be unsigned long long val1
- you appear to want to use unsigned integers in the rest of the code
strtoumax
should be strtoull
- to match your destination type of unsigned long long
.
Upvotes: 1
Reputation: 122443
You can use strtoll
.
And for the format specifier in printf
, use %ll
, u
stands for unsigned, which val1
isn't.
If unsigned long long
is what you want, declare val1
as so, and use strtoull
accordingly.
Upvotes: 0
Reputation: 3405
use unsigned long long
instead of long long
use strtoull
instead of strtoumax
use printf("%llu\n",val1);
Upvotes: 2