liveBook · Manning

Strtoul

As explained in Modern C

strtoul is a C standard library function that converts the initial portion of a string to an unsigned long value in a specified base.

A command-line length can be converted directly: `size_t len = strtoul(argv[1], 0, 0);`. The two zero arguments select the common defaults. section 16.4

The declaration `unsigned long strtoul(char const nptr[restrict], char** restrict endptr, int base);` converts the initial numeric portion of a string to `unsigned long`. `endptr` can receive the position where conversion stops; passing `0` ignores that feature. section 8.4

TAKEAWAY: choose the base deliberately, especially when input may use several numeric notations.

  • Pass `10`, `8`, or `16` for decimal, octal, or hexadecimal interpretation. section 8.4
  • Pass `0` to select the usual notation: `7` is decimal, `007` octal, and `0x7` hexadecimal. section 8.4 section 16.4
  • Interpretation proceeds through whitespace, an optional sign, the number, and remaining data; overflow handling sets `errno` to `ERANGE` in the implementation shown. section 8.4

Explicit and defaulted calls

`strtoul(argv[1], 0, 0)` accepts the input format selected by its prefix. When the stopping position matters, use all parameters explicitly, as in `strtoul(argv[1], ptr, 10)`. section 16.4

unsigned long int strtoul(char const nptr[restrict],
                          char** restrict endptr,
                          int base);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":17}],[{\"line\":0,\"ch\":18},{\"line\":0,\"ch\":25}],[{\"line\":0,\"ch\":26},{\"line\":0,\"ch\":36}],[{\"line\":0,\"ch\":42},{\"line\":0,\"ch\":50}],[{\"line\":1,\"ch\":33},{\"line\":1,\"ch\":41}],[{\"line\":0,\"ch\":26},{\"line\":0,\"ch\":30}],[{\"line\":1,\"ch\":26},{\"line\":1,\"ch\":30}],[{\"line\":0,\"ch\":42},{\"line\":0,\"ch\":50}],[{\"line\":1,\"ch\":33},{\"line\":1,\"ch\":41}],[{\"line\":0,\"ch\":14},{\"line\":0,\"ch\":17}],[{\"line\":2,\"ch\":26},{\"line\":2,\"ch\":29}]]"}
!@%STYLE%@!

Use the declaration to inspect the string, end-pointer, and base parameters together. section 8.4

How Modern C covers it

How deep it goes
Explains parameters, base selection, input parts, and overflow handling, but defers detailed end-pointer use.
Examples
Uses command-line length conversion and contrasts omitted defaults with explicit end-pointer and decimal-base arguments.
What you can do after
Apply it via the C standard library to parse integer strings with explicit or notation-selected bases.
How it connects
Connects string conversion with numeric bases, command-line arguments, errno, and function-like 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. The section links point to the parts of the book it draws on.