3.1 什么是变量

学如逆水行舟,不进则退

心似平原跑马,易放难收

3.1 什么是变量

3.1.1 内存和寻址概述

所有计算机、智能手机及其他可编程设备都包含微处理器和一定数量的临时存储空间,这种临时存储器被称为随机存取存储器( RAM)。

可以将 RAM 当作宿舍里成排存物柜的存储区域,每个存物柜都有编号,即地址。

3.1.2 声明变量以访问和使用内存

使用 C++等语言编程时,您只需定义用于存储这些值的变量。定义变量非常简单,其语法如下:

VariableType VariableName;VariableType VariableName = InitialValue;

变量类型向编译器指出了变量可存储的数据的性质,编译器将为变量预留必要的空间。

变量名由程序员选择,它替代了变量值在内存中的存储地址,但更友好。

示例,使用变量存储数字及其相乘的结果 代码如下:

#include <iostream>
using namespace std;

int main()
{
    cout << "This program will help you multiply two numbers" << endl;

    cout << "Enter the first number: ";
    int firstNumber = 0;
    cin >> firstNumber;

    cout << "Enter the second number: ";
    int secondNumber = 0;
    cin >> secondNumber;

    // Multiply two numbers, store result in a variable
    int multiplicationResult = firstNumber * secondNumber;

    // Display result
    cout << firstNumber << " x " << secondNumber;
    cout << " = " << multiplicationResult << endl;

    return 0;
}

代码运行结果如下

在这里插入图片描述

第 9 和 13 行声明了变量 firstNumber 和 secondNumber,用于临时存储用户输入的整数。第 10 和 14 行使用 std::cin 获取用户输入,并将其存储到两个整型变量中。

在 C++中,变量名可包含数字和字母,但不能以数字打头。变量名不能包含空格和算术运算符( +、-等)。另外,变量名不能是保留的关键字。

3.1.3 声明并初始化多个类型相同的变量

变量 firstNumber、 secondNumber 和 multiplicationResult 属于同一种类型—都是整型,但在 3 行中分别声明。如果您愿意,可简化这 3 个变量的声明,在一行代码中完成,如下所示:

int firstNumber = 0, secondNumber = 0, multiplicationResult = 0;

需要时再声明变量通常是更好的选择,因为这让代码更容易理解—变量的声明离使用它的地方不远时,别人更容易了解变量的类型。

3.1.4 理解变量的作用域

常规变量(如前面定义的所有变量)的作用域很明确,只能在作用域内使用它们,如果您在作用域外使用它们,编译器将无法识别,导致程序无法通过编译。在作用域外面,变量是未定义的实体,编译器对其一无所知。

#include <iostream>
using namespace std;

void MultiplyNumbers ()
{
     cout << "Enter the first number: ";
     int firstNumber = 0;
     cin >> firstNumber;

     cout << "Enter the second number: ";
     int secondNumber = 0;
     cin >> secondNumber;

     // Multiply two numbers, store result in a variable
     int multiplicationResult = firstNumber * secondNumber;

     // Display result
     cout << firstNumber << " x " << secondNumber;
     cout << " = " << multiplicationResult << endl;
}

int main()
{
    cout << "This program will help you multiply two numbers" << endl;

    // Call the function that does all the work
    MultiplyNumbers();

    // cout << firstNumber << " x " << secondNumber;
    // cout << " = " << multiplicationResult << endl;

    return 0;
}

上述代码与之前的代码功能相同,但是通过函数调用的,在函数里面声明的变量只能在其定义域内有效,在其定义域(函数内部)外部无效。

3.1.5 全局变量

如果使用全局变量,其需要定义在main函数之外,功能相同的函数代码如下:

#include <iostream>
using namespace std;

// three global integers
int firstNumber = 0;
int secondNumber = 0;
int multiplicationResult = 0;

void MultiplyNumbers ()
{
    cout << "Enter the first number: ";
    cin >> firstNumber;

    cout << "Enter the second number: ";
    cin >> secondNumber;

    // Multiply two numbers, store result in a variable
    multiplicationResult = firstNumber * secondNumber;

    // Display result
    cout << "Displaying from MultiplyNumbers(): ";
    cout << firstNumber << " x " << secondNumber;
    cout << " = " << multiplicationResult << endl;
}
int main()
{
    cout << "This program will help you multiply two numbers" << endl;

     // Call the function that does all the work
     MultiplyNumbers();

     cout << "Displaying from main(): ";

     // This line will now compile and work!
     cout << firstNumber << " x " << secondNumber;
     cout << " = " << multiplicationResult << endl;

     return 0;
}

不分青红皂白地使用全局变量通常是一种糟糕的编程习惯。

3.1.6 命名约定

每个单词的首字母都大写,这被称为 Pascal 拼写法 .

第一个单词的首字母采用小写,这被称为骆驼拼写法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值