Search code examples
clong-long

Parse a string as a (long long) integer


I am writing a code in which I need to parse a string to a "long long int"

I used to use atoi when changing from string to int, I dont think it still work. What Can I use now?

--Thanks


Solution

  • Use strtoll() (man page):

    #include <stdlib.h>
    
    long long int n = strtoll(s, NULL, 0);
    

    (This is only available in C99 and C11, not in C89.) The third argument is the number base for the conversion, and 0 means "automatic", i.e. decimal, octal or hexadecimal are selected depending on the usual conventions (10, 010, 0x10). Just be mindful of that in case your string starts with 0.