More on printf() Statement

For Complete YouTube Video: Click Here

In this class, we will try to understand More on printf() statement.

The basic understanding of the printf() function has already made in our previous class.

Let us consider the statement below.

The information that flows the % character specifies how the value is converted from its internal binary form to printed character form.

How the values are to be printed is dependent on the character specified after the % symbol in the printf statement.

Let us make it clear by using the programming example given below.

Consider the first line of code int a = 98;

The declaration states that variable a is of integer type and the value stored in the memory allocation of a is the binary value of 98 [1100010].

Assume that the compiler allotted memory location three for variable a.

The image below shows the impact created after executing the first line of code.

More on printf() Statement 1
After executing First line of code

Now, consider the second line of code char c = ‘a’.

This declaration states that variable c is of character type and the value stored in the memory allocation of ASCII code of ‘a’.

The ASCII code of ‘a’ is 97, and its binary value of 1100001.

The image below shows the impact created after executing the second line of code.

More on printf() Statement 2
After executing second line of code

Executing the printf() Statement

Consider the third line of code.

printf(“%d %c %d %c”, a, a, c, c);

In the above printf() function, the first conversion specifier %d will fetch the binary value stored in the location allowed for variable ‘a’ and print its integer value.

As a result, 98 will be printed.

The second conversion specifier %c will fetch the binary value stored in the location allowed for variable ‘a’ and print its corresponding ASCII character.

As a result, b will be printed.

Similarly, the third and fourth conversion specifiers %d and %c will fetch the binary value stored in the location allowed for variable c.

The printf() function will print the binary value [1100010] in integer and character format.

The output of the printf() function is 98 b 97 a.