Format specifiers
Introduction
Escape codes
\a alert (causes an audible or visual alert when printed) \b backspace \f formfeed (may clear the screen on some systems, don't rely on this) \n newline (goes to the beginning of the next line; will be automatically
translated to/from \r\n or another (if any) end-of-line sequence on systems where this is necessary)
\r carriage return (goes to the beginning of the current line) \t horizontal tab \v vertical tab \\ backslash (be careful when using backslashes in literals!) \' single quote (useful inside character literals) \" double quote (useful inside string literals) \? question mark (useful to avoid unwanted trigraph translation when two
question marks are followed by certain characters)
\<octal digits> character value in octal \x<hex digits> character value in hexadecimal
Format specifiers
%d signed int variable, decimal representation (equivalent to %i) %u unsigned int variable, decimal representation %x unsigned int variable, lowercase hexadecimal representation %X unsigned int variable, uppercase hexadecimal representation %o unsigned int variable, octal representation
(there are no format specifiers for binary representation)
%f float/double, normal notation %e float/double, exponential notation (%E uses E instead of e) %g float/double, notation %f or %e chosen depending on value (%E if %G) %c character (passed as int), text representation %s string (see 10. Arrays, strings and pointers) %p pointer (see 10. Arrays, strings and pointers) %n number of characters written upto now will be written to int
that the corresponding argument points to
You can change the type of the printed variable by inserting one of the following characters between the % sign and the type character (for example: %ld for long int instead of an int).
h for d,i,o,u,x,X: short int instead of int
(the short int will be promoted to int when passed anyway) for n: store result in short int instead of int
l for d,i,o,u,x,X: long int instead of int
for n: store result in long int instead of int Do NOT use for e,E,f,F,g,G for e.g. printing doubles.
L for e,E,f,F,g,G: long double instead of float/double
There are some flags and modifiers that can be put between the % and the type character:
- left alignment, pad on the right with spaces (default=right alignment) + print plus sign if positive (default=only print minus sign if negative)
(for signed numbers only)
space print space if positive (default=only print minus sign if negative)
(for signed numbers only)
0 pad with zeros instead of with spaces (for numbers only)
- "alternate form": - o: 0 will be prepended to a non-zero result
- x/X: prepends 0x/0X to result - f/F,e/E,g/G: decimal point even if no decimals - g/G: trailing zeros are not removed
<nonzero decimal value> specify field width to which result will be padded
(this can be used together with the 0 flag)
- field width will be passed as int parameter before the actual argument
.<nonzero decimal value> specify precision (default for f/F,e/E = 6)
(for s, precision will limit the number of printed characters)
.0 no decimal point is printed for f/F,e/E .* precision will be passed as int parameter before the actual argument
Other stuff
Overall example
Example:
- Untranslated:
- msgid="I am %s and I a %d-year old\nand I'm a happy \"aMule\" user "
- msgstr=""
- Would become (translation to spanish):
- msgid="I am %s, %d years old\nand &I'm a happy \"rabitty-aMule\" user "
- msgstr="Soy %s y tengo %d años\ny soy un fel&iz usuario del \"conejillo-aMule\" "
- Explanation:
- %s and %d must be copied literally since they will be substituted in the program with some string or number. General rule: anything between a character % and the next letter-character (that is, a, b, 'c, etc...) or percentage character (%) must be copied literally.
- \n must be copied literally too since it brakes the line into a new line. The general rule is: \ and it's very next character (can be \, a, n, t, ,f, ", ', etc) must be copied literally.
- & must be placed 'before the very same letter in the translation since it indicates the combination ALT+letter that will select that option.
- The ending space is left since in the original message it was there. Never remove startng or ending spaces, even if they look ugly. They are there for some reason. Nomally this will be because either before or after that string comes another string. For example: "Opening file " has a blank space at the end, so you can expect that right after it the name of a file will be displayed. Something like Opening file server.met