zoukankan      html  css  js  c++  java
  • 【学习笔记】C程序设计语言

    0.背景

    最近在看很久以前的一本关于C语言的书,英文名时 The C Programming Language,中文名《C程序设计语言》。 书中引言很概括地表达了C语言设计的总体理念,适合经常翻阅,所以记录一下。

    1.内容摘录

    C 是一种通用的程序设计语言。它是在 UNIX 系统上产生的,与 UNIX 系统紧密相关——UNIX系统和大多运行于其上的程序都是用 C 编写的。 但它并不特定于任何一个操作系统或机器。它很适合用于编写编译器和操作系统而被称为“系统程序设计语言”。然而它同样可以被很好地用于在不同领域编写主要程序。

    C 的很多重要概念源于 Martin Richards 开发的 BCPL 语言。
    BCPLC 的影响间接来自于 Ken Thompson 在1970年为第一个 UNIX 系统在DEC PDP-7机器上开发 B 语言。
    BCPLB 都是“无类型”语言。与此相比,C 提供了很多数据类型。
    基本类型有不同大小的字符型、整型以及浮点型。除此之外还有从指针、数组、结构、联合等产生的派生数据类型层次。表达式由运算符和操作数构成。
    任何一个表达式,包括赋值或函数调用,都可以是一个语句。指针提供了独立于机器的地址算术。

    C 为实现结构良好的程序提供了基础的控制流结构:语句组合、决策判断(if-else)、分支选择(switch)、循环终止检测如顶端检测(while、for)和底端检测(do)以及提前跳出循环(break)等。
    函数可以返回基本类型的值、结构、联合或指针。函数可以递归调用。局部变量通常是“自动类型”,即每次函数调用时重新创建变量。
    函数定义不可以嵌套,但变量可以用块结构的方式来声明。C程序的函数能以单独的源文件形式存在,这些文件分开编译。
    变量对于函数可以是内部的、也可以是外部的,可以只在一个外部源文件中可见,也可以对整个程序都是可见的。

    编译的预处理阶段对程序文本进行宏替换,将其他源文件包含进来,并进行条件编译。

    C 是一种相对低级的语言,这意味着 C可以处理大部分计算机可以处理的对象,如字符、数字和地址。这些对象可以用由真实机器实现的算术运算或逻辑运算来组合或移动。

    • C 不提供直接处理复合对象如字符串、集合、列表或数组等的操作。虽然结构可以被当作一个整体进行拷贝,但不存在处理整个数组或字符串的操作。
    • 除了静态定义和由函数的局部变量提供的栈规则之外,C 语言不提供任何内存分配工具。
    • C 也不提供堆和垃圾内存回收机制。
    • 最后,C 也不提供输入输出工具,没有READ/WRITE语句,没有内置文件访问方法。所有这些高级机制必须由显示调用的函数提供。C 的大多数实现已经包含了这类函数的标准集合。
    • C 只提供简单的单线程控制流:测试、循环、组合和子程序。但不提供多道程序设计、并行操作、同步以及协同例程。

    尽管缺少其中一些功能似乎是一个严重的缺陷,但将语言保持在适度的大小确实有好处。
    由于 C 相对较小,可以使用很小的篇幅将其描述出来,一个程序员能在较短时间内学会、理解并实际使用整个语言。

    很多年来,<b>C</b> 的定义就是<b><i>《The C Programming Language》</i></b>第1版中的参考手册。1983年美国国家标准协会(ANSI)成立了一个委员会提供 <b>C</b> 语言的现代化的、易于理解的定义。
    

    最终于1988年完成了ANSI标准,即“ANSI C”。这个标准的大部分特性已被现代编译器所支持。
    这个标准是基于最初的参考手册。制订标准的目标之一是确保现有的程序仍然可以有效编译运行,或者当编译失败时,编译器会产生对于新动作的警告信息。
    对于大部分程序员来说,最重要的变化是函数声明和定义相关的新语法。
    现在函数声明可以包含描述参数的信息。为了与声明相匹配,函数定义的语法也做了相应地改变。这些附加信息使编译器更容易检测到由于参数不匹配造成的错误。
    还有一些其它小规模的变化。

    • 一直被广泛使用的结构赋值和枚举现在成了语言的正式内容。
    • 浮点运算现在可以在单精度下进行。
    • 算术运算,特别是无符号类型的运算的属性,现在也被清晰地阐述了。
    • 预处理器得到了更详细的说明。
      所有这些改变对大部分程序员的影响都比较小。

    标准“ANSI C”的第二个突出贡献是定义了一个 C 的函数库。它说明了诸如访问操作系统、格式化输入输出、内存分配、字符串操作等的函数。一个标准头文件的集合提供了统一的访问函数和数据类型声明的方法。使用这个库来与宿主系统交互的程序能确保兼容性。这个库的大部分与 UNIX 系统的“标准I/O库”很相似。在第1版中对这个库已有所描述。截至目前这个库也已经在很多其他系统中广泛应用。同样,这对于大多数程序员的影响很小。

    由于大多数计算机支持 C 提供的数据类型和控制结构,只需要很小的运行时库即可实现自包含的程序。标准库函数总是被显示地调用,因此如果没有必要可以避免这些调用。除了涉及到操作系统的细节,大部分库函数可以用 C 来编写,并具有可移植性。
    虽然 C 与大部分计算机能力相匹配,但它其实是独立于任何特定的机器结构的。只要稍加细心即可写出可移植的程序,无需修改就可以运行在很多硬件平台上。标准“ANSI C”明确地提出了可移植性问题,并预设了一个常量集合来说明程序时所在机器的特性。
    C 不是一种强类型的语言,但随着它的发展,其类型检查机制不断被强化。
    C 最初的定义虽然不赞成将指针和整型数值混用,但却允许这样使用。现在已经不再允许这样的用法。这个标准现在要求正确的声明和显示的强制转换。实际上有一些好的编译器已经有这样的要求。新的函数声明方式是这个方向上的另一个尝试。编译器会对大部分的类型错误给出警告,并不再进行不相容的数据类型之间的自动转换。然而,C 保留了其初始的设计思想,即认为编程人员知道他们在做什么,只要求他们明确地显示地表达他们的意图。

    像其他语言一样,C语言也有瑕疵。一些运算符的优先级不合适;某些语法一部分的还可以做得更好。尽管如此,对很多程序设计的应用而言,C已被证明是一个非常高效且表达能力很强的语言。

    2.原文摘抄

    C is a general-purpose programming language. It has been closely associated with the UNIX system where it was developed, since both the system and most of the programs that run on it are written in C. The language, however, is not tied to any one operating system or machine; and although it has been called a "system programming language" because it is useful for writing compilers and operating systems, it has been used equally well to write major programs in many different domains.

    Many of the important ideas of C stem from the language BCPL, developed by Martin Richards. The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken Thompson in 1970 for the first UNIX system on the DEC PDP-7. BCPL and B are "typeless" language. By contrast, C provides a variety of data types. The fundamental types are characters, and integers and floating-point numbers of several sizes. In addition, there is a hierarchy of derived data types created with pointers, arrays, structures, and unions. Expressions are formed from operators and operands; any expression, including an assignment or a function call, can be a statement. Pointers provide for machine-independent address arithmetic.

    C provides the fundamental control-flow constructions required for well-structured programs: statement grouping, decision making(if-else), selecting one of a set of possible cases(switch), looping with the termination test at the top(while, for) or at the bottom(do), and early loop exit(break).

    Functions may return values of basic types, structures, unions, or pointers. Any function may be called recursively. Local variable are typically "automatic", or created anew with each invocation. Function definitionis may not be nested but variables may be declared in a block-structured fashion. The functions of a C program may exist in separate source files that are compiled separately. Variables may be internal to a function, external but known only within a single source file, or visible to the entire program.

    A preprocessing step performs macro substitution on program text, inclusion of other source files, and conditional compilation.

    C is a relatively "low level" language. This characterization is not pejorative; it simply means that C deals with the same sort of objects that most computers do, namely characters, numbers, and address. These may be combined and moved about with the arithmetic and logical operators implemented by real machines.

    C provides no operations to deal directly with composite objects such as character strings, sets, lists, or arrays. There are no operations that manipulate an entire array or string, although structures may be copied as a unit. The language does not define any storage facility other than static definition and the stack discipline provided by the local variables of functions; there is no heap or garbage collection. Finally, C itself provides no input/output facilities; there are no READ or WRITE statements, and no built-in file access methods. All of these higher-level mechanisms must be provided by explicitly-called functions. Most C implementations have included a reasonably standard collection of such functions.

    Similarly, C offers only straightforward, single-thread control flow: tests, loops, grouping, and subprograms, but not multiprogramming, parallel operations, synchronization, or coroutines.

    Although the absence of some of these features may seem like a grave deficiency, keeping the language down to modest size has real benefits. Since C is relatively small, it can be described in a small space, and learned quickly. A programmer can reasonably expect to know and understand and indeed regularly use the entire language.

    For many years, the definition of C was the reference manual in the first edition of The C Programming Language. In 1983, the American National Standards Institute(ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI C", was completed in 1988. Most of the features of the standard are already supported by modern compilers.

    The standard is based on the original reference manual. The language is relatively little changed; one of the goals of the standard was to make sure that most existing programs would remain valid, or, failing that, that compilers could produce warnings of new behavior.

    For most programmers, the most important change is a new syntax for declaring and defining functions. A function declaration can now include a description of the arguments of the function; the definition syntax changes to match. This extra information makes it much easier for compilers to detect errors caused by mismatched arguments; in our experience, it is a very useful addition to the language.

    There are other small-scale language changes. Structures assignment and enumerations, which had been widely available, are now officially part of the language. Floating-point computations may now be done in single precision. The properties of arithmetic, especially for unsinged types, are clarified. The preprocessor is more elaborate. Most of these changes will have only minor effects on most programmers.

    A second significant contribution of the standard is the definition of a library to accomany C. It specifies functions for accessing the operating system, formatted input and output, memory allocation, string manipulation, and the like. A collection of standard headers provides uniform access to declarations of functions and data types. Programs that use this library to interact with a host system are assured of compatible behavior. Most of the library is closely modeled on the standard I/O library of the UNIX system. This library was described in the first edition, and has been widely used on other systems as well. Again, most programmers will not see much change.

    Because the data types and control structures provided by C are supported directly by most computers, the run-time library required to implement self-contained programs is tiny. The standard library functions are only called explicitly, so they can be avoided if they are not needed. Most can be written in C, and except for the operating system details they conceal, are themselves portable.

    Although C matches the capabilities of many computers, it is independent of any particular machine architecture. With a little care it is easy to write portable programs, that is, programs that can be run without change on a variety of hardware. The standard makes portability issues explicit, and prescribes a set of constants that characterize the machine on which the program is run.

    C is not a strongly-typed language, but as it has evolved, its type-checking has been strengthened. The original definition of C frowned on, but permitted, the interchange of pointers and integers; this has long since been eliminated, and the standarrd now requires the proper declarations and explicit conversions that had already been enforced by good compilers. The new funciton declarations are another step in this direction. Compilers will warn of most type errors, and there is no automatic conversion of incompatible data types. Nevertheless, C retains the basic philosophy that programmers know what they are doing; it only requires that they state their intentions explicitly.

    C, like any other language, has its blemishes. Some of the operators have the wrong precedence; some part of the syntax could be better. Nonetheless, C has proven to be an extremely effective and expressive language for a wide variety of programming applications.

    (全文完)


    本文作者 :phillee
    发表日期 :2021年11月17日
    本文链接https://www.cnblogs.com/phillee/p/15567508.html
    版权声明 :自由转载-非商用-非衍生-保持署名(创意共享3.0许可协议/CC BY-NC-SA 3.0)。转载请注明出处!
    限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。

  • 相关阅读:
    问题:plugin with id 'android' not found
    问题:plugin with id 'android' not found
    【NYOJ】[122]Triangular Sums
    【NYOJ】[122]Triangular Sums
    【NYOJ】[113]字符串替换
    【NYOJ】[113]字符串替换
    【NYOJ】[111]分数加减法
    【NYOJ】[111]分数加减法
    【NYOJ】[101]两点距离
    【NYOJ】[101]两点距离
  • 原文地址:https://www.cnblogs.com/phillee/p/15567508.html
Copyright © 2011-2022 走看看