The Printf Function

In this tutorial, we will understand the printf function in details.


What is the printf Function?

Printf is short for print formatted. It is a tool that helps us show or print the information stored in a string, which is also known as a format string. In addition to this, the printf function lets us include external values or even complex expressions within the format string of our choosing.


The Syntax

The printf function follows a specific structure:

printf(format string, expr1, expr2, expr3, ...)

The format string is the first input to the printf function. It is a string that contains regular characters and format specifiers (or conversion specifications) as placeholders for multiple expressions (expr1, expr2, …). You can provide any number of expressions to the printf function.

The printf function is used to display the format string on the console screen. It acts as a way to print information. Additionally, it allows us to include external expressions in the string. These expressions can be as many as we want.

In the previous explanation, I mentioned two terms for the placeholders – format specifiers and conversion specifications. Let’s understand what they mean.


Conversion Specifications

With the printf function, we can include numerous expressions in our string using format specifiers or conversion specifications. A conversion specification begins with a % sign followed by a letter indicating the data type to be inserted.

Keep in mind that computers internally work with binary representations of data. All numbers, whether integers or floats, characters, and strings are stored in binary form. The conversion specification determines how the internal binary representation should be converted to the printed form.

Let’s consider the following code:

int age = 65;
printf("My age is %d", age);

In this example, the variable “age” is assigned the value 65. The printf function is given two inputs: the format string "My age is %d" and the “age” variable. The ordinary characters in the format string will be printed as they are, while the conversion specification %d will be replaced by the value to be printed. In this case, the binary representation of 65 (01000001) is converted to its decimal form and printed.

Now, let’s look at another code snippet:

int age = 65;
printf("My age is %c", age);

%c is the conversion specification that instructs printf to convert the internal representation of 65 to its printed form as a character. The character corresponding to the decimal code 65 is ‘A’, so the character ‘A’ will be printed instead of the value 65.

It’s worth noting that each character has a unique decimal code associated with it, which can be found in the ASCII table (ASCII stands for American Standard Code for Information Interchange).

Source: Wikimedia Commons

Character A has the code 65 and this is the reason why A will be printed after running the code.


The Order of Replacement

What happens if we provide multiple expressions to the printf function? How does the order of replacement work in such a case?

Let’s consider the following code as an example:

int age = 65, contact = 9849392401;
printf("My age is %d and my contact is %d.", age, contact);

In this code, the printf function has a format string with two conversion specifications. When multiple expressions are provided, the order of replacement follows a left-to-right sequence. This means that the first conversion specification %d will be replaced by the value of the “age” variable, and the second conversion specification %d will be replaced by the value of the “contact” variable.

Therefore, the output generated by running the above code will be:

"My age is 65 and my contact is 9849392401."


Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.