C语言编程风格--Points on C 学习(2)

本文分享了一位程序员在大学时期从C语言转向Java编程过程中积累的编码规范经验,并将其应用于C语言编程实践,包括代码分段、缩进、注释等方面。

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

起初在大学刚接触C语言的时候,对于编程风格并不是太在意,但是之后因为做毕业设计,接触了很多Java代码,同时也接触了很多关于编码规范的问题。在大学期间接触的C语言很小,我虽然没有写过大型的C语言程序,但是我写过很大的Java程序,总代码量在10000行左右,所以我深知规范的重要。因为我觉得语言都是想通的,所以我结合我在Java编程方面的编码规范和Points on C 提到的规范,说明一下我在C语言将要采取的规范问题。

1.关于空行,空行用于分割不同的逻辑代码段,他们按照功能分段。这样通过空行,读者就能看到逻辑代码的结束。对于这一点我觉得很有必要,但是对于我这样的初学者,思路可能不是很清晰,所以可能在分段上会有问题,但是不可否认,这在实际开发很有好处,因为这样便于维护人员读懂程序。

2.if和相关语句的括号是这些语句的一部分,而不是他们所测试的表达式的一部分。所以我们在括号和表达式之间留下一个空格,使表达式更突出一些。函数原型也是如此。

比如:if( (ch == '\n') ){}(if表达式);我在实际编程的时候是采取这种方式的,这个可以在我的Points on C 学习(1)中看到,是很有好处的。但是从我自身而言,刚开始这种方式确实很累人,这少一个空格,那少一个空格,但是坚持下去,相信我会有改变的。

3.在绝大多数的操作符使用当中,中间都是有空格,这样可使表达式的可读性更佳。有时,在复杂的表达式中,我会省略空格,这有助于显示子表达式的分组。你们可以从我上一个例子中看到,我写到 ch == '\n',其中两边就是用空格表示的。这里我特别提一点,因为这是我编程遇到的,有时我会写到:a ! = 3;我在感叹号和等号之间加了一个空格,这就会编译出错,因为!=写在一起是一个操作符,所以希望没有注意的读者留意一下。

4.嵌套语句要缩进,和Java一样,使用Tab按键。

5.绝大多数注释都是成块出现的,这样他们从视觉上在代码中很突出,读者可以很容易的找到或者跳过他们。这点在Java语言中很突出,在使用MyEclipse编程的时候,在新建一个class的时候,在导入的包的下面都会有注释/***/,这是对整个类的描述。另外如果你写完一个方法,然后在方法上一行,打上"/**"然后回车,会出现注释,你就可以对这个方法描述,如果有参数,注释里面会给你写好参数的名字,然后你对他进行描述。以前我在注释方面有两个问题,一个是很少注释,另一个就是注释分散。下面我就要采取成块注释的方式来使我的程序更为可读。另外有一点特别提醒大家,这也是我以前从没听到的,但是在实际开发会用到的,那就是注释不要和代码写在同一行,这样方便对注释的统计(现在已经有统计的工具了)。

6.在函数的定义当中,返回类型出现在独立的一行当中,而函数的名字则在下一行的起始处。这样在寻找函数的定义时,你可以在一行的开始处找到函数的名字。比如:

int 

main(void){

return EXIT_SUCCESS;

}

这个初步看起来没什么用处,但是我希望大家采取一下,因为我始终相信,计算机市场的良好风气应该是程序开发者尽力去满足用户的需求和利于维护而不是自己怎么方便怎么开发,而用户应当去迁就软件程序,这样才是正能量。

最后,我想说,C语言是一种自由风格的语言,你可以采取不同的方式,但是如果说有一种经过长期的经验得来的“规范”,遵守一下,未尝不可!

