C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn To Use The Multi-Talented Printf() Function In C++

The Printf function is one of the oldest and most powerful functions that we use to print out strings or that we use to print out to a stream or to another string. Do you want to learn the basics of printf() function? Who says you can’t use printf() in Modern C++? Who says printf() is oldish ? 🙂

Before we used cout cin in C++ (actually I don’t use them in my Modern Apps), we were using puts, gets, print, and scanf functions as input and output functions. While printf() is an old function that is used in C, still we can use this function in Modern C++. Interestingly, there are new printf() methods in Modern C++ as same as the old one that we used in modern strings like wide strings with wprintf() method or UnicodeString with Printf() method.

printf() function is one of the most powerful functions that comes with C and C++; it’s details allow us to format our strings well (especially in engineering outputs) and it is easy to use on integer or floating numbers or alphanumeric texts. Maybe one of the most powerful features of the C language is that detailed outputs of the C language come from this printf() function. It’s flexibility and detailed formatting features improve the readability of output data and we can easily compare these data and check-in screens.

As a result, we believe that it is necessary to provide some examples of this wonderful function, which is also required in Modern C++ in its new variations. You can use these functions to test by using the embarcadero c++ compiler.

The C++ Printf() function is answer to all sorts of questions

In C and C++ programming language, the printf() function, is a printing function with a formatted text form to stdout and variables are listed as other parameters of this function. printf functions writes the string pointed by format to the standard an output, generally it is stdout. It’s formatted text form is  formatted with format specifiers ( with %), and the other arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.

printf() function is defined in <stdio.h> library, and in std:: namespace. Simplified syntax of this function can be displayed as,

or in a more generic syntax,

printf Format Specifiers and scanf Format Specifiers are explained well in DocWiki of Embarcadero. All Format Specifiers together are listed and explained well here with examples.

Let’s give some basic examples of most used data types,

Use Printf() To display Integer numbers

Displaying Floating Numbers with Printf()

We can use printf to format floating numbers with Printf Precision Specifiers. Precision Specifiers are explained well here.

How to display Chars (Bytes) with Printf()

You can display the contents of Char Arrays (ASCII Strings) with Printf()

Displaying Bits with Printf()

Hexadecimal Numbers are no problem to display with Prinf()

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?

C++C++11Introduction to C++Learn C++Syntax

Full List Of Features In C++ 11

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

How To Use Basic Methods Of Strings In C++?