Home
avatar

静静

01x基础语法1-7

Hello World

cout 输出

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World";
    return 0;
}

注释

#include <iostream>
using namespace std;
/*
* 多行
* 注释
* 都不执行
*/
int main() {
    cout << "Hello World";
    // 单行注释
    return 0;
}

image-20250107224424545

变量

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

image-20250108183441508

常量

#define PI 3.14159 // 定义常量

int main() {
	const int a = 10; // 定义常量
	PI = 3.14; // 错误,常量不可修改
	cout << "Hello World \n"; // 输出 Hello World,并换行
	cout << "Hello World \n"; // 输出 Hello World,并换行
	cout << "a = " << a << endl; // 输出 a = 10,并换行
	cout << "PI = " << PI << endl; // 输出 PI = 3.14159,并换行
    // 单行注释
    return 0;
}

image-20250108183941086

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

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

image-20250108191410041

image-20250108191328189

标识符命名规则

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

关键字

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

C++