Pointers On C brings the power of pointers to your C programs. Designed for professionals and advanced students, Pointers on C provides a comprehensive resource for those needing in-depth coverage of the C programming language. An extensive explanation of pointer basics and a thorough exploration of their advanced features allows programmers to incorporate the power of pointers into their C programs. Complete coverage, detailed explanations of C programming idioms, and thorough discussion of advanced topics makes Pointers on C a valuable tutorial and reference for students and professionals alike. Features and Benefits Provides complete background information needed for a thorough understanding of C. Covers pointers thoroughly, including syntax, techniques for their effective use and common programming idioms in which they appear. Compares different methods for implementing common abstract data structures. Offers an easy, conversant writing style to clearly explain difficult topics, and contains numerous illustrations and diagrams to help visualize complex concepts. Includes Programming Tips, discussing efficiency, portability, and software engineering issues, and warns of common pitfalls using Caution! Sections. Describes every function on the standard C library. For those who need an up-to-date ANSI overview of the C programming language, this book would be an excellent introduction. Pointers are usually a stumbling block for those programming C initially, but the author does an excellent job of detailing the use of pointers in this book. The use of pointers dominates the entire book, and after studying it, readers will have a thorough, practical knowledge of how to take advantage of the performance power of C language, due mostly to its use of pointers. For those programming in a commercial/business environment, where coding practices are strictly enforced, this book would be a good desk reference, as the author includes discussion of sound programming practices throughout the book. The book would also serve well those involved in teaching C in the classroom, as it contains many exercises, ranging from very easy to highly advanced. And for those readers frequently facing legacy code in C, such as scientific programmers, the author cites the differences between the older "Kernighan-Ritchie" C, and the more modern ANSI C, the latter being used in the book. These differences are indicated in the margin of the book, and are of an enormous help for those who must take older code and get it to run on more up-to-date compilers. The author also endeavors to organize the C code for those who are going on to study C++ and the accompanying object-oriented approach to programming. In addition, he emphasizes how to write C code so as to make it more portable. For those writing commercial applications in C that must be used on different platforms, this is a very important issue of course. Particularly well-written is the author's discussion on the storage class of a variable, noting, for those such as I who are pre-disposed to using recursion, that the formal parameters to a function cannot be static if recursion is to be supported. The book is full of examples such as this that give readers insight on the workings of C that fit their particular programming style. He does discuss `goto' statements in relation to function scope and in C statement structures, but, thankfully, recommends such statements never be used. He gives an interesting counterexample to those who say that goto statements must be used to break out of nested loops. Also, the author discusses the difference between L- and R-values, and this is not usually included in beginning books on C. Dynamic memory allocation has been at times a somewhat painful aspect of programming in C, but the author shows how to do straightforwardly in the book. Having a book like this that is predominantly about pointers is quite a blessing for those who are inexperienced with them or for more experienced programmers who are still uncomfortable with their use. It is not uncommon these days to have to write programs in one's professional work that involve triple pointers or even quadruple pointers. In addition, for embedded systems programming, the use of pointer arithmetic is almost mandatory. This also is true for writing applications in cryptography using C. The author does pay careful attention to pointer arithmetic in the book. The performance pay-off for using pointers is undeniable, and so a thorough knowledge of their use and pit-falls is of upmost importance for those C programmers who are involved in writing performance-sensitive applications. The author discusses in detail what can happen when pointers are misused and gives many examples of what to avoid and good hints for the proper use of pointers. He recommends against the use of the `null' pointer in array searching, and recommends a strategy for circumventing them. Some very helpful diagrams are given for explaining pointer expressions. In addition, the author gives helpful hints on when to use pointers and not subscripts when manipulating arrays in C. The performance issues involved in this are extremely important in scientific programming using C. The author gives a very interesting example of the differences in performance using pointers involving a program to copy the contents of one array into another. Arrays of pointers, useful in data mining applications, are also given ample treatment in this book, and the author addresses the issue of when to use a matrix instead of an array of pointers. The author also gives an effective presentation of functions in C, particularly the construction of recursive functions, and he employs some useful diagrams to illustrate how the variables in a recursive function call change on the stack. The performance hit experienced by using recursion versus iterative loops is discussed in a standard way via the Fibonacci series. Those readers raised in the functional programming paradigm will want to pay notice these performance issues when using C to do recursion. Along the same lines, the author shows how to implement functions with variable argument lists in C. This is another topic that is frequently passed over in beginning books on C. The author's treatment of data structures in C is also very nicely done, and he includes again a topic not usually treated in beginning books on C, namely the concept of a self-referential data structure. These are very important in applications in artificial intelligence, and the author shows how to implement them in C using a data structure that points to itself. This leads to a discussion of incomplete declarations. Very helpful diagrams are used again to discuss how to access members of data structures and how to point to data structures. Bit fields, so often used in embedded system applications, are also given a detailed treatment.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值