Skip to content

Wiki

Input and Output

About 5 min read

Existing English translations were reused. 1 current source blocks remain in Simplified Chinese, without triggering paid translation.

1.输入输出

1.1.Data input

Purpose: Used to obtain data from the keyboard

**Keyword:** cin

Syntax: cin >> 变量

Example:

int main(){

	//整型输入
	int a = 0;
	cout << "请输入整型变量:" << endl;
	cin >> a;
	cout << a << endl;

	//浮点型输入
	double d = 0;
	cout << "请输入浮点型变量:" << endl;
	cin >> d;
	cout << d << endl;

	//字符型输入
	char ch = 0;
	cout << "请输入字符型变量:" << endl;
	cin >> ch;
	cout << ch << endl;

	//字符串型输入
	string str;
	cout << "请输入字符串型变量:" << endl;
	cin >> str;
	cout << str << endl;

	//布尔类型输入
	bool flag = true;
	cout << "请输入布尔型变量:" << endl;
	cin >> flag;
	cout << flag << endl;
	return EXIT_SUCCESS;
}

Run result (the following is an example when entering 10 , 20 , 30... in sequence as prompted):

请输入整型变量:
10
请输入浮点型变量:
20
请输入字符型变量:
3
请输入字符串型变量:
0
请输入布尔型变量:
1

2.C-style I/O and C++ streaming I/O

1. scanf and printf in the stdio.h of the C language

int scanf(const char \*format, ...) The function reads input from the standard input stream stdin and scans the input according to the provided format.

int printf(const char \*format, ...) The function writes output to the standard output stream **stdout and produces output according to the provided format.

printf("输出内容(可含占位符)",变量1,变量2)

printf("%d",a);      //输出一个整形变量a
printf("%f %f",a,b);   //输出二个单精度浮点数变量(fp32) a,b中间以空格隔开
printf("%d序号对应的值是%lf",a,b);   //输出二个单精度浮点数变量a,b中间以空格隔开
printf("%.2f",a);   //输出一个单精度浮点数(fp32) a,并保留两位小数
printf("你好")  //输出“你好”字符串

Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.

scanf("%d",&a);           //输入一个整形数
scanf("%d %d",&a,&b);   //输入两个整形数,中间以空格隔开      
scanf("%d,%d",&a,&b);   //输入两个整形数,中间以逗号隔开

Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.

2. std::cin and std::cout in iostream of C++

cout is used in conjunction with the stream insertion operator <<

std::cout << a;   //输出一个变量a
std::cout << "你好"   //输出"你好"
std::cout << "结果是:" << a << std::endl  //输出 结果是: a  并换行

Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.

cin is used in combination with the stream extraction operator >>

std::cin >> a     //输入一个变量a

Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.

3.Formatting output of modern C++

The previously introduced printf and std::cout are both very commonly used, but each has its drawbacks: printf's format placeholders are not type-safe enough when written incorrectly, and std::cout is also somewhat verbose when continuously outputting multiple variables.

Modern C++ provides tools better suited to formatted output:

  • std::format(C++20): Generate formatted std::string, concise syntax, type-safe.
  • std::print / std::println(C++23): Direct formatted output, println automatically wraps lines.

For example, output a name and score,std::cout often written as std::cout << name << ": " << score << std::endl;; the modern notation can be written as std::println("{}: {}", name, score);.

std::print / std::println By default, output goes to standard output, but you can also write the first argument as stdout or stderr. Here stdout / stderr refers to the C standard library's FILE*, not std::cout.

The later modern C++ chapters will specifically discuss:std::format / std::print.