liveBook · Manning

Strtoull

As explained in Modern C

strtoull is a C standard library function that converts the initial portion of a string to an unsigned long long value in a specified base and reports where parsing ends.

Parsing one string in two bases

char *next; unsigned long long n = strtoull("0789a", &next, 10); /* n == 789; *next == 'a' */ With base 0, the leading 0 selects octal instead: parsing stops at '8', after producing 7. The second argument receives the address where parsing ends. section 14.1

strtoull converts the initial numeric portion of a string to an unsigned long long value. Its end-pointer argument lets a caller continue scanning from the first character not consumed. section 14.1

A loop can use strtoull to collect successive numbers:

  • Pass the current character pointer as the string argument and pass &next as the end-pointer argument. section 14.1
  • Advance to next after each successful conversion; stop when next equals the input pointer, indicating that no number was found. section 14.1
 97   static
 98   size_t numberline_inner(char const*restrict act,
 99                          size_t numb[restrict], int base){
100     size_t n = 0;
101     for (char* next = 0; act[0]; act = next) {
102       numb[n] = strtoull(act, &next, base);
103       if (act == next) break;
104       ++n;
105     }
106     return n;
107   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":3}],[{\"line\":0,\"ch\":6},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":3}],[{\"line\":1,\"ch\":6},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":35}],[{\"line\":3,\"ch\":8},{\"line\":3,\"ch\":14}],[{\"line\":1,\"ch\":30},{\"line\":1,\"ch\":40}],[{\"line\":1,\"ch\":41},{\"line\":1,\"ch\":49}],[{\"line\":2,\"ch\":41},{\"line\":2,\"ch\":49}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":3}],[{\"line\":1,\"ch\":6},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":35}],[{\"line\":3,\"ch\":8},{\"line\":3,\"ch\":14}],[{\"line\":1,\"ch\":41},{\"line\":1,\"ch\":49}],[{\"line\":2,\"ch\":41},{\"line\":2,\"ch\":49}],[{\"line\":2,\"ch\":52},{\"line\":2,\"ch\":55}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":3}],[{\"line\":1,\"ch\":6},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":35}],[{\"line\":3,\"ch\":8},{\"line\":3,\"ch\":14}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":3}],[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":11}],[{\"line\":1,\"ch\":30},{\"line\":1,\"ch\":34}],[{\"line\":4,\"ch\":13},{\"line\":4,\"ch\":17}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":3}],[{\"line\":5,\"ch\":20},{\"line\":5,\"ch\":28}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":3}],[{\"line\":6,\"ch\":10},{\"line\":6,\"ch\":12}],[{\"line\":6,\"ch\":27},{\"line\":6,\"ch\":32}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":3}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":3}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":3}],[{\"line\":9,\"ch\":8},{\"line\":9,\"ch\":14}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":3}]]"}
!@%STYLE%@!

Inspect the complete numberline_inner loop using strtoull, next, and the no-progress test. section 14.1

How Modern C covers it

How deep it goes
Explains conversion, bases, end pointers, loop termination, and a const-qualification weakness, but defers broader error handling.
Examples
Uses "0789a" in bases 10 and 0, then embeds strtoull in numberline_inner.
What you can do after
Apply it via the C standard library to scan successive numeric fields and detect where parsing stops.
How it connects
Connects string parsing with numeric bases, character pointers, pointer-to-pointer arguments, and dynamic arrays.

Also explained in

Compare how 2 books explain strtoull →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.