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
.
Flag | Description |
---|---|
- | Left-aligns the output |
+ | Prepends a plus for positive signed-numeric types |
␣ | Prepends a space for positive signed-numeric types |
0 | Prepends 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. |
Length | Description |
---|---|
hh | int sized integer argument promoted from a char |
h | int sized integer argument promoted from a short |
l | long sized integer argument |
ll | long long sized integer argument |
z | size_t sized integer argument |
Specifier | Description |
---|---|
d , i | a decimal signed int |
u | a decimal unsigned int |
x , X | a hexadecimal unsigned int |
o | an octal unsigned int |
f , F | a double in fixed-point notation |
e , E | a double in standard notation |
g , G | a double in normal or standard notation |
s | a NUL -terminated string |
c | a char character |
p | void* 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'
.
- Bash supports this sequence as
-
\xhh
: Consists of one or more hexadecimal digits. Thex
prefix is required to distinguish from octal escape sequences.- Bash supports this sequence as
$'\xhh'
. One or two digits is supported.
- Bash supports this sequence as
-
\uhhhh
: Introduced in C11 to represent Unicode code points. Must have exactly four hexadecimal characters specified with0
leading padding if necessary.- Bash supports this sequence as
$'uhhhh'
. One to four hex digits is supported.
- Bash supports this sequence as
-
\Uhhhhhhhh
: Introduced in C11 to represent larger unicode code points. Must have exactly eight hexadecimal characters specified with0
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.