代码块加载不出来。重新刷新一下就出来了

基础入门

第一个程序

需要在文件开头加上 using namespace std;,否则编译器找不到 cout 和 endl。

#include <iostream>

using namespace std;
int main() {

    cout << "Hello, world!" << endl;

    //std::cout << "Hello, world!" << std::endl;
    return 0;
}

第二种学法

这是标准写法,直接使用 std 命名空间下的 cout 和 endl,不需要 using namespace std;,更推荐这种写法,避免命名冲突。

#include <iostream>
int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

变量

变量存在的意义:方便我们管理内存空间

语法数据类型 变量名 = 初始值;

#include <iostream>

using namespace std;
int main() {

    //变量的定义
    //语法:数据类型  变量名;
    int a = 10;
    cout << "a = " << a << endl;

    //std::cout << "Hello, world!" << std::endl;
    return 0;
}

变量其实只不过是程序可操作的存储器名称

在C++中,有很多种变量类型可用于不同种类的数据

C++中每个变量都有指定的类型,类型决定了变量存储的大小和布局,该范围内地值都可以存储在内存中,运算符可应用于变量上

大写字母和小写字母是不同的,因为C++大小写敏感的。

bool 类型案例

#include <iostream>

using namespace std;

int main(){
    
    bool isGreater;
    int a = 5,b =3;
    isGreater = (a>b); //比较a和b,true
    //关键词在前面,变量在后面,比较结果为true或false
    
    cout << "a > b 的结果是:"<< boolalpha << isGreater << endl;//先后顺序不能颠倒,否则会输出a和b的比较结果为0或1,启用boolalpha后才为true或false

    //bool类型输出为1或0,boolalpha未启用时默认为1或0,启用后为true或false
    
    return 0;
}

输出:a > b 的结果是:true

char 类型案例

#include <iostream>

using namespace std;

int main(){
    char ch1 = 'A';//存储ASCII字符
    char ch2 = 97;//存储ASCII字符对应的整数(97是a字符的ASCII码值)
    cout<<"ch1 = "<<ch1<<endl;
    cout<<"ch2 = "<<ch2<<endl;
    cout<<"ch1的ASCII码值为:"<<int(ch1)<<endl;

    return 0;
}

输出

ch1的值:A
ch2对应的字符:a
ch1的ASCII码:65

int 类型案例

#include <iostream>
using namespace std;

int main() {
    int age = 25;
    int num1 = 100, num2 = 50;
    int sum = num1 + num2;
    cout << "年龄是:" << age << endl;
    cout << num1 << " + " << num2 << " = " << sum << endl;
    return 0;
}

输出

年龄是:25
100 + 50 = 150

float 类型案例

#include <iostream>

using namespace std;

int main(){
    float pi = 3.14159f;//注意加f表示float(double)
    float a = 5.0f;
    float b = pi * a * a;//计算圆面积
    cout <<"半径为" << a << "的圆面积为" << b << endl;
    return 0;
}

输出:半径为5的圆面积是:78.5397

double 类型案例

#include <iostream>

using namespace std;

int main(){
    double price = 99.99;//双精度存储,精度比flaot高
    double quantity = 3.5;
    double total = price * quantity;
    cout << "总价是:" << total << endl;
    return 0;
}

输出:总价是:349.965

Void 类型案例

#include <iostream>

using namespace std;

void printHeloo(){
    cout << "Hello, World!" << endl;
}
int main(){
    printHeloo();
    return 0;
}

wchar_t 类型案例**

#include <iostream>
#include <wchar.h> // 用于宽字符处理
using namespace std;

int main() {
    wchar_t wch = L'中'; // L表示宽字符常量
    wcout.imbue(locale("")); // 配置本地化,让宽字符正常输出
    wcout << L"宽字符内容:" << wch << endl;
    return 0;
}

变量作用域

一般来说有三个地方可以定义变量:

  • 在函数或一个代码块内部声明的变量,称为局部变量

  • 在函数参数的定义中声明的变量,称为形式参数

  • 在所有函数外部声明的变量,称为全局变量

作用域是程序的一个区域,变量的作用域可以分为以下几种:

  • 局部作用域:在函数内部声明的变量具有局部作用域,它们只能在函数内部访问。局部变量在函数每次被调用时被创建,在函数执行完后被销毁。

  • 全局作用域:在所有函数和代码块之外声明的变量具有全局作用域,它们可以被程序中的任何函数访问。全局变量在程序开始时被创建,在程序结束时被销毁。

  • 块作用域:在代码块内部声明的变量具有块作用域,它们只能在代码块内部访问。块作用域变量在代码块每次被执行时被创建,在代码块执行完后被销毁。

  • 类作用域:在类内部声明的变量具有类作用域,它们可以被类的所有成员函数访问。类作用域变量的生命周期与类的生命周期相同。

局部变量

#include <iostream>
using namespace std;
 
int main ()
{
  // 局部变量声明
  int a, b;
  int c;
 
  // 实际初始化
  a = 10;
  b = 20;
  c = a + b;
 
  cout << c;
 
  return 0;
}

全局变量

在所有函数外部定义的变量(通常是在程序的头部),称为全局变量。全局变量的值在程序的整个生命周期内都是有效的。

