liveBook · Manning

Encoding

As explained in Modern C, Third Edition

An encoding is a mapping or representation that associates data values with character or textual forms.

Encoding maps character data between representations. In C, a character written by the program may become different bytes in a file or on a console; text I/O performs the conversion when supported. The source and execution environments may also use different encodings, so character constants such as 'a' and '\n', and string literals such as "hello:", provide portable representations for the current platform. section 8.4.3 section 8.5

Use these constraints when handling encoded character data in C:

  • Represent a newline with '\n' rather than assuming one file-level end-of-line byte sequence; the C library adapts it to the platform. section 8.4.3
  • Do not depend on trailing blanks or tabs surviving text output, because text conversion may suppress whitespace preceding an end of line. section 8.4.3
  • Use <ctype.h> classifiers and toupper or tolower for character classes and case conversion instead of hard-coding every platform character. section 8.5
  • Terminate hexadecimal escapes carefully: a sequence such as "\xdeBruyn" consumes following hexadecimal characters differently from "\xde""Bruyn". section 8.5

Character codes and numeric conversion

The hexatridecimal example converts an input character into a numeric digit: isdigit(a) maps '0' through '9' to 0 through 9, while toupper followed by isupper maps Latin letters to values beginning at 10. The conversion relies on decimal digits being consecutive and on isdigit not being locale-dependent.

How Modern C, Third Edition covers it

How deep it goes
Explains platform conversion, escape syntax, locale-sensitive classification, and character-to-number logic, but not a complete encoding specification.
Examples
Uses '\n', hexadecimal escapes, <ctype.h> classifiers, and a hexatridecimal character-to-number conversion.
What it assumes
Assumes familiarity with C character and string literals while teaching platform representations, escape encodings, and classification interfaces.
What you can do after
Apply it via C text and character interfaces to write portable handling for newlines, character classes, escapes, and numeric representations.
How it connects
Connects text I/O, character literals, string literals, locales, character classification, and numeric string conversion.

Also explained in

Compare how 9 books explain encoding →

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.