C++ 代码输出带换行的 'I'm a student'
C++ 代码输出带换行的 'I'm a student'
以下代码使用 C++ 标准输出流 std::cout 和换行符 std::endl 输出字符串 'I'm a student',并在其后添加换行:
#include <iostream>
int main() {
std::cout << 'I'm a student' << std::endl;
return 0;
}
输出:
I'm a student
解释:
#include <iostream>:包含 C++ 标准输入/输出流库,提供std::cout和std::endl。std::cout:标准输出流对象,用于将数据输出到控制台。<<:插入运算符,将数据插入到输出流中。'I'm a student':要输出的字符串。std::endl:换行符,用于在输出流中添加一个换行符。return 0;:表示程序正常结束。
运行结果:
该代码会将字符串 'I'm a student' 输出到控制台,并在其后添加一个换行符。
注意:
- 字符串必须用单引号
'括起来。 - 可以使用
\n作为换行符的替代方案,但std::endl更易读且方便。
拓展:
你可以根据需要修改字符串的内容和输出方式,例如添加多个换行符或使用其他控制字符。
原文地址: https://www.cveoy.top/t/topic/loCI 著作权归作者所有。请勿转载和采集!