读谭师傅的书(6) 读谭师傅的书(8)
Mar 26

之前已经学习了第二章,现在来看第三章,《数据类型、运算符与表达式》。

第3.1节:

1、谭师傅说,struct是C语言提供的数据结构。这里还得有请谭师傅“翻译”一下所谓的“数据结构”是个什么东西,不然此类“谭氏术语”看得多了之后,头都会大三圈的。

2、谭师傅还把C语言里的数据类型分成了几大类,可谓标新立异,用心良苦。现在让我们来欣赏一下“谭氏C语言类型“(如下图)。

tan-3.1

通常,我们都会把谭师傅所说的基本类型叫做算术类型,因为这些类型的数据可以进行加、减、乘、除的运算,而不是像谭师傅那样把指针当成一更”高级“的类型。另外,谭师傅所谓的“整型”,大概是包括int, short int 以及long int。在标准C里,这些类型和字符型、枚举型一并称为“Integral Type”(中文ANSI C标准译为“整型”,但容易和int类型搞混),因为所有这些类型都可以用整数来表示。谭师傅所谓“构造类型”中的结构体和共用体,我们一般称之为“复合类型”,因为它们可以将不同类型的数据组合到一起。最后,C语言里没有什么“空类型”。换句话说,C不允许没有类型的类型。当然,谭师傅很有可能指的是“void”。不过,void可不能单独使用。void*表示可以指向任何类型的指针,void函数表示函数不返回值,而函数形参用void则表示函数不带参数。不知道谭师傅所谓的“空类型”是哪一种?

第3.2节:

3、例3.1中,谭师傅先定义了PRICE,再#include<stdio.h>。这样也不能说不对,但养成了习惯就不太好。

4、谭师傅说,符号常量不能被赋值,不过没有讲为什么不能被赋值。作为努力寻求知识的学生,我很希望看到谭师傅讲清楚#define 实际上是做了替换,把PRICE换成了30,这样,不能写30=40这样的语句不就很清楚了吗?

5、谭师傅说,PRICE的“作用域”(姑且认为师傅讲的是scope)在例3.1中是主函数。我看了心里一阵嘀咕。符号常量有“作用域”吗?当preprocessor把PRICE换成了30后,它就是一个常数。常数有什么“作用域”?还是“主函数”?

6、谭师傅在讲到变量时教导我们说,“由编译系统给每一个变量分配对应的内存地址“。嗯,让我想想,如果有一函数f,它里面用到了一个局部变量a,但这个函数从来没被调用过,那么这个a有被分配内存地址吗?如果有被调用过呢?是“编译系统”给分配的吗?

7、我也很希望谭师傅能够在讲变量时加一句,尽可能不要使用下划线开头的变量名,以免和系统定义的标识符冲突。很遗憾,没有看到。

8、谭师傅还斩钉截铁地说,“ANSI C标准没有规定标识符的长度“。这一句话让我赶快把K&R又翻出来看了一下。还好,C还是那个可移植的C。C89规定了一般标识符最少要识别31个字符,并区分大小写(external的6个字符不分大小写)。谭师傅提到PC上的MS C不知道是什么年代的古董,只认8个字符。我劝谭师傅还是把那破玩艺砸了,把字节都掰成比特扔掉。“建议变量名的长度最好不要超过8个字符“这种“谭氏C标准”还是留给师傅自己用吧。

9、谭师傅还举了几个长变量名的例子,例如student-name和student-number。且慢,这两个不是是非法变量名么?

10、虽然谭师傅提倡用长变量名,要见名知义,不过呢,师傅“在一些简单的举例中,为了方便起见,仍用单字符的变量名“。还好我心细,看到了这句,不然还以为写这些例子的是个菜鸟。

11、谭师傅又一次把“声明”说成“定义”。咳,说什么好呢。

12、最后,有一处错误信息里的“Undefined“被拼成了”Undifeed“。还好大多数人也就只看前三个字母,无伤大雅,无伤大雅。

好了,第3.1节和3.2节就学习到这里。下次接着学

