Close

Code: Hello World!

A project log for reprint: modern printf

reprint is printf redone with decades of hindsight, revamping the semantics and syntax to make a function worthy of Hello World.

analog-twoAnalog Two 03/29/2016 at 11:061 Comment

Let's look at Hello World with both printf and reprintf. Both calls produce identical output:

1 printf("%s", "Hello World!\n");
2 reprintf("\fdp", "Hello World!\n");

In line 1,

  1. '%' is an escape character directing the printf interpreter to read the following characters as format parameters.
  2. The single format parameter is 's', which corresponds to the English word "string".
  3. The type of input string is assumed to be ASCII, UTF-8, or whatever is configured for your locale and terminated with a null character.
  4. The output format of the string is also locale dependent. Note that on Windows or OSX, the preferred output is in wide strings

In line 2

  1. '\f' is the escape character directing reprintf to print human readable formatted output.
  2. 'd' indicates that the input is a string of null terminated characters. The reason for 'd' is that Strings occupy offset 4 in the Input Types section in the ASCII table (see diagram from previous log).
  3. 'p' indicates 8-bit character encoding, specifically UTF-8.

Though reprintf requires 2 extra characters for Hello World, there is more clarity to the formatting behaviour in reprint.

  1. '%' is just a normal character in reprint. Escaping should be done with, well, THE escape character '\' (as in '\n')
  2. Character byte size is exactly defined. Change 'p' to 'q' for a 16-bit character string, 'r' for a 32 bit character string, or 'w' for the wstring_t type. These other characters are just other entries in the Input Sizes section of the ASCII table.
  3. There are no external dependencies on locale.

Discussions

Eric Hertz wrote 04/01/2016 at 03:39 point

Ah hah! Thanks for this explanation.

  Are you sure? yes | no