C language 变量命名之underscore

本文介绍了C语言变量命名的规则,解释了为什么变量名可以包含下划线,并且详细阐述了C语言中字母的定义及变量命名的具体要求。

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

      又回到C了,苦逼的我,绕了一大圈,又回到了起点,(ˇˍˇ) 想~当年老师的苦口婆心,恨少年的轻狂,最终还是那句话,欠的债总是要还的,好啦,叨唠了一番,言归正传。

今天看了下C界的达摩圣书<C程序设计语言>,就是K&R写的那本,读了第一张,果不其然,确实是一本好书,讲的很全面,例如变量命名规范,这里为什么变量命名开头除了字母还有underscore,来,我们来看下面这句话“C language, developed at Bell Labs in the early 1970s, allowed the underscore as an alphabetic character.",哈哈,看到这句话大家会焕然大悟了吧,其实下划线在C中也是被认为是一个alphabetic,也就是字母字符,这是为什么呢,我们再来看看

The Standard requires that an alphabet of 96 symbols is available    for C as follows:

a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
! " # % & ' ( ) * + , - . /
: ; < = > ? [ \ ] ^ _ { | } ~
space, horizontal and vertical tab
form feed, newline

Table 2.1. The Alphabet of C

大家看到了吧,C语言中定义了96符号作为英文字母(注意:这与我们平常认为的英文字母只有26个是不一样的),他们在ASCII码的什么位置呢,再来看看下面这句话:

The ASCII codes (American Standard Code for   Information Interchange) used the numbers from 32 to 127 to represent   characters of the English alphabet. These 96 codes corresponded to the upper and lower   case alphabet, the numerals, and the most useful symbols for punctuation.

也就是32到127代表了英文字母,而变量命名规定只能用52个大小写字母、以及10个数字,和下划线,原话如下:

The rules for the construction of identifiers are simple: you may use    the 52 upper and lower case alphabetic characters, the 10 digits and    finally the underscore ‘_’, which is considered to be    an alphabetic character for this purpose. The only restriction is the    usual one; identifiers must start with an alphabetic    character.

    大家现在明白了吗,为什么变量可以以下划线开头,但是这边为什么变量取名只能包含字母、数字或下划线,而不能包含其他字符,这个我就不清楚了,书本没说,好啦,这就是今天看第一章的一点小心得,明天继续,A~ZA

 

附注:Reference link:http://publications.gbdirect.co.uk/c_book/chapter2/keywords_and_identifiers.html

                                   http://www.alvechurchdata.co.uk/company/softasc.html

                                   http://en.wikipedia.org/wiki/Underscore

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值