全局变量可以被任何函数访问。也就是说,全局变量一旦声明,在整个程序中都是可用的。下面的实例使用了全局变量和局部变量:

#include <iostream>
using namespace std;
 
// 全局变量声明
int g;
 
int main ()
{
  // 局部变量声明
  int a, b;
 
  // 实际初始化
  a = 10;
  b = 20;
  g = a + b;
 
  cout << g;
 
  return 0;
}

类作用域

#include <iostream>

class Myclass{
    public:
    static int class_var;

};

int Myclass::class_var = 20;

int main(){
    std::cout << Myclass::class_var << std::endl;
    return 0;
    
}

常量

作用:用于记录程序中不可更改的数据

C++定义常量两种方式

  1. #define 宏常量: #define 常量名 常量值

    1. 通常在文件上方定义,表示一个常量

  2. const修饰的变量 const 数据类型 常量名 = 常量值

    1. 通常在变量定义前加关键字const,修饰该变量为常量,不可修改

#include <iostream>

using namespace std;

//常量的定义方式
//1、#define 宏常量
//2、const 修饰的常量

#define day 7
int main() {

    cout << "一周有" << day << "天" << endl;

    //2、const修饰的常量
    const int month = 12;
    cout << "一年总共有" << month << "个月份" << endl;

    return 0;
}

浮点常量

3.14159       // 合法的 
314159E-5L    // 合法的 
510E          // 非法的:不完整的指数
210f          // 非法的:没有小数或指数
.e55          // 非法的:缺少整数或分数

布尔常量

布尔常量共有两个,它们都是标准的 C++ 关键字:
true 值代表真。
false 值代表假。

字符常量

关键字

asm

do

if

return

typedef

auto

double

inline

short

typeid

bool

dynamic_cast

int

signed

typename

break

else

long

sizeof

union

case

enum

mutable

static

unsigned

catch

explicit

namespace

static_cast

using

char

export

new

struct

virtual

class

extern

operator

switch

void

const

FALSE

private

template

volatile

const_cast

float

protected

this

wchar_t

continue

for

public

throw

while

default

friend

register

TRUE

delete

goto

reinterpret_cast

try

const关键字

使用 const 前缀声明指定类型的常量,如下所示:

const type variable = value;

#include <iostream>
using namespace std;

int main(){
    const int a = 10;
    const int b = 20;
    const char c = '\n';
    int d;

    int d = a * b;
    cout << d;
    cout << c;
    return 0;
}

define预处理器

下面是使用 #define 预处理器定义常量的形式

#include <iostream>
using namespace std;

#define a 10
#define b 20
#define c '\n'

int main(){
    int d;
    d = a + b;
    cout << d;
    cout << c;
    return 0;
}

标识符命名规则

作用:C++规定给标识符(变量、常量)命名时,有一套自己的规则

  • 标识符不能是关键字

  • 标识符只能由字母、数字、下划线组成

  • 第一个字符必须为字母或下划线

  • 标识符中字母区分大小写

建议:给标识符命名时,争取做到见名知意的效果,方便自己和他人的阅读

#include <iostream>

using namespace std;

int main() {

    //1、标识符不可以是关键字
    //2、标识符是由数字、下划线、字母构成
    int _abc = 10;
    int _123abc = 20;

    //3、标识符第一个字符只能是字母或下划线
    //int 123abc = 30; //错误,不能以数字开头

    //4、标识符区分大小写
    int aaa= 10;
    

    return 0;
}

三字字符

注意!:

C++17标准中已经移除了三字符组支持,因此现代编译器可能不支持这种写法

三字符组就是用于表示另一个字符的三个字符序列,又称为三字符序列。三字符序列总是以两个问号开头。

三字符序列不太常见,但 C++ 标准允许把某些字符指定为三字符序列。以前为了表示键盘上没有的字符,这是必不可少的一种方法。

三字符序列可以出现在任何地方,包括字符串、字符序列、注释和预处理指令。

案例 1:三字符组替换成符号

#include <iostream>
using namespace std;

int main() {
    // 三字符组 ??! 会被替换成 |(逻辑或)
    int a = 1, b = 0;
    if (a ??! b) {  // 等价于 if (a | b)
        cout << "a | b 为真" << endl;
    }

    // 三字符组 ??< 和 ??> 替换成 { 和 }
    int arr[3] ??< 1, 2, 3 ??>;  // 等价于 int arr[3] {1,2,3};
    cout << "数组元素:";
    for (int i = 0; i < 3; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    // 三字符组 ??) 替换成 ]
    cout << "数组第2个元素:" << arr ??(1??) << endl;  // 等价于 arr[1]

    return 0;
}

案例 2:预处理指令中的三字符组

// 三字符组 ???= 替换成 #(预处理指令开头)
???=include <iostream>  // 等价于 #include <iostream>

using namespace std;

int main() {
    cout << "预处理指令也能识别三字符组" << endl;
    return 0;
}

大部分现代编译器(如 GCC、Clang)默认启用三字符组替换,直接编译上述代码即可得到预期结果

g++ trigraph.cpp -o trigraph && ./trigraph

输出结果:

a | b 为真
数组元素:1 2 3 
数组第2个元素:2
预处理指令也能识别三字符组