C PROGRAMMING % FORMAT SPECIFIERS (e.g. printf) OR PRINT FORMAT STRING ON C

SPECIFIER SYNTAX:  %[flag][field_width][.[precision]][l]conversion_char

u        -Display an unsigned integer in decimal notation.
o        -Display an integer in unsigned octal notation.
x, X   -Display an integer in unsigned hexadecimal notation. Use x for lowercase output
and X for uppercase output.
c        -Display a single character (the argument gives the character’s ASCII code).
e, E   -Display a float or double in scientific notation (for example, 123.45 is displayed as
1.234500e+002). Six digits are displayed to the right of the decimal point unless
another precision is specified with the f specifier. Use e or E to control the case of
output.
f       -Display a float or double in decimal notation (for example, 123.45 is displayed as
123.450000). Six digits are displayed to the right of the decimal point unless
another precision is specified.
g, G  -Use e, E, or f format. The e or E format is used if the exponent is less than -3 or
greater than the precision (which defaults to 6). f format is used otherwise. Trailing
zeros are truncated.
n     -Nothing is displayed. The argument corresponding to an n conversion command is
a pointer to type int. The printf() function assigns to this variable the number of
characters output so far.
s     -Display a string. The argument is a pointer to char. Characters are displayed until a
null character is encountered or the number of characters specified by precision
(which defaults to 32767) is displayed. The terminating null character is not
output.
%    -Display the % character.

PRINT SYNTAX:  printf( format-string[,arguments,…]);

Specifier Meaning Types Converted
%c Single character char
%d Signed decimal integer int, short
%ld Signed long decimal integer long
%f Decimal floating-point number float, double
%s Character string char arrays
%u Unsigned decimal integer unsigned int, unsigned short
%lu Unsigned long decimal integer unsigned long
%% self %
%\x -%X int unsigned hex value hexadecimal
%\o  int unsigned octal value octal
%p pointer address pointer

 

  • \\ (backslash)
  • \a bell (alert)
  • \n (newline)
  • \t (horizontal tab)
  • \v (vertical tab)
  • \f (new page)
  • \b (backspace)
  • \r (carriage return)
  • \n (newline)
  • \’ (single quote)
  • \”(dougle quote)
  • (terminator or null character)

%d      print as decimal integer
%6d    print as decimal integer, at least 6 characters wide
%f       print as floating point
%6f     print as floating point, at least 6 characters wide
%.2f    print as floating point, 2 characters after decimal point
%6.2f  print as floating point, at least 6 wide and 2 after decimal point

EXAMPLE CODE: Code Source

 printf("Color %s, number1 %d, number2 %05d, hex %#x, float %5.2f, unsigned value %u.\n",
        "red", 123456, 89, 255, 3.14159, 250);

will print the following line (including new-line character, \n):

Color red, number1 123456, number2 00089, hex 0xff, float  3.14, unsigned value 250.
Advertisements
This entry was posted in C. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s