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 function 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 basics of printf() function? Who says you can’t use printf() in Modern C++? Who says printf() is oldish ? 🙂

Before we use cout cin in C++ (actually I don’t use them in my Modern Apps), we were using puts, gets, printf and scanf functions as an input and output functions. While printf() is an old function which 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 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 function comes with C and C++, it’s details allows us to format our strings well (especially in engineering outputs) and it is easy to use on integer or floating numbers or alphanumeric texts. May be one of the most powerful feature of C language is about detailed outputs of C language comes from this printf() function. It’s flexibility and detailed formatting features improves readability of output data and we can easily compare these data and check in screens.

Thus, we think that we must give some examples to this great function, which is also needed in Modern C++ in it’s new variations.

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 to 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()

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++ComponentsDatabaseIntroduction to C++Language FeatureLearn C++

Get To Know The Powerful C++ Data Bindings In Windows Apps

C++Introduction to C++Language FeatureLearn C++

How to Create a New Windows FMX Static Library In C++

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

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

en_USEnglish