How does strtoull work?

3

I've tried every way to understand what is the second parameter to use this function and it in general but I still can not understand.

    
asked by anonymous 21.05.2015 / 02:28

1 answer

3

The first parameter of strtoull is the string to convert.

The third parameter is the numeric basis. For numbers written in decimal is 10, for hexadecimal is 16, etc.

The second parameter has to do with the fact that strtoull ignores "invalid" characters that are outside the base you chose. For example, strtoull("123qwer456", NULL, 10) returns 123 and ignores "qwer456" .

If you want to ignore the "rest" after the numeric part, pass NULL as the second parameter. If you want to know if there are non-numeric characters at the end of the string pass the address of a char * as the second parameter.

char *resto;
unsigned long long n = strtoull("123qwer456", &resto, 10);
printf("O número é %ull, o resto da string é %s\n", n, resto);

If the string passed to pro strtoll only contains integers, "rest" is going to be worth an empty string (that is, *resto == 'man strtoull' ). Otherwise, rest points to the first invalid character of the string.

Changing the subject, if you have doubt of the documentation a good place to look is in the Opengroup site Opengroup site . And if you happen to be on Linux the documentation is already installed on your computer, just make a %code% .

    
21.05.2015 / 03:16