Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

What is the general syntax of a printf statement?

+7 votes
I was just wondering what the general syntax of a printf statement and maybe of an example of how it could be used.
asked Feb 15, 2015 in (Winter 14) by Nowlan Savage (100 points)

1 Answer

+6 votes

The general formula for a printf statement is: System.out.printf(<format string>, <parameter>, ..., <parameter>); 

A format string is like a normal String, except that it can contain placeholders called format specifiers that allow you to specify a location where a variable’s value should be inserted, along with the format you’d like to give that value. Format specifiers begin with a % sign and end with a letter specifying the kind of value, such as d for decimal integers (int) or f for floating-point numbers (real numbers of type double). You then signify the parameters that will replace the format specifiers. (pg. 259)

You can find different format specifiers on page 260 in the textbook!


answered Feb 15, 2015 by Lauren Johnson (100 points)