liveBook · Manning

Strtoul

As explained in Modern C, Third Edition

strtoul is a C standard-library function that converts the initial part of a string to an unsigned long value using an end-pointer and integer-base parameter.

strtoul converts the initial numeric portion of a string to unsigned long. Its three parameters are the input string, an optional end-pointer, and the integer base. section 8.5

Parsing a command-line number

int main(int argc, char *argv[argc + 1]) { if (argc < 3) return EXIT_FAILURE; size_t len = strtoul(argv[1], nullptr, 0); double piv = strtod(argv[2], nullptr); /* ... */ } Here, nullptr discards the position of remaining input, while base 0 selects the apparent notation: binary with a leading 0b, hexadecimal with 0x, octal with 0, and decimal otherwise. section 17.5

Interpret the call through these rules:

  • Pass an explicit base such as 2, 8, 10, or 16 when the accepted notation should be constrained. section 8.5
  • Provide a non-null end-pointer when the caller must locate data remaining after the converted number; otherwise pass a null pointer. section 8.5 section 17.5
  • Remember that conversion considers whitespace, an optional sign, the number, and remaining data; numeric character interpretation depends on the execution character set. section 8.5
  • Wrap repeated defaults only deliberately: a variadic macro can supply nullptr for endptr and 0 for base while preserving explicit three-argument calls. section 17.5

How Modern C, Third Edition covers it

How deep it goes
Explains the signature, input parts, bases, end-pointer, and wrapper macros without fully developing conversion internals.
Examples
Uses command-line parsing, strtod comparisons, function prototypes, and macros that supply omitted endptr and base arguments.
What you can do after
Apply it via the C standard library to parse integer strings with explicit bases or automatic notation detection.
How it connects
Connects string processing, numeric conversion, end pointers, integer bases, and variadic macros.

Also explained in

Compare how 2 books explain strtoul →

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.