For the basics, see Mythryl tutorial, displaying text.
Escape characters ∞
\n
- newline\t
- tab\
- Literal backslash-
\r
- carriage return- Some computers use \n, some \r, some \r\n.
\a
- alert\b
- backspace\f
- formfeed-
\v
- vertical tab
Leaning Toothpick Syndrome ∞
This is ugly:
printf "%s\n" "She said \"Hello\"";
It can get much worse! Too many backslashes leads to Leaning toothpick syndrome, making your code illegible.
However, you can use the ./text/
quotation construct instead, like this:
printf "%s\n" ./She said "Hello"/;
If you have both double-quote ( "
) and slash ( /
) characters to worry about, you can use the .'text'
construct like this:
printf "%s\n" .'Displaying " and /';
The two quotation constructs have limitations though. ./text/
cannot have a slash ( /
) in it, and .'text'
cannot have a single-quote ( '
) in it. Escaping doesn't work to allow those characters either.
Constants ∞
Just like \n
becomes a newline, printf
can include 'constants' that begin with a %
(percent)
%c
- Characters%d
- Decimal%f
- Floats%e
- Exponents%g
- General%x
- hex%o
- octal-
%b
- boolean
%c
- Characters ∞
Just one single character is possible with this.
printf "The letter '%c'\n" 'a'; # => # The letter 'a'
Notice how the 'a' at the end has single-quotes.
%f
- Floats ∞
Floating point numbers. Numbers with a decimal point.
printf "%f\n" 1.2; # => # 1.200000
You can also use scientific notation with floats:
printf "%f\n" 1.2e10; # => # 12000000000.000000
You may get a different number of zeroes at the end. This will be discussed later.
Scientific Notation ∞
You can use either a lowercase e
or a capital E
to display a double value in standard form.
printf "%f\n" 1.6e2; printf "%f\n" 1.6E2; # => # 160.000000 # 160.000000
%e
- Exponents ∞
Exponents in standard notation.
printf "%f\n" 1.5e1; printf "%e\n" 1.5e1; printf "%e\n" 1.5e01; printf "%E\n" 1.5E01; # => # 15.000000 # 1.500000e01 # 1.500000e01 # 1.500000E01
Notice how %E
capitalized E01.
You may get a different number of zeroes at the end. This will be discussed later.
%g
- General ∞
General is either of %f
or %e
, as appropriate.
printf "%g\n" 1.5e100; printf "%G\n" 1.5E100; printf "%g\n" 1.5; # => # 1.5e100 # 1.5G100 # 1.5
Again, notice how %G capitalized the E100.
%x
- hex ∞
printf "%x" 13; printf "%x" 14; printf "%x" 10; printf "%x" 13; printf "%x" 11; printf "%x" 14; printf "%x" 14; printf "%x" 15; printf "\t"; printf "%x" 1087; printf "\n"; # => # deadbeef 43f
If you'd like to capitalize any characters that appear, use a capital %X
printf "%X" 1087; # => # 43F
It's a C convention that a decimal prepended with a 0x
is treated as hexadecimal. Notice we're using %d
and neither %x
nor %X
.
printf "%d" 0x12; # => # 18
%o
- octal ∞
printf "%o" 1087; # => # 2077
It's a C convention that a decimal prepended with a 0
is treated as octal. Notice we're using %d
and not %o
.
printf "%d\n" 012; # => # 10
%b
- boolean ∞
Boolean TRUE/FALSE is %b
and TODO %b (boolean) will be described another time.
Mixing constants together ∞
Everything can be mixed together.
printf "Character %c, decimal %d, and Floats %f and %f\n" 'a' 1 1.2 1.0e2; # => # Character a, decimal 1, and Floats 1.200000 and 100.000000
Remember what we can do with long strings:
printf "Character %c, decimal %d, and \ \Floats %f and %f\n" 'a' 1 1.2 1.0e2; # => # Character a, decimal 1, and Floats 1.200000 and 100.000000
TODO: Other constants for printf
∞
In general most of the C library printf syntax is supported, where it makes sense in the Mythryl context.
Unlike in C, where the compiler will freely interconvert between different size ints behind your back in order to make a printf()
do the right thing, in Mythryl %d
etc are specifically Int, and other integer types must be converted to Int before you can use printf with them.
(Mythryl is strict)
printf
and field width ∞
The following example prints four fields. One of those fields is a string using a fixed width of ten characters, using %10s
.
printf "%s" "1234567890"; printf "%s" " right"; printf "\n"; printf "%10s" "text"; printf "%s" " right"; # => # 1234567890 right # text right
You might expect that if too-long text is put into a too-small field, the text would be truncated.
printf "%2s" "a long string"; # => # a long string
The full text is displayed.
You can specify the width for anything, not just strings. Here it is again with a decimal.
printf "%10d" 1; printf "%s" "right\n"; # => 1
You can left-align using the -
(dash) 'flag'.
printf "%-10s" "left"; printf "%s" "right"; # => left right
Last updated 2017-11-19 at 02:51:27