Input and Output
Input and Output
Data input
Function: Used to get data from the keyboard
Keywords: cin
How to Write Your First Program Using the 8051 Microcontroller
This is a tutorial on using the 8051 microcontroller for programming. Here, I will guide you through how to write your first program. In the tutorial, I will explain how to set up the environment and how to run programs on the 8051 microcontroller.
8051 Microcontroller Tutorial
- 8051 Microcontroller Introduction
- 8051 Microcontroller Environment Setup
- How to Program the 8051 Microcontroller
References
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;
}
运行结果(以下为按提示依次输入 10、20、30……时的一次示例):
请输入整型变量:
10
请输入浮点型变量:
20
请输入字符型变量:
3
请输入字符串型变量:
0
请输入布尔型变量:
1
C-style I/O and C++ stream I/O
scanfandprintfin C'sstdio.h
The int scanf(const char *format, ...) function reads input from the standard input stream stdin and scans the input according to the provided format.
The int printf(const char *format, ...) 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("你好") //输出“你好”字符串
运行结果: 此代码块不含 main 函数,是语法、接口或分文件示例,不能独立运行,因此没有终端输出。
scanf("%d",&a); //输入一个整形数
scanf("%d %d",&a,&b); //输入两个整形数,中间以空格隔开
scanf("%d,%d",&a,&b); //输入两个整形数,中间以逗号隔开
运行结果: 此代码块不含 main 函数,是语法、接口或分文件示例,不能独立运行,因此没有终端输出。
std::cinandstd::coutin C++'s iostream
cout is used in conjunction with the stream insertion operator <<.
std::cout << a; //输出一个变量a
std::cout << "你好" //输出"你好"
std::cout << "结果是:" << a << std::endl //输出 结果是: a 并换行
运行结果: 此代码块不含 main 函数,是语法、接口或分文件示例,不能独立运行,因此没有终端输出。
cin is used with the stream extraction operator >>
std::cin >> a //输入一个变量a
运行结果: 此代码块不含 main 函数,是语法、接口或分文件示例,不能独立运行,因此没有终端输出。
Modern C++ Formatted Output
The previously introduced printf and std::cout are both very commonly used, but each has its drawbacks: printf is less type-safe when its format placeholders are written incorrectly, while std::cout can be verbose when outputting multiple variables consecutively.
Modern C++ provides more suitable tools for formatted output:
std::format(C++20): generates formattedstd::string, with concise syntax and type safety.std::print/std::println(C++23): Directly formatted output,printlnautomatically wraps.
For example, when outputting a name and score, std::cout is often written as std::cout << name << ": " << score << std::endl;, and modern writing can be written as std::println("{}: {}", name, score);.
std::print / std::println by default outputs to standard output, but the first parameter can also be set to stdout or stderr. Here, stdout / stderr refer to the C standard library's FILE*, not std::cout.
The later section on modern C++ will cover in detail: std::format / std::print.
