How to Use printf() function in C Programming Language

printf() function in C Programming Language

Introduction :

The printf() is one of the most commonly used functions in C programming language. If you want to learn the C programming language, you have to know the syntax, usage and formatting options of printf() function. This function generally displays a simple text message on the console screen. You can also display formatted output using this function.

Using printf() function, you can display different types of data such as integer number, floating point number, character, string, pointers etc. In this article, I shall show you how to use printf() function using the C programming language. Here, I also show you the syntax, usage and various formatting options of printf() function.

What is printf() function :

The printf() function is part of the standard input-output library (stdio.h) in C programming language. This library or header file contains the declaration for the printf() function. The printf() function is a very important function in the C programming language. It is used to print formatted text-based output on the console screen. It allows you to format the output in various ways to create clear and organized output on the screen.

Using printf() function, you can display static text with the value of variables, constants and expressions on the console screen. The name printf means “print formatted”. It allows you to display various types of data such as strings, numbers (integers and floating-point), characters and other variables in a specific format. Before use printf() function, you have to include the “stdio.h” library or header file at the beginning of your program.

Syntax of the printf() function :

In C programming language the following code is the syntax of the printf() function.

The printf() function returns an integer value. The return value represents the number of characters printed on the console. If returns a negative value, you can see an error. The first parameter or argument of printf() function is “formatted string”. It defines the layout and formatting of the output on the console screen. This is a string that may contain ordinary characters with or without format specifier.

After “formatted string” parameter, you can pass multiple parameter or arguments in the printf() function such as para1, para2, para3 and so on. These parameters or arguments will replace the format specifiers in the formatted string. They may be variable, constant or expression. These parameters must be matched with the number and type of the format specifiers within the formatted string.

Format specifier in C programming language :

Within the format string of printf() function, you can use the format specifier to indicate where and how the values of arguments will be inserted. Without format specifier, the ordinary characters are printed on the output screen directly. Otherwise, the values of the parameter or argument will be replaced in the “formatted string” before print on the screen.

The format specifier starts with a percent sign (%) followed a conversion specifier. You can also use other sub specifier such as flag, field width, precision, length modifiers, etc. The following code is the syntax of format specifier in C programming language.

Specifier in C programming language :

The specifier is a conversion character that define the type of data is inserted into the format string. There are different types of the specifiers you can use in the printf() function. If you want to display value of various variables, you can use following specifiers in the printf() function.

  • %d —integer number in decimal format like — printf(“%d”,int_variable);
  • %i —integer number in decimal format like — printf(“%i”,int_variable);
  • %u —unsigned integers in decimal format like — printf(“%u”,int_variable);
  • %x —hexadecimal number in hexadecimal format in lowercase like — printf(“%x”,int_variable);
  • %X —hexadecimal number in hexadecimal format in uppercase like — printf(“%X”,int_variable);
  • %o —octal number in octal format like — printf(“%o”,octal_number);
  • %f —float number in floating format like — printf(“%f”,float_number);
  • %c —character in char format like — printf(“%c”,character);
  • %s —strings in strings format like — printf(“%s”,string);
  • %p —pointer in pointer format like — printf(“%p”,ptr);

Width in C programming language :

Width is a sub-specifier that specifies the minimum number of characters will be printed on the console screen. If width is greater than the number of characters, the white space will be inserted before the characters otherwise the characters display as usual. You can use the code of width sub-specifier like printf(“%20d”,int_number);

Precision in C programming language :

In printf() function, precision specifies the number of digits to be printed for floating-point numbers. It is also used to print the maximum number of characters for strings. It is denoted by a period (.) sign followed by a number and also followed by an integer argument. You can use precision like printf(“%.1f”, float_number);

Escape sequences in C programming language :

Escape sequences are very important for formatting output and controlling the appearance of text in C programming language. It is used in the format string of printf() function to insert special characters such as newline, tab, backslash and more into the output. It is generally preceded by a backslash () character. You can see following escape sequences in printf() function.

  • \n —— create newline using \n escape sequences like —- printf(“Puskar\nJasu”);
  • \t —- create horizontal tab using \t escape sequences like —- printf(“Puskar\tJasu”);
  • \ —— print single backslash using \ escape sequences like —- printf(“Puskar\Jasu”);
  • \’ —— print single quote using \’ escape sequences like —- printf(“This is Puskar\’website”);
  • \” —— print double quote using \” escape sequences like —- printf(“This is Puskar\”website”);
  • \a —— produces a beep sound using \a escape sequences like —- printf(“\a”);
  • \b ——delete left character or space of string like —- printf(“Puskar\bJasu”);
  • \r —— move the cursor to the beginning of the current line like— printf(“Puskar\rJasu”);

Use of printf() function :

You can display simple text directly using printf() function on console screen. Using printf() function, you can also display the values of variables with the help of format specifiers. It allows you to display formatted output using the formatting options such as width, precision, escape sequences and more.

Run the program :

In the below program, I use all types of format specifiers and formatting options of printf() function. If you want to run the program on your pc, you have to install VS Code. You can use any other C complier like Turbo C++ IDE. Then open VS Code and create new file give name as “pac.c”. After that copy the below code and paste in the “pac.c” file. Now save the file and run the program on your pc.

Source code of the program :

The following code is used for printf() function in C programming language.

Output :

When you run the program on your pc, you can see the output of the program like below image.

Output of printf() function in C Programming Language

Conclusion :

At last, you have learned how to use printf() function in the C programming language. Now you can use printf() function to display any type of formatted output with format specifiers for integers, floating-point numbers, characters, strings and pointer variables. Thank you for visiting my site.

Scroll to Top