0.背景
最近在看很久以前的一本關於C語言的書,英文名時 The C Programming Language,中文名《C程序設計語言》。 書中引言很概括地表達了C語言設計的總體理念,適合經常翻閱,所以記錄一下。
1.內容摘錄
C 是一種通用的程序設計語言。它是在 UNIX 系統上產生的,與 UNIX 系統緊密相關——UNIX系統和大多運行於其上的程序都是用 C 編寫的。 但它並不特定於任何一個操作系統或機器。它很適合用於編寫編譯器和操作系統而被稱為“系統程序設計語言”。然而它同樣可以被很好地用於在不同領域編寫主要程序。
C 的很多重要概念源於 Martin Richards 開發的 BCPL 語言。
BCPL 對 C 的影響間接來自於 Ken Thompson 在1970年為第一個 UNIX 系統在DEC PDP-7
機器上開發 B 語言。
BCPL 和 B 都是“無類型”語言。與此相比,C 提供了很多數據類型。
基本類型有不同大小的字符型、整型以及浮點型。除此之外還有從指針、數組、結構、聯合等產生的派生數據類型層次。表達式由運算符和操作數構成。
任何一個表達式,包括賦值或函數調用,都可以是一個語句。指針提供了獨立於機器的地址算術。
C 為實現結構良好的程序提供了基礎的控制流結構:語句組合、決策判斷(if-else)、分支選擇(switch)、循環終止檢測如頂端檢測(while、for)和底端檢測(do)以及提前跳出循環(break)等。
函數可以返回基本類型的值、結構、聯合或指針。函數可以遞歸調用。局部變量通常是“自動類型”,即每次函數調用時重新創建變量。
函數定義不可以嵌套,但變量可以用塊結構的方式來聲明。C程序的函數能以單獨的源文件形式存在,這些文件分開編譯。
變量對於函數可以是內部的、也可以是外部的,可以只在一個外部源文件中可見,也可以對整個程序都是可見的。
編譯的預處理階段對程序文本進行宏替換,將其他源文件包含進來,並進行條件編譯。
C 是一種相對低級的語言,這意味着 C可以處理大部分計算機可以處理的對象,如字符、數字和地址。這些對象可以用由真實機器實現的算術運算或邏輯運算來組合或移動。
- C 不提供直接處理復合對象如字符串、集合、列表或數組等的操作。雖然結構可以被當作一個整體進行拷貝,但不存在處理整個數組或字符串的操作。
- 除了靜態定義和由函數的局部變量提供的棧規則之外,C 語言不提供任何內存分配工具。
- C 也不提供堆和垃圾內存回收機制。
- 最后,C 也不提供輸入輸出工具,沒有
READ/WRITE
語句,沒有內置文件訪問方法。所有這些高級機制必須由顯示調用的函數提供。C 的大多數實現已經包含了這類函數的標准集合。 - C 只提供簡單的單線程控制流:測試、循環、組合和子程序。但不提供多道程序設計、並行操作、同步以及協同例程。
盡管缺少其中一些功能似乎是一個嚴重的缺陷,但將語言保持在適度的大小確實有好處。
由於 C 相對較小,可以使用很小的篇幅將其描述出來,一個程序員能在較短時間內學會、理解並實際使用整個語言。
很多年來,C 的定義就是《The C Programming Language》第1版中的參考手冊。1983年美國國家標准協會(ANSI)成立了一個委員會提供 C 語言的現代化的、易於理解的定義。
最終於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)。轉載請注明出處!
限於本人水平,如果文章和代碼有表述不當之處,還請不吝賜教。