C Program#121 : Printing mixed data type values in single printf().

Printing mixed data type values in single printf():

We can print mixed data type values in a single printf() using appropriate format specifiers for the “control string” with appropriate arguments. Arguments must aligned with their format specifiers. e.g., if “%d,%s,%f” is control string then arguments must be int, string and then floating point values or variables.

Syntax :
printf("control string/text",arguments);

  1. [control string] – Format Specifiers%d , %i, %f, %u, %s, %ld etc.
  2. [text] – Additional text or Prompt message along with data (if any).
  3. [arguments] – Values or Variable, Separated using Comma (,).

Example :
printf("\nItem  %d : %f",itemcode,itemprice)
Here in the example above

  1. [ %d ] – First used, so there must be first argument after [“,] an integer (i.e. itemcode).
  2. [ %f ] – Second used, thus second argument must be afloat (i.e. itemprice).

Note: Arguments must match in (1) Type (2) Order (3) Number of arguments.

Here is an example program explaining the same.

Here is the output of the program: