The printf() function is the C-style method of printing output to the screen.
It can take multiple arguments. The first argument is always a format string.
The format string can be either just a plain string (in which case it is the
only argument to printf) or a string with formatting tokens embedded in it.
The tokens dictate how to display certain datatypes. It takes the form:
%[col][.prec]type. The type is d for an integer,
f for a double and c for a character. The column
width and precision are optional.
Examples:
%d
%f
%.2f
%10d
%10.3f
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int num1, num2;
double dnum1, dnum2;
printf("Enter two integers: ");
cin >> num1 >> num2;
print("Enter two doubles: ");
cin >> dnum1 >> dnum2;
print("%d %d %f %f.\n", num1, num2, dnum1, dnum2);
print("%10d %10d %10f %10f.\n", num1, num2, dnum1, dnum2);
print("%10d %10d %10.2f %10.3f.\n", num1, num2, dnum1, dnum2);
return 0;
}
Now alter that file and try the following formatting changes:
d to:
o - will display the octal value, eg %10o
x - will display the hexadecimal value, eg %10x
%-10d - will do left justification
%010d - will print out "0000000009" when told to display 9 for example.