top of page

Format Specifiers (%_)

Format specifiers in the form of "% (something)" are used to tell the printf or scanf, what input or output it should be expecting. printf() stands for print formatted and in the parameters, you will be telling this function what it should expect to print (i.e. format specifiers) and what should be printed (i.e. data). Same goes for scanf(), it stands for scan formatted. This article will mainly dicusss more about the use of format specifiers in OUTPUT.

The most general form is:

%[parameter][flags][width][.precision][length]type

*Only type field is necessary, the others are optional.

In the general form, each field (parameter, flags, width, precision, length, type) is to be replaced by numbers or characters that represent certain characteristics of the format.

Using formatted outputs allows you to output dynamically--you can put a placeholder in the printf statement that outputs the value of a variable (not constant), and each time this printf statement runs, it can output different values at that that placeholder position depending on your code. Also note that you can have as many placeholder (each having its own format specifiers) in a single printf or scanf statement. And if you have more than one placeholder, they corresponds to the respective values after the actual output statement. For example,

printf("1 is %d, 2 is %d & 3 is %d", 1, 2, 3);

screen display: 1 is 1, 2 is 2 & 3 is 3

Type Field (necessary field)

This field indicates to the compiler the data type of the output that it should expect and display at that placeholder position.

Table showing the list of common Type field characters together with some example codes and outputs.

Length Field

This field, if filled in, typically promotes the current data type (as specified in the type field) into a variable with more precision/range.

Table showing the list of common Length field characters together with some example codes and outputs.

.Precision Field

This field, if filled in, is used to limit the number of characters or decimal places in the output. This is useful in cases where you need to limit the number of characters or decimal places in the output so that the outputs when printed out will be in-line or in the same column.

Table showing the list of common .Precision field characters together with some example codes and outputs.

Width Field

This field, if filled in, is used to state the minimum number of characters in the output. If there are less characters in the output, these will be filled with spaces or '0's. However, if the ouput has more characters to output than the specified number of characters, printf statement will still output everything it needs to output (i.e. display more characters than specified as the minimum width).

Table showing the list of common Width field characters together with some example codes and outputs.

Flags Field

This field, if filled in, is used mainly for styling purposes; to align the output to the left or right, or to replace spaces with '0's.

Table showing the list of common Flags field characters together with some example codes and outputs.

When all the above fields are used together properly, you will be able to create a nicely formatted output. And having a good formatted output is important to ensure good readability and styling.

That's all for the topic on for, while and do-while loops. If you have any doubts, opinions or suggestions, feel free to leave them in forum section or email me @ KYX@outlook.sg. Thanks~

References

This article was written with reference from the following websites to ensure high accuracy of content written here:

1) https://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html#Formatted-Output

2) https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx

If you are interested to read more on formatted output, please check out the first website and if you are interested to find out more about data types and their ranges, check out the second website.

Related Posts

See All

MH1810-Math I 2016-2017 Semester 2

Question 1) A matrix is only invertible when its determinant is non-zero. Find the determinant of the 3 x 3 matrix with the unknown x inside using (1) Cofactor expansion or (2) 3x3 matrix determinant

FE1008/CY1402-"Error: stray '\240' in program"

If you have been copy-and-pasting my code, you might have faced this error when you compile using any IDE. This error is due to the presence of a special character ('\204') in the code that you have c

Recent Posts

Related Posts

Leave your comments or questions below:

bottom of page