Skip to content

Latest commit

 

History

History
127 lines (98 loc) · 1.57 KB

File metadata and controls

127 lines (98 loc) · 1.57 KB

printf special characters

  • New line, insert a new line after printed text.
\n
  • tab, indents text to the right.
\t
printf("\tindented text\n");

Output

        indented text
  • escape, allows printing of double quotes etc.
\"
printf("\"text inside double quote\".\n");

Output

"text inside double quotes."

Watch out for this, compile error occur.

printf("\");

To print a backslash , you need to escape it first.

\\
printf("Escape symbol is \"\\\"\n");

Output

Escape symbol is "\"

printf placeholder formatting options

%[flags] [width] [.precision] specifier

printf placeholders specifiers

  • int type
%d
  • float or double types
%f
  • double type
%lf
  • char type
%c
  • strings
%s

printf field width

Field width helps to print tables.

Specify the field width for a given specifier.

Field width set to 10.

printf("|||%10d|||\n", 5);

Output

|||         5|||

printf flags

  • right align, no flag (default)
  • Left align flag is: -
printf("|||%-10d|||\n", 5);

Output

|||5         |||

printf real number precision

Define the precision of float or doubles.

For two floating points of precision. i.e modify 5.4567 to 5.46, use .2 as precision.

printf("|||%10.2f|||\n", 5.456700);

Output

|||      5.46|||

printf hexadecimal

printf("ACCEL_CONFIG bitfields = 0x%02X\n", accelConfigReg8);

Output

ACCEL_CONFIG bitfields = 0xB0