C++ printf is a formatting serve as this is used to print a string to stdout. The fundamental thought to name printf in C++ is to supply a string of characters that wish to be revealed as it’s in this system. The printf in C++ additionally accommodates a structure specifier this is changed via the real worth all over execution.
The C++ Printf Prototype
The C++ printf prototype is:
int printf(const char * structure, …);
The printf prototype is explained within the <cstdio> header report. While you use the printf() serve as, it prints the string identified via the structure to the usual output stdout. The structure too can include some specifiers that get started with a % and exchange values of variables with the printf() serve as. To position it merely, they paintings as further arguments to the printf() serve as.
The C++ Printf Parameters
The C++ prototype accommodates the next parameters:
- const char: Any textual content to be imprinted on the console as is
- structure: A pointer to the string with the non-compulsory structure specifier beginning with the %d signal.
Go back Worth of Printf in C++
C++ printf returns:
- The selection of characters written if a success
- A unfavorable worth, if failed
Our Unfastened Lessons with Certificates
Normal Type of Layout Specifier
The overall type of a C++ printf structure specifier is as underneath:
%[flags][width][.precision][length]specifier
Within the above structure:
- %: That is the main signal that denotes the start of the structure specifier
- Flags: An non-compulsory a number of values that modifies the conversion conduct. The to be had values are:
- -: The results of printf serve as is via default proper justified. This worth left justifies the effects
- +: This will likely upload an indication to the values of the end result, even for the sure values
- Area: If there’s no signal hooked up to a worth, this will likely connect an area to the start of the end result
- #: This results in another type of conversion being carried out
- 0: You employ this flag for integers and floating-point numbers to pad the values with zeros as a substitute of house
- Width: An non-compulsory box that accepts * or an integer worth to decide the minimal width box
- Precision: An non-compulsory argument specifies precision via accepting a *, integer worth, or not anything after a ‘.’ image
- Period: An non-compulsory parameter that defines the dimensions of the argument
- Specifier: This box accepts a conversion structure specifier
Layout Specifiers Utilized in C++ Printf
Printf in C++ can settle for the next structure specifiers.
Specifier |
Description |
% |
Prints the % signal |
c |
Writes a unmarried personality to stdout |
s |
Writes a string to stdout |
d or i |
Transforms a signed integer to a decimal illustration |
o |
Presentations an unsigned integer the usage of an octal illustration |
x or X |
Transforms an unsigned integer to a hexadecimal illustration |
u |
Presentations an unsigned integer the usage of a decimal illustration |
f or F |
Turns a floating-point quantity right into a decimal illustration |
e or E |
Transforms a floating-point quantity to a decimal exponent notation |
a or A |
Presentations a floating-point quantity the usage of a hexadecimal exponent |
g or G |
Presentations a floating-point quantity the usage of decimal or decimal exponent notation |
n |
Returns the selection of characters written via a long way the usage of this name serve as. It writes the end result to the worth pointed via the argument. |
p |
A pointer that issues to the implementation-defined personality collection |
Examples of C++ Printf() Serve as
Now that you recognize the basics of C++ printf, have a look at some examples that will provide you with a greater working out of ways the printf() serve as in C++ works.
Instance 1
On this instance, you are going to merely show the worth of an integer the usage of %d and write a string to stdout the usage of the %s structure specifiers.
#come with <cstdio>
int primary(){
int i = 9;
char title[] = “Simplilearn”;
printf(“i = %d n”, i);
printf(“Welcome to %s n”, title);
go back 0;
}
Output:
Instance 2
On this instance, you are going to use more than a few structure specifiers from the above desk to outline the conversion conduct and get the end result within the desired structure.
#come with <cstdio>
int primary(){
char c = ‘S’;
drift x = 7.0, y = 9.0;
double d = 6.548;
int i = 50;
printf(“The drift department is : %.3f / %.3f = %.3f n”, x,y,x/y);
printf(“The double worth is : %.4f n”, d);
printf(“Environment the width of c : %*c n”,3,c);
printf(“The octal similar of %d is %o n”,i,i);
printf(“The hex similar of %d is %x n”,i,i);
go back 0;
}
Output:
C++ Printf Vs. Sprintf
The Sprintf() serve as in C++ is sort of very similar to the printf() serve as with a unmarried primary distinction. Whilst the C++ printf writes to the usual output, the sprintf writes the output to a string buffer. The sprintf() serve as prototype is:
int sprintf(char * buffer, const char * structure, …);
Within the above prototype, the parameters are:
- Buffer: Pointer to a buffer the place the effects can be written
- Layout: Pointer to a null-terminated string written to the report flow
Instance of C++ Stringf() Serve as
Within the underneath instance, you are going to write a string to a buffer the usage of the sprintf serve as and output the formatted string. You’ll additionally show the whole selection of characters written to the buffer.
#come with <cstdio>
#come with <iostream>
the usage of namespace std;
int primary(){
char buf[100];
int return_Val;
char my_name[] = “Simplilearn”;
char subject[] = “tutorials”;
return_Val = sprintf(buf, “Welcome to %s you’ll be able to learn more than a few programming %s right here!!”, my_name, subject);
cout << buf << endl;
cout << “Overall characters written are: ” << return_Val << endl;
go back 0;
}
Output:
Printf Vs. Cout in C++
The C++ printf() serve as is most often utilized in C programming however too can run in C++. Then again, the cout is particularly an addition in C++. The desk underneath shows the numerous variations between the C++ printf and cout.
C++ Printf |
C++ Cout |
|
Maximum utilization |
C language |
C++ language |
Object of |
<cstdio> header report |
<iostream> header report |
Layout specifier |
Takes specifier |
It does now not take specifier |
Go back worth |
Returns the selection of characters if a success and a unfavorable worth if unsuccessful |
It does now not go back any worth |
Sort secure |
Now not sort secure in enter parameters |
Sort secure in enter parameters |
Instance of C++ Cout
Within the instance underneath, you are going to see using C++ printf and cout concurrently to peer the adaptation.
#come with <iostream>
the usage of namespace std;
int primary() {
char c = ‘S’;
cout<< “The worth of c : ” << c;
printf(“nThe worth of c : %c”, c);
go back 0;
}
Output:
Conclusion
You discovered the entirety concerning the C++ printf() serve as on this article. You’ve gotten additionally noticed other structure specifiers that you’ll be able to use to structure the effects and use them in examples. Now it is time to use the opposite specifiers too. If you wish to be told extra such basic ideas of C++ programming, you’ll be able to confer with Simplilearn’s C++ Educational for Freshmen. The academic is designed for novices and covers all of the fundamental ideas intimately.
If you wish to pursue a occupation in internet building, our Complete-Stack Internet Building Route would possibly fit you. The route provides hours of carried out studying and on-line studying fabrics that can assist you snatch more than one internet building programming languages that permit you to excel within the box.
Have any questions for us? Point out them as feedback, and our mavens gets again to you at the identical ASAP!
supply: www.simplilearn.com