How to convert string to long long in c

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

Answers


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"


  1. use unsigned long long instead of long long

  2. use strtoull instead of strtoumax

  3. use printf("%llu\n",val1);


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.


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.


Need Your Help

Change RootViewController inside UISplitView

ios ipad uisplitviewcontroller uisplitview

I'm developing a little example for iPad from a UISplitView XCode Template. It's formed by a root controller wich is shown in the left of the window and a detail view wich is shown in the right.

With JS, is it possible to block the loading of images that are statically defined in the HTML?

javascript ajax

So I have a hand-coded one-pager site with a very long length and dozens of images. Let's skip the discussion on whether that's a great strategy, but the situation is is that rendering all these im...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.