Featured image of post 01x基础语法1-7

01x基础语法1-7

Hello World

cout 输出

1#include <iostream>
2using namespace std;
3
4int main() {
5    cout << "Hello World";
6    return 0;
7}

注释

 1#include <iostream>
 2using namespace std;
 3/*
 4* 多行
 5* 注释
 6* 都不执行
 7*/
 8int main() {
 9    cout << "Hello World";
10    // 单行注释
11    return 0;
12}

image-20250107224424545

变量

 1#include <iostream>
 2using namespace std;
 3/*
 4* 多行
 5* 注释
 6* 都不执行
 7*/
 8int main() {
 9    int a = 10;
10    char16_t b = u'C'; // 修复了变量声明和初始化的问题
11	cout << "Hello World \n"; // 输出 Hello World,并换行
12	cout << "a = " << a << endl; // 输出 a = 10,并换行
13	cout << "b is " << b << endl; // 输出 b is C,并换行
14    // 单行注释
15    return 0;
16}

image-20250108183441508

常量

 1#define PI 3.14159 // 定义常量
 2
 3int main() {
 4	const int a = 10; // 定义常量
 5	PI = 3.14; // 错误,常量不可修改
 6	cout << "Hello World \n"; // 输出 Hello World,并换行
 7	cout << "Hello World \n"; // 输出 Hello World,并换行
 8	cout << "a = " << a << endl; // 输出 a = 10,并换行
 9	cout << "PI = " << PI << endl; // 输出 PI = 3.14159,并换行
10    // 单行注释
11    return 0;
12}

image-20250108183941086

问题:为什么char类型会变得很奇怪?

回答:一个字符变量想写多个字符的方法

image-20250108191410041

image-20250108191328189

标识符命名规则

  • 不能是关键字
  • 由字母、数字、下划线组成
  • 区分大小写
  • 只能是字母或下划线开头

关键字

完整关键字列表:https://www.runoob.com/w3cnote/cpp-keyword-intro.html

Licensed under CC BY-NC-SA 4.0