The printf(), or “print formatted”, function can print a string to the console, including variables within the string.


printf(string, value1, value2 ... valueN)

The printf() function takes a string with variable placeholders, also known as format specifiers, followed by any number of values to format in the string.


#include <stdio.h>
int main(void) {
printf("%d is a number\n", 5);
printf("%d is after %d\n", 2, 1);
return 0;

The output would be:

5 is a number
2 is after 1

Format Specifiers

As seen above, the string given contains a % character followed by a letter to indicate what type of value will be formatted there. Some common ones are shown below:

Format Specifier Type
%c char
%d double
%i integer
%p pointer
%s string

Decimal Precision

When using format specifiers for decimal numbers, the number of decimal places to be printed can be specified.

For example, to print two digits after the decimal place with printf(), 0.2f can be used:

#include <stdio.h>
int main() {
float pi = 3.14159;
// Print 2 digits after the decimal place
printf("%0.2f", pi);
return 0;

The output would be:



Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C on Codecademy