Storing Information: Variables and Constants in C (二)

本文介绍了C语言中变量的概念及命名规则。变量是计算机内存中命名的数据存储位置。C语言变量名可含字母、数字和下划线,首字符须为字母或下划线,区分大小写,不能用关键字。还提及命名长度、风格,如用下划线或驼峰记法,强调使用描述性名称和统一风格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Storing Information with Variables

A variable is a named data storage location in your computer's memory. By using a variable's name in your program, you are, in effect, referring to the data stored there.

Variable Names

To use variables in your C programs, you must know how to create variable names. In C, variable names must adhere to the following rules:

  • The name can contain letters (a to z and A to Z), digits (0 to 9), and the underscore character (_).

  • The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended at the beginning of a name. A digit (0 to 9) cannot be used as the first character.

  • Case matters (that is, upper- and lowercase letters). C is case-sensitive, thus, the names count and Count refer to two different variables.

  • C keywords can't be used as variable names. A keyword is a word that is part of the C language. (A complete list of the C keywords can be found in Appendix B, "Reserved Words.")

The following list contains some examples of legal and illegal C variable names:

Variable Name

Legality

Percent Legal
y2x5__fg7h Legal
annual_profit Legal
_1990_tax Legal but not advised
savings#account Illegal: Contains the illegal character #
double Illegal: Is a C keyword
4sale Illegal: First character is a digit

Because C is case-sensitive, the names percent, PERCENT, and Percent would be considered three different variables. C programmers commonly use only lowercase letters in variable names, although this isn't required. Using all-uppercase letters is usually reserved for the names of constants (which are covered later today).

For many compilers, a C variable name can be up to 31 characters long. (It can actually be longer than that, but the compiler looks at only the first 31 characters of the name.) With this flexibility, you can create variable names that reflect the data being stored. For example, a program that calculates loan payments could store the value of the prime interest rate in a variable named interest_rate. The variable name helps make its usage clear. You could also have created a variable named x or even ozzy_osborne; it doesn't matter to the C compiler. The use of the variable, however, wouldn't be nearly as clear to someone else looking at the source code. Although it might take a little more time to type descriptive variable names, the improvements in program clarity make it worthwhile.

Many naming conventions are used for variable names created from multiple words. You've seen one style: interest_rate. Using an underscore to separate words in a variable name makes it easy to interpret. The second style is called camel notation. Instead of using spaces, the first letter of each word is capitalized. Instead of interest_rate, the variable would be named InterestRate. Camel notation is gaining popularity, because it's easier to type a capital letter than an underscore. The underscore is used in this book because it's easier for most people to read. You should decide which style you want to adopt.

DO use variable names that are descriptive.

DO adopt and stick with a style for naming your variables.

DON'T start your variable names with an underscore unnecessarily.

DON'T name your variables with all capital letters unnecessarily.

<think>我们注意到用户的问题是关于pip安装时出现的"Double requirement given"错误。根据引用[1]中的示例,错误信息通常是由于在requirements.txt文件中出现了重复的包声明。例如,同一个包(如numpy)被列出了两次,但版本不同或相同。 错误信息格式通常为: Double requirement given: [包名]==[版本] ... (already in [包名]==[版本] ...) 用户遇到的错误是:Double requirement given: install (already in install, name='install')。这看起来比较特殊,因为通常包名不会叫"install"。可能的原因是在requirements.txt文件中出现了两行都包含"install"的情况,或者可能是由于某种原因将"install"误解为一个包名。 然而,根据常见情况,我们推测用户的问题可能是: 1. requirements.txt文件中确实有重复的行(可能是同一个包重复,或者有多个行都写了同一个包的不同版本)。 2. 或者,requirements.txt文件中可能包含了一些非标准的行,例如误将命令(如`pip install`)写入了requirements.txt。 解决步骤: 步骤1:检查requirements.txt文件 - 打开requirements.txt文件,检查是否有重复的包声明。特别注意同一个包是否出现了两次(即使版本不同也会冲突)。 - 检查是否有行包含非包声明的内容,例如写入了`install`这样的命令。 步骤2:清理requirements.txt - 删除重复的行,确保每个包只出现一次。如果同一个包需要指定版本,只能保留一个版本(通常保留最新的或项目要求的版本)。 - 删除所有非包声明的行(例如,不能有`pip install`这样的命令,只能是`包名==版本`的形式)。 步骤3:重新安装 - 清理后,再次运行`pip install -r requirements.txt`。 如果用户的问题是由于在requirements.txt中写入了`install`这个单词(可能是误写),那么删除包含`install`的行即可。 示例: 假设requirements.txt内容如下: numpy==1.12.1 install pandas==1.0.3 那么第行的`install`就是错误的,应该删除。 另外,如果用户是通过其他方式生成的requirements.txt(例如使用`pip freeze`),通常不会出现这种情况。所以可能是手动编辑时不小心加入的。 如果用户的问题不是上述情况,那么可能是pip的bug,但这种情况非常罕见。 下面我们按照用户要求,用清晰的结构给出解决方案。</think>### 解决 "Double requirement given" 错误的方法 这个错误通常发生在 `requirements.txt` 文件中存在重复或冲突的包声明时。以下是详细解决步骤: #### 🔍 步骤 1:检查 requirements.txt 文件 1. 打开 `requirements.txt` 文件 2. 检查是否有重复的包声明(如两个 `install` 行) 3. 查找版本冲突(如 `numpy==1.12.1` 和 `numpy==1.9.3`) 4. 特别注意格式错误(如误将命令 `install` 当作包名) #### 🛠 步骤 2:清理 requirements.txt ```diff # 错误示例 install numpy==1.12.1 + numpy==1.9.3 # 重复声明 pandas==1.3.4 # 修正后 - install # 删除无效行 numpy==1.12.1 # 保留一个版本 pandas==1.3.4 ``` #### ⚙ 步骤 3:使用哈希验证(推荐) ```bash pip install --require-hashes -r requirements.txt ``` 此命令会严格检查包哈希值,防止重复安装[^1] #### 🔧 步骤 4:手动安装冲突包 若仍有冲突,单独安装: ```bash # 先卸载冲突版本 pip uninstall numpy -y # 再安装指定版本 pip install numpy==1.12.1 ``` #### ✅ 验证修复 ```bash pip check # 检查依赖冲突 pip list | grep numpy # 确认安装版本 ``` #### ⚠ 预防措施 1. 生成 requirements 时使用: ```bash pip freeze --exclude-editable > requirements.txt ``` 2. 安装前先检查冲突: ```bash pip check ``` 3. 使用虚拟环境隔离项目: ```bash python -m venv myenv source myenv/bin/activate ``` > **关键点**:此错误通常由三种情况引起: > 1. 文件包含无效行(如 `install`) > 2. 同一包多次声明 > 3. 不同版本的相同包冲突[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值