liveBook · Manning

Numeric base

As explained in Modern C

A number base is the radix used to interpret the positional digits of a numeric string.

A numeric base, or radix, determines how positional digits in a numeric string are interpreted. C conversion functions such as strtoull and strtoul receive the base as an integer parameter. section 14.1 section 16.4

One string, different bases

Calling strtoull("0789a", &next, 10) reads 0789 and stops at 'a'. Calling it with base 0 applies the special prefix rules: the leading '0' selects octal, so parsing stops at '8'. Thus the same characters produce different results depending on the base. section 14.1

The base parameter supports two ordinary modes and one automatic mode:

  • Use a positive base, such as 10, to interpret the string in that specified base. section 14.1
  • Use base 0 to infer the effective base from the prefix: hexadecimal for a leading 0x, octal for a leading 0, and decimal otherwise. section 16.4
  • Track the first character that cannot belong to the selected base through strtoull's end-pointer argument. 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 parsing loop that repeatedly passes the chosen base to strtoull and advances through the input using the returned end pointer. section 14.1

How Modern C covers it

How deep it goes
Explains specified and prefix-deduced bases with worked parsing boundaries, but defers conversion-function internals.
Examples
Demonstrates base 10 and base 0 on "0789a", then applies base 0 to a command-line argument.
What it assumes
Assumes familiarity with C library conversion functions and their string, end-pointer, and base parameters.
What you can do after
Apply it via strtoul or strtoull to choose explicit bases or prefix-based interpretation for numeric strings.
How it connects
Connects numeric bases with string conversion, command-line arguments, end pointers, and C library functions.

Also explained in

Compare how 2 books explain numeric base →

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.