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,
- '%' is an escape character directing the printf interpreter to read the following characters as format parameters.
- The single format parameter is 's', which corresponds to the English word "string".
- 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.
- 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
- '\f' is the escape character directing reprintf to print human readable formatted output.
- '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).
- '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.
- '%' is just a normal character in reprint. Escaping should be done with, well, THE escape character '\' (as in '\n')
- 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.
- There are no external dependencies on locale.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
Ah hah! Thanks for this explanation.
Are you sure? yes | no