liveBook · Manning

Strtoull

As explained in Modern C, Third Edition

strtoull is a C standard-library function that converts the initial part of a string to an unsigned long long integer using a specified or inferred base.

strtoull converts the initial numeric portion of a string to an unsigned long long value. Its third argument selects the base, while its second argument receives a pointer to the first character not consumed. section 14.1

Parsing repeated numbers

A loop can advance through a string by passing the address of next and then assigning act = next:\n\nfor (char* next = nullptr; act[0]; act = next) {\n numb[n] = strtoull(act, &next, base);\n if (act == next) break;\n ++n;\n}\n\nIf no number is found, next equals act, so the loop stops. Otherwise, next identifies the parsing boundary. section 14.1

The base controls which characters belong to the number:

  • Base 10 parses 0789a as 789 and stops at a. section 14.1
  • Base 8 parses 0789a as 7 and stops at 8. section 14.1
  • Base 16 parses the hexadecimal digits before the terminating character. section 14.1
  • Base 0 infers the effective base from the first character or first two characters; a leading 0 therefore makes 0789a octal. section 14.1

Use strtoull to parse input, not to discover the boundaries of numbers originating from C literals. Literal spelling can diverge from string-processing syntax: C23 digit separators may cause parsing to stop much earlier than intended. section 8.5.1

How Modern C, Third Edition covers it

How deep it goes
Explains base selection, stopping characters, no-conversion detection, and end-pointer iteration, but not full error-range handling.
Examples
Uses repeated line parsing, the input 0789a under several bases, and generated ULLONG_MAX spellings with and without digit separators.
What it assumes
Assumes familiarity with C strings, pointers, and loops, while demonstrating pointer-to-pointer use for the end position.
What you can do after
Apply it via the C library to parse one number or iterate through multiple numbers using the returned end pointer.
How it connects
Connects string conversion with pointer parameters, string termination, base prefixes, and dynamically collected numeric 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, Third Edition. The section links point to the parts of the book it draws on.