C Printf() Function Tutorial with Examples – POFTUT

C Printf() Function Tutorial with Examples

C and C++ programming language a printf function that is used to print given values or data to the standard output or current terminal. printf() function supports different formatting types. In this tutorial, we will look at them in detail.

printf() Function Syntax

Syntax of printf is like below.


Print Given Text and Variables

printf function can be used simply just providing a single variable. In this example, we have an integer variable named age . We print this variable with %d which is type specifier of the given variable.

int age=20;

printf("My age is %d",age);

Print Multiple Values

We can use printffunction in order to print multiple values. We will add the variables to the end of the print() function. In this example, we will print variables named name , age and city.

char[] name="poftut";

int age=2;

char[] city = "ankara";

printf("Name:%s , Age:%d , City:%s",name, age, city);

Print String or Char Array Variable

We have already used string or character array types to print with printf . We will use %s in order to specify string or character array type variables in an output string.

char[] name="poftut";

printf("Name: %s",name);

Print Numbers or Integers

Integer variables can be printed with %d in print() function. In this example, we will print age integer variable.

int age=12;


Print Float Variable and Values

Float variables type generally holds floating values. These values can be print with %f in a print() function. We will print the price floating point type variable value in this example.

int price=1.99;


List Of Print Format Specifiers

Here we can find all printf supported format specifiers.

%ddecimal (integer) number (base 10)
%eexponential floating-point number
%ffloating-point number
%iinteger (base 10)
%ooctal number (base 8)
%sa string of characters
%uunsigned decimal (integer) number
%xnumber in hexadecimal (base 16)
%%print a percent sign
\%print a percent sign

How To Print Percent Sign %

As we have seen previous examples printf() function uses % as a format specifier. So there is a problem how can we print percent sign without breaking code? We can use \ to specify the percent sign is just a character in the print().

printf("\% is percent sign.");


printf("%% is percent sign.");

Print As Left Justified

We may need to beautify the printf() function output. The most basic beautification is aligning output. We can print given values left-justified with - and adding the space count.


Fill Zero

We can fill integer output before given integer value. We will put 0 between % and d. In this example, we will set 3 total numbers and provide 1 .

printf("%03d", 1);

This will output following.


Format Floating Point

Floating points have two part which is decimal part and other is floating part. We can format these two-part too. We will use . and numbers to specify numbers counts. In this example, we want 4 as decimal part but 3 for the floating-point part.

printf("'%4.3f'", 10.345642);

This will print only 3 number after the point.


Printf Special Characters

Printf has some special characters to make special behaviors. We can use these special characters to backspace, newline, tab, vertical tab, etc.

\aaudible alert
\fform feed
\nnewline, or linefeed
\rcarriage return
\vvertical tab

LEARN MORE  How To Find Specified Strings In Files With Find Command In Windows From Command Line With Examples?

1 thought on “C Printf() Function Tutorial with Examples”

Leave a Comment