C printf Function
C printf Function
The C printf function is a standard library function. The Standard Input/Output Library contains functions for formatted data transfer in and out of the C program. The two such functions are as follows:
- printf (print formatted) function
- scanf (scan formatted) function
The data printing and reading functions are defined as standard functions which are part of
the language specification but are not part of the C Language itself. The functions are defined in the system header file stdio.h
C printf Function
The printf function formats and displays the arguments on the standard output. By default, it prints the output to the terminal(the standard output device)
The printf() function prints output to the standard output stream i.e stdout, according to format and other arguments passed to printf().
Syntax
The general syntax of the printf function is as below:
printf(format, arg1,arg2,arg3,…);
The string format consists of two types of items:
- Characters that will be printed to the screen
- conversion specifiers that define how the arguments are displayed.
Examples
Print String
In this example, we will print the string literal to the output screen using the print() function. A string literal is a combination of characters like letters, digits, and other special characters enclosed in double quotation marks. For example, “My name is, John!”
The print() function call to print the string literal “Hello, World!” is shown below:
printf(“Hello, World! \n”);
Note that the printf function will not print the new line. We can achieve this with \n character sequence. The \n is the new line character. This sequence advances the terminal to the left margin of the next line.
Print Number
In this example, we will print the number along with string information. The argument number is an integer variable. The format specification %d is for printing in decimal notation.
For example:
printf(“The total sum is %d”,number);