C Programming K Amp;r

1 view
Skip to first unread message

Trinh Livingston

unread,
Aug 5, 2024, 7:12:55 AM8/5/24
to scaltacrota
TheC Programming Language (sometimes termed K&R, after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the C programming language, as well as co-designed the Unix operating system with which development of the language was closely intertwined. The book was central to the development and popularization of C and is still widely read and used today. Because the book was co-authored by the original language designer, and because the first edition of the book served for many years as the de facto standard for the language, the book was regarded by many to be the authoritative reference on C.[1][2]

C was created by Dennis Ritchie at Bell Labs in the early 1970s as an augmented version of Ken Thompson's B.[3]Another Bell Labs employee, Brian Kernighan, had written the first C tutorial,[4]and he persuaded Ritchie to coauthor a book on the language.[5]Kernighan would write most of the book's "expository" material, and Ritchie's reference manual became its appendices.


The first edition, published February 22, 1978, was the first widely available book on the C programming language. Its version of C is sometimes termed K&R C (after the book's authors), often to distinguish this early version from the later version of C standardized as ANSI C.[6]


In April 1988, the second edition of the book was published, updated to cover the changes to the language resulting from the then-new ANSI C standard, particularly with the inclusion of reference material on standard libraries. The second edition of the book (and as of 2024[update], the most recent) has since been translated into over 20 languages.[7] In 2012, an eBook version of the second edition was published in ePub, Mobi, and PDF formats.[8]


C was first standardized in 1989 (as ANSI X3.159-1989) and has since undergone several revisions. However, no new edition of The C Programming Language has been issued to cover the more recent standards.


Byte magazine stated in August 1983, "[The C Programming Language] is the definitive work on the C language. Don't read any further until you have this book!"[1] Jerry Pournelle wrote in the magazine that year that the book "is still the standard ... a bit terse". He continued, "You can learn the C language without getting Kernighan and Ritchie, but that's doing it the hard way. You're also working too hard if you make it the only book on C that you buy."[9]


The C Programming Language has often been cited as a model for technical writing, with reviewers describing it as having clear presentation and concise treatment. Examples generally consist of complete programs of the type one is likely to encounter in daily use of the language, with an emphasis on system programming. Its authors wrote,


We have tried to retain the brevity of the first edition. C is not a big language, and it is not well served by a big book. We have improved the exposition of critical features, such as pointers, that are central to C programming. We have refined the original examples, and have added new examples in several chapters. For instance, the treatment of complicated declarations is augmented by programs that convert declarations into words and vice versa. As before, all examples have been tested directly from the text, which is in machine-readable form.


The book introduced the "Hello, World!" program, which prints only the text "hello, world" as an illustration of a minimal working C program. Since then, many texts have followed that convention for introducing a programming language.


Before the advent of ANSI C, the first edition of the text served as the de facto standard of the language for writers of C compilers. With the standardization of ANSI C, the authors more consciously wrote the second edition for programmers rather than compiler writers, writing,


The influence of The C Programming Language on programmers, a generation of whom first worked with C in universities and industry, has led many to accept the authors' programming style and conventions as recommended practice, if not normative practice. For example, the coding and formatting style of the programs presented in both editions of the book is often referred to as "K&R style" or the "One True Brace Style" and became the coding style used by convention in the source code for the Unix and Linux kernels.


Like other programming style guides, the issues covered span not only aesthetic issues offormatting, but other types of conventions or coding standards as well. However, this documentfocuses primarily on the hard-and-fast rules that we follow universally, andavoids giving advice that isn't clearly enforceable (whether by human or tool).


Example code in this document is non-normative. That is, while the examplesare in Google Style, they may not illustrate the only stylish way to represent thecode. Optional formatting choices made in examples should not be enforced as rules.


Aside from the line terminator sequence, the ASCII horizontal spacecharacter (0x20) is the only whitespace character that appearsanywhere in a source file. This implies that:


The order you choose for the members and initializers of your class can have a great effect onlearnability. However, there's no single correct recipe for how to do it; different classes mayorder their contents in different ways.


What is important is that each class uses some logical order, which itsmaintainer could explain if asked. For example, new methods are not just habitually added to the endof the class, as that would yield "chronological by date added" ordering, which is not a logicalordering.


Methods of a class that share the same name appear in a single contiguous group with no othermembers in between. The same applies to multiple constructors (which always have the same name).This rule applies even when modifiers such as static orprivate differ between the methods.


Exception: In places where these rules allow a single statement ending with a semicolon(;), a block of statements can appear, and the openingbrace of this block is preceded by a line break. Blocks like these are typically introduced tolimit the scope of local variables, for example inside switch statements.


Each time a new block or block-like construct is opened, the indent increases by twospaces. When the block ends, the indent returns to the previous indent level. The indent levelapplies to both code and comments throughout the block. (See the example in Section 4.1.2,Nonempty blocks: K & R Style.)


Java code has a column limit of 100 characters. A "character" means any Unicode code point.Except as noted below, any line that would exceed this limit must be line-wrapped, as explained inSection 4.5, Line-wrapping.


When there are multiple continuation lines, indentation may be varied beyond +4 asdesired. In general, two continuation lines use the same indentation level if and only if theybegin with syntactically parallel elements.


A single blank line may also appear anywhere it improves readability, for example betweenstatements to organize the code into logical subsections. A blank line before the first member orinitializer, or after the last member or initializer of the class, is neither encouraged nordiscouraged.


Optional grouping parentheses are omitted only when author and reviewer agree that there is noreasonable chance the code will be misinterpreted without them, nor would they have made the codeeasier to read. It is not reasonable to assume that every reader has the entire Javaoperator precedence table memorized.


Local variables are not habitually declared at the start of their containingblock or block-like construct. Instead, local variables are declared close to the point they arefirst used (within reason), to minimize their scope. Local variable declarations typically haveinitializers, or are initialized immediately after declaration.


After a switch label, there is a line break, and the indentation level is increased +2, exactlyas if a block were being opened. The following switch label returns to the previous indentationlevel, as if a block had been closed.


Within a switch block, each statement group either terminates abruptly (with abreak,continue,return or thrown exception), or is marked with a commentto indicate that execution will or might continue into the next statement group. Anycomment that communicates the idea of fall-through is sufficient (typically// fall through). This special comment is not required inthe last statement group of the switch block. Example:


Exception: A switch statement for an enum type may omitthe default statement group, if it includesexplicit cases covering all possible values of that type. This enables IDEs or other staticanalysis tools to issue a warning if any cases were missed.


Annotations applying to a class appear immediately after thedocumentation block, and each annotation is listed on a line of its own (that is, one annotationper line). These line breaks do not constitute line-wrapping (Section4.5, Line-wrapping), so the indentation level is notincreased. Example:


Package names use only lowercase letters and digits (no underscores). Consecutive words aresimply concatenated together. For example, com.example.deepspace, notcom.example.deepSpace orcom.example.deep_space.


Class names are typically nouns or noun phrases. For example,Character orImmutableList. Interface names may also be nouns ornoun phrases (for example, List), but may sometimes beadjectives or adjective phrases instead (for example,Readable).


Underscores may appear in JUnit test method names to separate logical components of thename, with each component written in lowerCamelCase, forexample transferMoney_deductsFromSource. There is no OneCorrect Way to name test methods.


Constants are static final fields whose contents are deeply immutable and whose methods have nodetectable side effects. Examples include primitives, strings, immutable value classes, and anythingset to null. If any of the instance's observable state can change, it is not aconstant. Merely intending to never mutate the object is not enough. Examples:

3a8082e126
Reply all
Reply to author
Forward
0 new messages