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,
1 2 3 |
printf( "FORMATTED_TEXT_FORM", value1, value2, value3, ...); |
or in a more generic syntax,
1 2 3 |
int printf ( const char * format, ... ); |
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
1 2 3 4 5 6 7 |
int x = 100; printf ( "Integer Number = %d \n", x ); long int y = 100; printf ( "Double Number = %ld \n", y ); |
Displaying Floating Numbers with Printf()
We can use printf to format floating numbers with Printf Precision Specifiers. Precision Specifiers are explained well here.
1 2 3 4 5 6 7 8 9 |
float x = 273.15; printf ( "Float Number = %f \n", x ); printf ( "Float Number = %8.3f \n", x ); double y = 273.15; printf ( "Double Floating Number = %lf \n", y ); printf ( "Double Floating Number = %8.3lf \n", y ); |
How to display Chars (Bytes) with Printf()
1 2 3 4 |
char x = 65; printf ( "Charcter is : %c \n", x ); |
You can display the contents of Char Arrays (ASCII Strings) with Printf()
1 2 3 4 |
char x[] = "this is ascii string"; printf ( "Text is : %s \n", x ); |
Displaying Bits with Printf()
1 2 3 4 |
bool x = true; printf ( "Booleanr is : %d \n", x ); |
Hexadecimal Numbers are no problem to display with Prinf()
1 2 3 4 |
bool x = 65536; printf ( "Hexadecimal is : %h \n", x); |