C – Printf Tutorial with Examples


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


Syntax of printf is like below.


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

Print Multiple Values

We can use printffunction inorder to print multiple values. We wll add the variables to the end of the printf function. In this example we will print variables named name , age and city.

Print String

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 a output string.

Print Numbers or Integers

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

Print Floats

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

LEARN MORE  CSharp Types and Objects How They Match?

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 seen previous examples printf  function uses % as format specifier. So there is a problem how can we print percent sign without breaking code? We can use \ to specify percent sign is just a character in the printf.


Print 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  between % and d . In this example we will set 3 total numbers and provide 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 floating point part.

LEARN MORE  Powershell Trim Operation Like Filename, String, End, Start, Spaces

This will print only 3 number after the point.

Printf Special Characters

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

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

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)