C++ 变量作用域
一般来说有三个地方可以定义变量:
-
在函数或一个代码块内部声明的变量,称为局部变量。
-
在函数参数的定义中声明的变量,称为形式参数。
-
在所有函数外部声明的变量,称为全局变量。
作用域是程序的一个区域,变量的作用域可以分为以下几种:
-
局部作用域:在函数内部声明的变量具有局部作用域,它们只能在函数内部访问。局部变量在函数每次被调用时被创建,在函数执行完后被销毁。
-
全局作用域:在所有函数和代码块之外声明的变量具有全局作用域,它们可以被程序中的任何函数访问。全局变量在程序开始时被创建,在程序结束时被销毁。
-
块作用域:在代码块内部声明的变量具有块作用域,它们只能在代码块内部访问。块作用域变量在代码块每次被执行时被创建,在代码块执行完后被销毁。
-
类作用域:在类内部声明的变量具有类作用域,它们可以被类的所有成员函数访问。类作用域变量的生命周期与类的生命周期相同。
注意:如果在内部作用域中声明的变量与外部作用域中的变量同名,则内部作用域中的变量将覆盖外部作用域中的变量。
局部变量
在函数或一个代码块内部声明的变量,称为局部变量。它们只能被函数内部或者代码块内部的语句使用。下面的实例使用了局部变量:
实例
#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; }
在程序中,局部变量和全局变量的名称可以相同,但是在函数内,局部变量的值会覆盖全局变量的值。下面是一个实例:
https://avg.163.com/topic/detail/10034653
https://avg.163.com/topic/detail/10034654
https://avg.163.com/topic/detail/10034652
https://avg.163.com/topic/detail/10034651
https://avg.163.com/topic/detail/10034646
https://avg.163.com/topic/detail/10034630
https://avg.163.com/topic/detail/10034609
https://avg.163.com/topic/detail/10034608
https://avg.163.com/topic/detail/10034607
https://avg.163.com/topic/detail/10034596
https://avg.163.com/topic/detail/10034591
https://avg.163.com/topic/detail/10034584
https://avg.163.com/topic/detail/10034583
https://avg.163.com/topic/detail/10034582
https://avg.163.com/topic/detail/10034581
https://avg.163.com/topic/detail/10034579
https://avg.163.com/topic/detail/10034576
https://avg.163.com/topic/detail/10034574
https://avg.163.com/topic/detail/10034575
https://avg.163.com/topic/detail/10034577
https://avg.163.com/topic/detail/10034573
https://avg.163.com/topic/detail/10034572
https://avg.163.com/topic/detail/10034553
https://avg.163.com/topic/detail/10034571
https://avg.163.com/topic/detail/10034570
https://avg.163.com/topic/detail/10034569
https://avg.163.com/topic/detail/10034568
https://avg.163.com/topic/detail/10034567
https://avg.163.com/topic/detail/10034566
https://avg.163.com/topic/detail/10034564
https://avg.163.com/topic/detail/10034565
https://avg.163.com/topic/detail/10034563
https://avg.163.com/topic/detail/10034562
https://avg.163.com/topic/detail/10034561
https://avg.163.com/topic/detail/10034560
https://avg.163.com/topic/detail/10034559
https://avg.163.com/topic/detail/10034558
https://avg.163.com/topic/detail/10034556
https://avg.163.com/topic/detail/10034557
https://avg.163.com/topic/detail/10034555
https://avg.163.com/topic/detail/10034554
https://avg.163.com/topic/detail/10034553
https://avg.163.com/topic/detail/10034551
https://avg.163.com/topic/detail/10034550
https://avg.163.com/topic/detail/10034549
https://avg.163.com/topic/detail/10034548
https://avg.163.com/topic/detail/10034547
https://avg.163.com/topic/detail/10034546
https://avg.163.com/topic/detail/10034545
https://avg.163.com/topic/detail/10034544
https://avg.163.com/topic/detail/10034543
https://avg.163.com/topic/detail/10034542
https://avg.163.com/topic/detail/10034539
https://avg.163.com/topic/detail/10034541
https://avg.163.com/topic/detail/10034540
https://avg.163.com/topic/detail/10034538
https://avg.163.com/topic/detail/10034517
https://avg.163.com/topic/detail/10034536
https://avg.163.com/topic/detail/10034534
https://avg.163.com/topic/detail/10034522
https://avg.163.com/topic/detail/10034535
https://avg.163.com/topic/detail/10034533
https://avg.163.com/topic/detail/10034522
https://avg.163.com/topic/detail/10034531
https://avg.163.com/topic/detail/10034530
https://avg.163.com/topic/detail/10034529
https://avg.163.com/topic/detail/10034527
https://avg.163.com/topic/detail/10034528
https://avg.163.com/topic/detail/10034526
https://avg.163.com/topic/detail/10034524
https://avg.163.com/topic/detail/10034523
https://avg.163.com/topic/detail/10034502
https://avg.163.com/topic/detail/10034521
https://avg.163.com/topic/detail/10034519
https://avg.163.com/topic/detail/10034520
https://avg.163.com/topic/detail/10034518
https://avg.163.com/topic/detail/10034517
https://avg.163.com/topic/detail/10034516
https://avg.163.com/topic/detail/10034514
https://avg.163.com/topic/detail/10034513
https://avg.163.com/topic/detail/10034512
https://avg.163.com/topic/detail/10034482
https://avg.163.com/topic/detail/10034511
https://avg.163.com/topic/detail/10034510
https://avg.163.com/topic/detail/10034509
https://avg.163.com/topic/detail/10034507
https://avg.163.com/topic/detail/10034505
https://avg.163.com/topic/detail/10034504
https://avg.163.com/topic/detail/10034503
https://avg.163.com/topic/detail/10034502
https://avg.163.com/topic/detail/10034501
https://avg.163.com/topic/detail/10034500
https://avg.163.com/topic/detail/10034499
https://avg.163.com/topic/detail/10034498
https://avg.163.com/topic/detail/10034497
https://avg.163.com/topic/detail/10034494
https://avg.163.com/topic/detail/10034495
https://avg.163.com/topic/detail/10034493
https://avg.163.com/topic/detail/10034492
https://avg.163.com/topic/detail/10034490
https://avg.163.com/topic/detail/10034491
https://avg.163.com/topic/detail/10034488
https://avg.163.com/topic/detail/10034489
https://avg.163.com/topic/detail/10034487
https://avg.163.com/topic/detail/10034486
https://avg.163.com/topic/detail/10034485
https://avg.163.com/topic/detail/10034484
https://avg.163.com/topic/detail/10034483
https://avg.163.com/topic/detail/10034482
https://avg.163.com/topic/detail/10034481
https://avg.163.com/topic/detail/10034478
https://avg.163.com/topic/detail/10034479
https://avg.163.com/topic/detail/10034477
https://avg.163.com/topic/detail/10034476
https://avg.163.com/topic/detail/10034475
https://avg.163.com/topic/detail/10034440
https://avg.163.com/topic/detail/10034473
https://avg.163.com/topic/detail/10034472
https://avg.163.com/topic/detail/10034445
https://avg.163.com/topic/detail/10034469
https://avg.163.com/topic/detail/10034470
https://avg.163.com/topic/detail/10034468
https://avg.163.com/topic/detail/10034467
https://avg.163.com/topic/detail/10034466
https://avg.163.com/topic/detail/10034465
https://avg.163.com/topic/detail/10034464
https://avg.163.com/topic/detail/10034463
https://avg.163.com/topic/detail/10034462
https://avg.163.com/topic/detail/10034460
https://avg.163.com/topic/detail/10034459
https://avg.163.com/topic/detail/10034458
https://avg.163.com/topic/detail/10034455
https://avg.163.com/topic/detail/10034454
https://avg.163.com/topic/detail/10034453
https://avg.163.com/topic/detail/10034452
https://avg.163.com/topic/detail/10034451
https://avg.163.com/topic/detail/10034447
https://avg.163.com/topic/detail/10034448
https://avg.163.com/topic/detail/10034445
https://avg.163.com/topic/detail/10034444
https://avg.163.com/topic/detail/10034443
https://avg.163.com/topic/detail/10034442
https://avg.163.com/topic/detail/10034441
https://avg.163.com/topic/detail/10034439
https://avg.163.com/topic/detail/10034420
https://avg.163.com/topic/detail/10034437
https://avg.163.com/topic/detail/10034435
https://avg.163.com/topic/detail/10034436
https://avg.163.com/topic/detail/10034434
https://avg.163.com/topic/detail/10034433
https://avg.163.com/topic/detail/10034417
https://avg.163.com/topic/detail/10034432
https://avg.163.com/topic/detail/10034431
https://avg.163.com/topic/detail/10034430
https://avg.163.com/topic/detail/10034428
https://avg.163.com/topic/detail/10034427
https://avg.163.com/topic/detail/10034426
https://avg.163.com/topic/detail/10034425
https://avg.163.com/topic/detail/10034423
https://avg.163.com/topic/detail/10034400
https://avg.163.com/topic/detail/10034422
https://avg.163.com/topic/detail/10034421
https://avg.163.com/topic/detail/10034420
https://avg.163.com/topic/detail/10034419
https://avg.163.com/topic/detail/10034418
https://avg.163.com/topic/detail/10034417
https://avg.163.com/topic/detail/10034416
https://avg.163.com/topic/detail/10034415
https://avg.163.com/topic/detail/10034414
https://avg.163.com/topic/detail/10034413
https://avg.163.com/topic/detail/10034412
https://avg.163.com/topic/detail/10034411
https://avg.163.com/topic/detail/10034410
https://avg.163.com/topic/detail/10034409
https://avg.163.com/topic/detail/10034406
https://avg.163.com/topic/detail/10034405
https://avg.163.com/topic/detail/10034402
https://avg.163.com/topic/detail/10034403
https://avg.163.com/topic/detail/10034404
https://avg.163.com/topic/detail/10034401
https://avg.163.com/topic/detail/10034400
https://avg.163.com/topic/detail/10034399
https://avg.163.com/topic/detail/10034398
https://avg.163.com/topic/detail/10034397
https://avg.163.com/topic/detail/10034396
https://avg.163.com/topic/detail/10034358
https://avg.163.com/topic/detail/10034395
https://avg.163.com/topic/detail/10034393
https://avg.163.com/topic/detail/10034391
https://avg.163.com/topic/detail/10034392
https://avg.163.com/topic/detail/10034390
https://avg.163.com/topic/detail/10034389
https://avg.163.com/topic/detail/10034388
https://avg.163.com/topic/detail/10034387
https://avg.163.com/topic/detail/10034385
https://avg.163.com/topic/detail/10034383
https://avg.163.com/topic/detail/10034382
https://avg.163.com/topic/detail/10034380
https://avg.163.com/topic/detail/10034379
https://avg.163.com/topic/detail/10034375
https://avg.163.com/topic/detail/10034378
https://avg.163.com/topic/detail/10034377
https://avg.163.com/topic/detail/10034338
https://avg.163.com/topic/detail/10034376
https://avg.163.com/topic/detail/10034284
https://avg.163.com/topic/detail/10034366
https://avg.163.com/topic/detail/10034362
https://avg.163.com/topic/detail/10034359
https://avg.163.com/topic/detail/10034358
https://avg.163.com/topic/detail/10034357
https://avg.163.com/topic/detail/10034356
https://avg.163.com/topic/detail/10034353
https://avg.163.com/topic/detail/10034355
实例
#include <iostream> using namespace std; // 全局变量声明 int g = 20; int main () { // 局部变量声明 int g = 10; cout << g; return 0; }
当上面的代码被编译和执行时,它会产生下列结果:
10
初始化局部变量和全局变量
当局部变量被定义时,系统不会对其初始化,您必须自行对其初始化。定义全局变量时,系统会自动初始化为下列值:
| int | 0 |
| char | '\\0' |
| float | 0 |
| double | 0 |
| pointer | NULL |
正确地初始化变量是一个良好的编程习惯,否则有时候程序可能会产生意想不到的结果。
块作用域指的是在代码块内部声明的变量:
实例
#include <iostream>
int main() {
int a = 10;
{
int a = 20; // 块作用域变量
std::cout << "块变量: " << a << std::endl;
}
std::cout << "外部变量: " << a << std::endl;
return 0;

