The printf command takes one or more parameters. The first parameter is a string to print out. But before the string is printed, certain character sequences, called control codes, are replaced using data from the remaining parameters to the printf call. The first control code is replaced by the second parameter, the second control code is replaced by the third parameter, and so on.
For example, the following code
name = "Matthew"
cust_num = 7
printf("Hello %s, you are customer number %d today!", name, cust_num)
results in the following output:
Hello Matthew, you are customer number 7 today!
Control codes consist of a percent sign ('%') and one or more digits and letters. In the simplest form, control codes consist of a percent sign and a single letter. For example, to print string data, you can use the code %s, as illustrated above. The %s control code will also print numeric data. However, the various numeric control codes will generate an error if you try to use them to print non-numeric data.
To print an integer, use %d. More precisely, %d prints only the integral part of the data. So printf("%d", 23.234) will simply print 23. Note this example also indicates that the additional parameters do not have to be variables, they can be any expression.
To print out numeric data containing decimal information, use %f. You can limit the number of decimal positions printed by inserting a period '.' and number between the percent sign and the 'f'. For example, to print out only two decimal places, printf("%.2f", 23.123456) yields 23.12, while printf(".4f", 23.123456) yields 23.1234 The appropriate number of zeroes and appended so that you always get exactly the number of decimal places specified in the control code.
There are a number of other control codes available. See the documentation for more information. I will mention one other option, the control code 'e' will print numeric data using scientific notation. A capital 'E' also does scientifc notation with the exponent sign capitalized. Hence, printf("%.2e", 17.123456) yields 1.71e+01 while printf("%.2E", 17.123456) yields 1.71E+01.