“读谭师傅的书(7)”有13篇评论

  1. wupi Says:

    读得真仔细。赞!

  2. ronaldfree Says:

    我晕!我就是看谭师傅的书自学的C语言,怪不得水平总是难以提高。
    楼主,帮忙推荐一本比较好的C语言教材吧。谢谢!



    C语言的经典《The C Programming Language》第二版是目前我所知的最好教材。作者之一Dennis Ritchie就是C语言的创始人。也可以考虑《The C Book》。网上搜索一下都能找到。

  3. ronaldfree Says:

    好的,谢谢!

  4. 基本 Says:

    分类看上去就乱了。评论部分能不能markup?


    可以直接用HTML的tag。 — eng

  5. 基本 Says:

    重排型(type)的分类(classification)

    见《STANDARD C》(P.J.PLAUGER和JIM BROODIE作)
    function types
    data object types
      nonscalar types
        struct types, union types, array types
      scalar types
        arithmetic types
          integer types
            char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long
            plain bitfields, signed bitfields, unsigned bitfields
            enumeration types
          floating types
            float, double, long double
        pointer types
          pointer to function types, pointer to data object types, pointer to in complete types
    incomplete types
      void
      incomplete struct types, incomplete union types, incomplete array types

    K&R的首版是这样分的(A.4):
    fundamental types
      arithmetic types
        integral types
          char
          short int, int, long int
          unsigned
        floating types
          float, double
    derived types
      array of objects of most types
      functions which return objects of a given type
      pointers to objects of a given type
      structures containing a sequence of objects of various types
      unions capable of containing any one of several objects of various types

    K&R第二版只是增加了几种新型(A.4.2和A.4.3):
    basic types
      arithmetic types
        integral types
          char,unsigned char,signed char
          short int, int, long int
          unsigned short int, unsigned int, unsigned long int
    enumerations
        floating types
          float, double,long double
      void
    derived types
      array of objects of most types
      functions which return objects of a given type
      pointers to objects of a given type
      structures containing a sequence of objects of various types
      unions capable of containing any one of several objects of various types

  6. 基本 Says:

    谢谢清理,再试试html tag:

    见《STANDARD C》(P.J.PLAUGER和JIM BROODIE作)

    function types
    data object types
      nonscalar types
        struct types, union types, array types
      scalar types
        arithmetic types
          integer types
            char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long
            plain bitfields, signed bitfields, unsigned bitfields
            enumeration types
          floating types
            float, double, long double
        pointer types
          pointer to function types, pointer to data object types, pointer to in complete types incomplete types
      void
      incomplete struct types, incomplete union types, incomplete array types

    K&R的首版是这样分的(A.4):

    fundamental types
      arithmetic types
        integral types
          char
          short int, int, long int
          unsigned
        floating types
          float, double
    derived types
      array of objects of most types
      functions which return objects of a given type
      pointers to objects of a given type
      structures containing a sequence of objects of various types
      unions capable of containing any one of several objects of various types

    K&R第二版只是增加了几种新型(A.4.2和A.4.3):

    basic types
      arithmetic types
        integral types
          char,unsigned char,signed char
          short int, int, long int
          unsigned short int, unsigned int, unsigned long int
          enumerations
        floating types
          float, double,long double
      void
    derived types
      array of objects of most types
      functions which return objects of a given type
      pointers to objects of a given type
      structures containing a sequence of objects of various types
      unions capable of containing any one of several objects of various types

  7. eric Says:

      教材不是标准的说明书,要从有利于读者理解出发。事实上国内的教材对数据类型的分类和谭老师的分类是大同小异的。各种教材有不同的表叙,很难说那一种绝对正确,那一种绝对错,只要读者容易理解,使用方便就可以了。
      难道一定要把枚举类型放到整型中去讲吗?在外部形式上,枚举与整型是不同的,你试试把枚举放到整型一起讲,效果怎样。教材不是手册。而且枚举类型对初学者用得并不多。该书在稍后介绍了枚举类型,说明了枚举变量的值是一个整数,这就够了。有什么不可以。不要强加于人。

  8. eng Says:

    eric,不懂拜托不要装懂。国内的教材和谭师傅写得一样谭师傅就写对了?为什么国外的教材(包括C语言创始人写的教材)都和谭师傅不一样?这说明什么问题还不清楚吗?

    是的,枚举类型一定要放到整数类型里讲。因为,怎么说呢,它就是整数类型嘛,你还要放到哪里讲?还能放到哪里讲?说明枚举变量的值是个整数就行了?你完全没有型的概念嘛。指针的值不也是整数?

    教材不是手册,不等于教材就可以乱写。强加于人的正是谭师傅。好好的一个C语言讲的乱七八糟。

  9. Nika Says:

    是谁不懂装懂,拜托搞明白历史再开骂。出书是好事,与名人竞争也是好事,但是要通过正当渠道推销自己的书。当然站在巨人的肩膀上骂人出名
    会快些,倒是个捷径。尤其通过网络,一夜之间会让某个人臭,也会让某个人红起来。幸亏现在人们都比较冷静,会通过自己的头脑分析,不再被人瞎说搞晕。

  10. 思考 Says:

    eng口口声声号召大家学C语言就应当学K&R的“The C Programming Language”。不知道eng是在国内上大学的,还是在国外上大学的?你是学哪本书入门的?请注意,K&R声明:“本书并不是一本有关程序设计的入门性手册,它要求读者熟悉基本的程序设计概念,如变量、赋值语句、循环和函数等”。也就是说,应当有初步的程序设计知识和经验。eng如果是教师,可以试验一下,对零起点的学生,指定用K&R的“The C Programming Language”作为入门教材,看效果如何!学生不坐飞机才怪呢。这也难怪,eng是不懂教学的。

    据我所知,有些留学生到了美国,有机会接触到许多美国专家写的书,却写信回来买谭老师的书,认为谭老师的书好懂,便于入门。这是为什么?

  11. 尤里卡 Says:

    这是怎么了,谭浩强都批评不得?错了就错了嘛,还这么多人过来狡辩,恐怕谭知道这些,自己都会觉得不好意思。

  12. 穆扬 Says:

    中文ANSI C标准译为“整型”,但容易和int类型搞混
    这个见解好!

  13. 薛非 Says:

    《品悟C——抛弃C程序设计中的谬误与恶习》http://product.dangdang.com/product.aspx?product_id=22868365&_ddclickunion=P-306226-34242918-s15995126|ad_type=0|sys_id=1#dd_refer=http%3A%2F%2Fbook.douban.com%2Fsubject%2F15995126%2F

    薛非在此向《读谭师傅的书》系列博文作者鞠躬致谢!

发表评论

CAPTCHA Image
*