12.2k views
3 votes
What are some of the formats used with the printf function?

1 Answer

2 votes

Final answer:

The printf function uses format specifiers such as %d, %u, %f, %c, %s, %x, %X, %p, %e, and %g to print various data types in formatted output. These can be further modified with flags, width, precision, and length modifiers for more control.

Step-by-step explanation:

The printf function in programming languages such as C and C++ is used to print formatted output to the screen. When using printf, you specify a string that contains text to be printed along with format specifiers that dictate how various variables are formatted when printed. Here are some of the common format specifiers used with printf:

  • %d or %i: Used to print an integer value.
  • %u: Used to print an unsigned integer.
  • %f: Used to print a floating-point number.
  • %c: Used to print a single character.
  • %s: Used to print a string.
  • %x or %X: Used to print a number in hexadecimal notation (lowercase or uppercase, respectively).
  • %p: Used to print a pointer address in hexadecimal.
  • %e or %E: Used to print scientific notation (lowercase 'e' for %e, uppercase 'E' for %E).
  • %g or %G: Uses %f or %e (%E) whichever gives shorter output.

Each of these format specifiers can be combined with flags, width, precision, and length modifiers to control the output's format even more precisely.

User Trung Ta
by
8.5k points