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
C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?