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-67635882dae30151130870/]

or in a more generic syntax,

[crayon-67635882dae3b113029026/]

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-67635882dae3e290020556/]

Displaying Floating Numbers with Printf()

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

[crayon-67635882dae41696842943/]

How to display Chars (Bytes) with Printf()

[crayon-67635882dae42662528822/]

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

[crayon-67635882dae46457874114/]

Displaying Bits with Printf()

[crayon-67635882dae48088797851/]

Hexadecimal Numbers are no problem to display with Prinf()

[crayon-67635882dae49293071952/]

Exit mobile version