Printf stands for "print formated" is an built-in function defined in stdio.h header file that prints the output on the screen supplied with proper formated string. Basically formated string consists of text, speical character codes for format specifiers and arguments in sequence.
printf("format string", argument);
The first format string argument determines how many arguments are there and are of what data types. Second argument is the list of variable names seperated by commas in correct sequence, if wrong will result wrong output.
Eg: printf("Value of a and b = %d : %d", a,b);
%i or %d
Some printf statements
printf("%s\n", "My sixth statement");
printf("%30s\n", "My seventh statement");
printf("%.12s\n", My eighth statement");
printf("%-20s\n", My ninth statement");
prinft("%10.6s\n", My tenth statement");
printf("Octal of 31 : %o\n", 31);
printf("Hexamdecimal of 52 : %x\n, 52);
Output of statements
My sixth statement
My seventh statement
My eighth st
My ninth statement
Octal of 31 : 37
Hexamdecimal of 52 : 34
Value of a & b are declared with their data types and \n is used to enter a new line in each statement.
In first printf statement %d prints the value of a in decimal integer.
Second statement prints the value of a in decimal integer with width of at least 4 wide that creates two blank spaces at the start.
%f prints floating point value of b.
%.2f prints floating point with a precision of 2 after decimal point.
%4.1f prints floating point with width of four and precision of 1, so there is one space at the start.
%s prints the contents within double quotes as it is.
%30s as string content is 20 characters, an output should be 30 characters wide it adds 10 blank spaces at the start.
%.12s prints the first twelve characters of the string.
%-20s does opposite of seventh statement, it adds two blank spaces at the end as string characters are eighteen.
%10.6s prints six characters from the string by adding four blank spaces at the start, as the output should be of ten characters wide. Similarly if it is %-10.6s, it will add blankspaces at the end.