日志1
一、打印Hello World
(C语言)
#include<stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
(C++)
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
注释:
1.<iostream> 是C++ 标准库中的一个头文件。它提供了用于输入和输出操作的基本功能,比如可以使用 cout 进行标准输出(向控制台输出信息),使用 cin 进行标准输入(从控制台获取用户输入的信息)等。通过包含这个头文件,程序就能使用这些输入输出相关的功能了。
2.namespace(命名空间)是C++ 中用于组织代码、避免名称冲突的一种机制。
3.std 是C++ 标准库所使用的命名空间。当使用 using namespace std; 语句后,就意味着在当前代码文件中,可以直接使用 std 命名空间下的所有标识符(比如 cout、cin 等),而不需要在每次使用时都加上 std:: 前缀。
二、在C++ 中几种常见的换行方式:
1.\n(转义字符)
• 这是在C++ 字符串(例如,使用cout输出文本内容时)中表示换行的转义字符。
#include <iostream>
using namespace std;
int main()
{
cout << "第一行\n第二行";
return 0;
}
2.endl(操纵符)
#include <iostream>
using namespace std;
int main()
{
cout << "第一行" << endl << "第二行";
return 0;
}
注释:
1.当输出的内容含有换行符时特殊处理
例如:
HuHu喜欢踢球,请帮他打印小孩踢球,详见“输出”。 输入描述: 无 输出描述: O /H\ LL o
#include<stdio.h>
int main()
{
printf(" O\n/H\\\n LL o");
return 0;
}
2.要输出的字符串中含有双引号时特殊处理
例如:
秋凝爱发明,对于发明特别感兴趣,特别喜欢美国发明家爱迪生,所以找到一个爱迪生的名人名言想输出在屏幕上。爱迪生说过:A strong man will struggle with the storms of fate.
输入描述: 无 输出描述: Edison said: "A strong man will struggle with the storms of fate."
#include<iostream>
using namespace std;
int main()
{
cout<<"Edison said: \"A strong man will struggle with the storms of fate.\"";
return 0;
}
字符串常量"Edison said: \"A strong man will struggle with the storms of fate.\""是要输出的内容,注意这里为了在字符串中正确显示双引号,使用了\"这种转义字符的形式,这样就能在输出时完整地呈现出包含双引号的句子。
查看1道真题和解析