C Programming

What is printf() Function in C Programming

C is one of the popular programming languages that has broad applications in software development. One of its significant functions in C is the printf function, which is widely used for displaying output on the console. If you don’t know about printf() function in C, follow this article where we will get into the details of the printf() function, its syntax, and its use in C programming.

What is printf() in C

The printf() function is included in the C standard library and is widely adopted in a program to display output on the console. This function accepts any type of input provided inside the closed brackets. However, the users must specify the type of output using the format specifiers. Without using any format specifiers, the printf() function will fail to generate an output on the console.

The following is the syntax to use printf() function in C programming.

printf(format, arg1, arg2, ...);

What are the Parameters of printf() Function

The printf() function accepts the following arguments.

  • Format: A pointer to a null-terminated string written to the file stream. It is made up of characters and an extra format specifier that starts with %.
  • Additional arguments: Other arguments describing data to be printed. They appear in the format specifier’s order.

What Does a Format Specifier Include

The parts of the format specifier are given as:

  • A leading sign %.
  • One or more than one flags modifying conversion behavior (optional).
  • If no sign is there, a space is inserted to the initiative of the result.
  • The optional * or integer number is used to define the minimum width field.
  • To define precision, an optional field that includes a. followed by a * or integers or nothing.
  • A length modifier that is optional and defines the size of an argument.
  • The conversion format specifier.

For more understanding look at the example of the printf() function in C given below:

#include <stdio.h>
int main()
{
    char chr = 'k';
    float num1 = 9.007, num2 = 0.9756;
    int int_num = 60;
    printf("num1 multiplied by num2= %f\n", num1*num2);
    printf("Setting width %*c \n", 8, chr);
    printf("Octal equivalent of %d is %o", int_num, int_num);
    return 0;
}

The above code defines variables for a character, floating-point numbers, and an integer. It then uses printf() function to display the multiplication of the floating-point numbers, set the width of the character, and show the octal equivalent of the integer.

The most common format specifiers with printf() function are:

  • %d or %i for printing integers
  • %f for printing floating-point numbers
  • %c for printing a single character
  • %s for printing a string

Conclusion

The printf() is used in C to write the formatted strings. It is defined inside the <stdio.h> header file. In the above guide, we described the syntax, arguments, and working of printf() function along with an example. This tutorial also discussed about the format specifier.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.