What is atoi equivalent for 64bit integer(uint64_t) in C that works on both Unix and Windows?

Use strtoull if you have it or _strtoui64() with visual studio.

unsigned long long strtoull(const char *restrict str,
       char **restrict endptr, int base);


/* I am sure MS had a good reason not to name it "strtoull" or
 * "_strtoull" at least.
 */
unsigned __int64 _strtoui64(
   const char *nptr,
   char **endptr,
   int base 
);

You've tagged this question c++, so I'm assuming you might be interested in C++ solutions too. You can do this using boost::lexical_cast or std::istringstream if boost isn't available to you:

#include <boost/lexical_cast.hpp>
#include <sstream>
#include <iostream>
#include <cstdint>
#include <string>

int main() {
  uint64_t test;
  test = boost::lexical_cast<uint64_t>("594348534879");

  // or
  std::istringstream ss("48543954385");
  if (!(ss >> test))
    std::cout << "failed" << std::endl;
}

Both styles work on Windows and Linux (and others).

In C++11 there's also functions that operate on std::string, including std::stoull which you can use:

#include <string>

int main() {
  const std::string str="594348534879";
  unsigned long long v = std::stoull(str);
}

Something like...

#ifdef WINDOWS
  #define atoll(S) _atoi64(S)
#endif

..then just use atoll(). You may want to change the #ifdef WINDOWS to something else, just use something that you can rely on to indicate that atoll() is missing but atoi64() is there (at least for the scenarios you're concerned about).

Tags:

C++

C

Atoi