|
|
Tutorials

Cpp

|
Topic |
How is "printf()" used in C++?
|
|
Explanation |
|
printf() is an I/O function that prints the formatted data to the stdout. This function returns the total number of
characters, but on failure a negative value is returned.
Syntax:
int printf ( const char * format, ... );
The following table lists the type specifier used with printf()
| Type |
Description |
| c |
Character |
| d or i |
Signed decimal integer |
| e |
Scientific notation using e character |
| E |
Scientific notation using E character |
| f |
Decimal floating point |
| g |
Use the shorter of %e or %f |
| G |
Use the shorter of %E or %f |
| o |
Signed octal |
| s |
String of characters |
| u |
Unsigned decimal integer |
| x |
Unsigned hexadecimal integer |
| X |
Unsigned hexadecimal integer(capital) |
| p |
Pointer address |
| % |
A % followed by another % character will write % to the stream. |
Example:
#include <stdio.h>
int main()
{
printf ("Characters: %c %c \n", 'b',42);
printf ("Decimals: %d %ld\n", 187, 345);
printf ("Preceding blanks: %7d \n", 2010);
printf ("Preceding zeros: %07d \n", 2010);
printf ("Float values : %3.2f %+.0e %E \n",
4.316, 4.2456, 6.1317);
printf ("%s \n", "Hscripts");
return 0;
}
|
Result:setbufeg.txt
Characters: b
Decimals: 1
Preceding blanks: 2010
Preceding zeros: 0002010
Float values : 4.32 +4e+00 6.131700E+00
Hscripts
In the above example the "printf()" is used with different type specifiers.
|
| Note |
C++ is one of the most used programming languages in the world. Also known as "C with Classes".
Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.
|
|
|
|