Site icon Learn C++

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,

[crayon-6630ac6104255351103207/]

or in a more generic syntax,

[crayon-6630ac610425c181294837/]

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

[crayon-6630ac610425e033270752/]

Displaying Floating Numbers with Printf()

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

[crayon-6630ac6104260706012616/]

How to display Chars (Bytes) with Printf()

[crayon-6630ac6104262573372521/]

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

[crayon-6630ac6104266588134323/]

Displaying Bits with Printf()

[crayon-6630ac6104267976958756/]

Hexadecimal Numbers are no problem to display with Prinf()

[crayon-6630ac6104269150229771/]

Exit mobile version