forked from WQBin/WQBin.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: C++变量学习 | ||
layout: post | ||
categories: 编程语言 | ||
tags: 'C++,变量,C++ primer' | ||
--- | ||
<span id="jump"></span> | ||
|
||
|
||
## **变量定义** | ||
----------------------------- | ||
<br/> | ||
|
||
- 1.初始值不是赋值。初始化的含义是创建变量时赋予其一个初始值。而赋值的含义是把对象当前值擦除,以一个新值来代替。 | ||
|
||
- 2.定义可以为分为列表初始化和默认初始化。 | ||
<br/> | ||
<br/> | ||
------------------------------- | ||
## **变量声明与定义的关系** | ||
-------------------------------- | ||
<br/> | ||
### 为了允许把程序拆分成多个逻辑部分来编写,C++语言支持分离式编译机制,该机制允许将程序分隔为若干个文件,每个文件可被独立编译。为了支持分离式编译,C++语言将声明和定义区别开来 | ||
# | ||
|
||
|
||
- `声明`使得名字为变量所知 | ||
- `定义`负责创建与名字关联的实体 | ||
<br/> | ||
# | ||
``` c++ | ||
extern int i; //声明i,不是定义 | ||
int j;//声明并定义j | ||
extern double pi=3.1416;//定义 | ||
``` | ||
<br/> | ||
--------------------------------- | ||
## **标识符** | ||
------------------------------------ | ||
<br/> | ||
|
||
# | ||
>`C++标识符`由字母、数字和下划线组成,其中必须以字母或者下划线开头,标识符的长度没有限制,但是对大小写字母敏感。 | ||
以下为变量命名规范: | ||
1. 标识符要能体现实际含义 | ||
2. 变量名一般用小写字母,如index | ||
3. 用户自定义类名一般以大写字母开头 | ||
4. 如果标识符由多个单词组成,则单词之间要有明显区分 | ||
<br/> | ||
<br/> | ||
|
||
---------------------------------------- | ||
## 名字的作用域 | ||
---------------------------------------- | ||
|
||
|
||
>作用域是程序的一部分,在其中名字有其特定的含义。C++语言中,大多数作用域都以花括号为分隔。 | ||
|
||
# | ||
同一个名字在不同的作用域中可能指向不同的实体,名字的有效区域始于名字的声明语句,以声明语句所在的作用域末端为结束。 | ||
<br/> | ||
### **嵌套的作用域** | ||
1. 内层作用域:作用域能彼此包含,被包含的作用域为内层作用域。 | ||
2. 外层作用域:包含着别的作用域的作用域。 | ||
<br/> | ||
<br/> | ||
<br/> | ||
<br/><br/><br/><br/><br/><br/><br/><br/><br/> | ||
##<center>[回到顶部](#jump) | ||
<br/> | ||
|
||
>#*<center>The End* |