Overview

A contiguous sequence of characters terminated by the NUL character (refer to ASCII). Text data is said to be more platform-independent than binary data since it is unaffected by word size or byte ordering.

printf

The syntax for the format placeholder is %[flags][width][.precision][length]specifier.

FlagDescription
-Left-aligns the output
+Prepends a plus for positive signed-numeric types
Prepends a space for positive signed-numeric types
0Prepends zeros for numeric types
#For g and G, trailing zeros are not removed. For f, F, e, E, g, and G, output always has a decimal point. For o, x, and X, the text 0, 0x, and 0X is prepended to nonzero numbers respectively.
LengthDescription
hhint sized integer argument promoted from a char
hint sized integer argument promoted from a short
llong sized integer argument
lllong long sized integer argument
zsize_t sized integer argument
SpecifierDescription
d, ia decimal signed int
ua decimal unsigned int
x, Xa hexadecimal unsigned int
oan octal unsigned int
f, Fa double in fixed-point notation
e, Ea double in standard notation
g, Ga double in normal or standard notation
sa NUL-terminated string
ca char character
pvoid* address in an implementation-defined format

Escape Sequences

C has a standard for processing different escape sequences. Many languages built with C in mind parse these escape sequences in a similar way.

  • \ooo: Consists of one to three octal digits.

    • Bash supports this sequence as $'\ooo'.
  • \xhh: Consists of one or more hexadecimal digits. The x prefix is required to distinguish from octal escape sequences.

    • Bash supports this sequence as $'\xhh'. One or two digits is supported.
  • \uhhhh: Introduced in C11 to represent Unicode code points. Must have exactly four hexadecimal characters specified with 0 leading padding if necessary.

    • Bash supports this sequence as $'uhhhh'. One to four hex digits is supported.
  • \Uhhhhhhhh: Introduced in C11 to represent larger unicode code points. Must have exactly eight hexadecimal characters specified with 0 leading padding if necessary.

Bibliography

  • Arnold D. Robbins, “GAWK: Effective AWK Programming,” October 2023, https://www.gnu.org/software/gawk/manual/gawk.pdf.
  • Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988).
  • Bryant, Randal E., and David O’Hallaron. Computer Systems: A Programmer’s Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
  • Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
  • Mendel Cooper, “Advanced Bash-Scripting Guide,” n.d., 916.
  • “Printf,” in Wikipedia, January 18, 2024, https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962.