Location>code7788 >text

⒉ Output

Popularity:740 ℃/2025-04-05 20:55:40

After talking about the framework, let’s talk about the output.

There are many kinds of outputs in C++. I will introduce it one by one.

coutStatement

This is the most commonly used output statement in C++ language, and the syntax is:

cout<<a<<b<c<<d;
 //"<<" means caret, a, b, c, d means what to output

For example, we outputHello world!You can write it ascout<<"Hello world!"

Notice!cout<<"Hello world!"I used double quotes here", that's because double quotes will output the included content without any changes. If you output this:cout<<"1+1", the result will be1+1; and if you output this:cout<<1+1, the result will be\(1+1\)Value of\(2\)

Let's practice a question.

Output100+1000=and the value it equals.

#include<iostream>
 using namespace std;
 int main(){
	 cout<<"100+1000="<<100+1000;//In the front, output "100+1000=" directly, and afterwards, output the value of 100+1000 of 1100.
	 return 0;
 }

We can also write this:

#include<iostream>
 using namespace std;
 int main(){
	 The result of cout<<100/*100 is 100*/<<'+'/*Single quotes can only be stuffed into a single character*/<<1000<<'='<<100+1000;//There is above the back, and no explanation will be repeated.
	 return 0;
 }

in addition,coutNo line wrapping will be automatically. for example:

cout<<1;
cout<<2;

Will output

12

need

cout<<1<<endl;
cout<<2;

or

cout<<"1\n";
cout<<2;

Only output

1
2

printfStatement

printfStatement C++ is not commonly used and is mainly used in C language. Learning this is mainlycoutThe statement is slower than it, and we use it when the data is large.

printfRequires librarycstdioor, and it does not require a namespace.

Use it to output100+1000=And the value it equals, you can write it like this:

#include<>
int main(){
	printf("100+1000=%d",100+1000);
	return 0;
}

Use it to output newline characters\n

printf("\n");

putcharStatement

putcharIt is used to output a single character and requires a librarycstdioor, and it does not require a namespace.
For example, we canputchar('1'),putchar('\n'),putchar('A'), output

1
A
#include<>
int main(){
	putchar('1'),putchar('\n'),putchar('A');
	return 0;
}

putsStatement

putsIt is used to output strings and requires a librarycstdioor, need to use itcstringorlibrary, and it does not require a namespace.Notice!putsA line break is included at the end of the statement!
usputs("Hello world!")Equivalent tocout<<"Hello world\n", all outputs

Hello world!

#include<>
#include<>
int main(){
	puts("Hello world!");
	return 0;
}