How does strtoull work?

Asked

Viewed 62 times

3

I have tried anyway to understand what is the second parameter to use this function and it in general but I still can not understand.

1 answer

3

The first parameter of strtoull is the string to be converted.

The third parameter is the numerical 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 have chosen. For example, strtoull("123qwer456", NULL, 10) returns 123 and ignores the "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 pro strtoll contains only integers, "rest" will be set to an empty string (this is, *resto == '\0'). 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 site of Opengroup. And if you happen to be on Linux the documentation is already installed on your computer, just do a man strtoull.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.