C printf() Function – Displaying Output on the Console

Filed Under: C Programming
C Printf() Function

Hey, all. In this article, we will be focusing on C printf() function in detail.

Be it any programming language, we always require the user interaction with the system through the console or interface. In C programming, the same can be achieved through printf() function.

So, let us begin!!


Working of C printf() function

The printf() function enables the programmer to display the output or some information on the console or interface directly. That is, it displays the formatted output on the console.

Using the printf() function, we can represent the values across different data types on the screen along with some explanatory information. The printf() function displays different values such as integer, float, string, octal values, etc.

Having understood the working of printf() function, let us now focus on the structure of the same.


Syntax of printf() function

The printf() function accepts the format specifier that gives information about the data type of the value.

Syntax:

printf("%format-specifier",variable);
  • format-specifier: It represents the data type of the value that will be printed on the screen.
  • variable: Displays the value inside this variable.

On successful compilation of the function, the printf() function returns the number of characters/values that are displayed on the screen.


Examples of printf() function

In the below example, we have displayed an integer and a float value on the console using the particular format specifiers as parameters–

#include <stdio.h>
int main()
{
    int i = 20;
    float f = 20.25;
    printf("Integer value = %d\n", i);
    printf("Float value = %f", f);
    return 0;
}

Output:

Integer value = 20
Float value = 20.250000

Now, let us display the formatted string on the console as shown below–

#include <stdio.h>
int main()
{
    printf("Hey, folks!!");
    return 0;
}

Output:

Hey, folks!!

In this example, we have tried to display the number of characters displayed on the console through the printf() function.

#include <stdio.h>
int main()
{
    float f = 21.21;
    printf("Float value : %f\n", f);
    printf("\nThe number of characters displayed by the printf() function: %d ",printf("%f", f));
    return 0;
}

Here, we pass the float value to the printf() function, which gets displayed on the screen.

Output:

Float value : 21.209999
21.209999
The number of characters displayed by the printf() function: 9 

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

Till then, Happy Learning!!


References

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages