Errors and programming-Discard errors and bad habits in C Programming

Source: Internet
Author: User

Pingwu C-Abandon errors and bad habits in C Programming--Preface

  • What kind of book is this?

This is a book about common errors in C programming. The book analyzes and discusses in detail some common mistakes and practices in learning or using C language. The objective is to help readers better understand and use the C language.

  • Why write such a book?

Errors are inseparable from programming. Errors cannot be avoided as long as you write a program. This is an iron law. No matter whether you are a novice or experienced veteran, no exception exists. In a sense, the software industry has three main tasks: making errors, correcting errors, and hiding errors. Therefore, without in-depth understanding and understanding of errors, you cannot really understand programming.

Confucius said: "unknown, why do you know ?" Program Design is also the truth, do not understand errors can not really understand the main points of the programming language and the true meaning of programming. Practice shows that people often learn much more from mistakes than they do from correctness. This is what people often say: "eat a grain, grow a wisdom ".

In fact, the growth of every programmer is a history of error correction that keeps recognizing and correcting errors. Every good programmer stood out from countless errors and had to fight against errors persistently. The shining thoughts have never been honed by mistakes.

From this point of view, the discussion of errors in program design is more helpful to the improvement of the program designer level than those with a serious description. The purpose of this book is here.

However, despite the fact that many C language books can be used in the market, it is a pity that few books specifically discuss the errors in C Programming, except for the c traps and defects written by Koenig. However, I think the author Mr. Koenig "I have not fully explained it. I have my opinion ".

In fact, Mr. Koenig does not understand China's national conditions. He does not know that there are two types of errors in programming. One is errors and the other is Chinese errors. In a word, he could not imagine how strange we were learning C Programming with quality textbooks and how backward we were. Therefore, his book only mentions General errors, but does not mention all kinds of errors with Chinese characteristics. This is a huge gap that needs to be filled (I don't know if this is not an academic gap, in a sense it is more like removing technical pollution ). Currently, no one has done this job. Since the sages of ancient times taught us how to "do nothing", now we have to leave this book alone.

  • How are errors made?

As we all know, the so-called programming is essentially a solution to describing a problem to a computer in a specific format language. Prior to this, learning this language was also an essential part. Errors may occur in any part of the entire process.

First, many errors may occur during language learning. These errors are not only caused by the learners themselves, but also by textbooks.

Many devout learners subconsciously think that there will be no mistakes in textbooks. They always kneel on textbooks to learn and review themselves when encountering errors. In a society where even baby milk powder may contain melamine, this idea is really too naive. There is no reason to expect your textbooks to be purer than baby milk.

Textbooks may also be wrong. Such "Water Source" pollution is the most serious pollution. In this case, it is impossible to find the cause of your own error. This kind of preemptible error is very difficult to correct, and its impact is often the most profound. For example, it is still unclear that many people become professional programmers.

A + = A-= A *

This expression is incorrect.

Not familiar with the language is another major reason for beginners to make mistakes. It usually shows that the code is either a defect or a little more. This kind of error is the most elementary one, which can be pointed out by the compiler, so it is easier to be corrected.

Learning a language is essentially to learn a new way of thinking. Introducing less rigorous natural language thinking habits into a program often leads to misunderstandings about the programming language. For example, if "X is not equal to 1 or 2 ",

X! = 1 | X! = 2

Even the rigorous mathematical language we are familiar with may cause errors in the program due to context changes. For example, many people have made the mistake of writing "=" ("equal to") in mathematics as "=" ("value assignment", indicating "equal" in mathematics. This error usually needs to be corrected through long-term Conscious Training.

What beginners often ignore is the bad programming habits. They only want to write the correct code, instead of writing the correct code correctly. This often causes many low-level mistakes to be committed again, such as repeated unpaired parentheses.

A lack of good programming habits is hard to write programs. Any worker knows that a proper process is required to ensure a good product quality. It is almost impossible to write the correct program without knowing how to write the program correctly.

Another major reason for beginners to ignore bad habits is that these bad programming habits sometimes do not show their negative effects in the short term, such as "int A, B, C; "This type of rotten variable name can no longer be rotten. In fact, good programming habits not only mean avoiding errors, but also mean making fewer mistakes or easily checking and correcting errors, these long-term benefits cannot be realized in a short period of time.

Good habits depend on self-conscious cultivation, but if textbooks do not tell you this, it is a textbook problem. Because it is impossible for beginners to understand the programming norms accumulated by their predecessors for decades by their own understanding in a short time. By the way, the Code styles of some textbooks are extremely abuse and misleading to beginners.

Familiarity with language elements alone is far from enough for programming, because every program's goal is to solve the problem. From the problem to the program, the problem should be raised, analyzed, solved, and finally implemented as code. In this process, errors may occur in every step.

Correctly proposing a problem is a prerequisite for solving the problem, and the wrong problem cannot be solved. For example, "output 1 ~" is required ~ The cube array composed of natural numbers of N * n is an incorrect problem because the level 2 cube does not exist. To solve such a problem, we can say that the problem is not solved first.

The ambiguous requirement of the question is also an error. For example, "three numbers A, B, and C are required to be output in order of size." What is the number of three numbers here, it is ambiguous, and the code is completely different under different circumstances. The requirements should be clear. Unclear requirements are the mistakes of the author, but the demand for unauthorized guess is a programmer's mistake.

It is harmful and harmful to write programs that solve wrong or not rigorous problems. This will make programmers lose the necessary professional rigor without knowing it, and their demands for errors will gradually become insensitive. You must be aware that in actual development, software errors caused by software requirements errors account for more than half of the total number of errors, and the requirements for errors are often unclear and easy to detect. Without the habit and ability to examine problems or make requirements reasonable, in real software development, it is like a blind man riding a horse, and it is impossible to fall into the trap.

Few beginners realize that the problem itself may be wrong. Many beginners often solve the problem of errors in November. This is a huge chronic threat to their logical thinking ability.

Even if the problem is raised correctly, it may be misunderstood by the program designer. Understanding the problem requirements incorrectly is also an important cause of the error.

Logical errors are the most likely to occur in the process of analyzing the problem, or the problem is not fully analyzed. This comprehensive logical vulnerability is often more difficult to find than a full logical error, because the program may "appear" correctly.

Even if there are no errors mentioned above, there is still a long way to go before writing good code. Writing code is the same as writing an article. You need to carefully design the layout, in terms of design.

The design includes not only algorithm design but also data structure design. Under the guidance of the one-sided view of "algorithm-the soul of the program", many people despise the design of the data structure and think about the so-called algorithm. In fact, algorithms and data structures are inseparable. The obscure bad algorithms are mostly due to spam data structure design.

Some people do not have the consciousness to write a program. When they come up, they begin to rush to write code, the code written is clumsy, rigid, logically disordered, repetitive, or inefficient. Such code is an ugly code that is unqualified even if the result can be output.

Is the program ugly? Yes, as an art, programs have their own aesthetic standards. From a micro perspective, this beauty fully reflects the beauty of the language and the conciseness of the style; from a macro perspective, it is a clear and clear hierarchy. This kind of standard is not purely aesthetic, but also has its function value-it is not easy to make mistakes, it is easy to correct errors. To achieve this goal, the practical process of program design is inseparable from the correct philosophical guidance. a core concept of this philosophy is the structured program design philosophy.

At this point, although there are still many types of errors not mentioned, it is not difficult to draw a conclusion that there are many program design errors. Always be cautious about errors.

  • How should we handle errors in programming?

First, do not be afraid of mistakes. Although there are many errors, you can correct them and avoid them as long as you understand them. The most terrible thing is ignorance of mistakes and an ostrich-like disregard. Understanding errors is a prerequisite for correct errors.

Secondly, the sooner you learn about errors, the better you know. The sooner you get to know, the faster you get to know.

Third, errors are also a fortune. Since accumulating experience from mistakes is the only way to improve the level of program design, learning from others' mistakes is a shortcut to improve the level of program design, because the so-called shortcuts only mean less detours or no detours.

  • Main content and features of this book

This book brings together a large number of errors and bad programming habits in C language and discusses them in detail one by one, these errors are mainly taken from C language textbooks with a large circulation and influence in China, some other non-textbook C language books, and some popular ideas on the Internet, including □□□□□( to save space, delete 286 characters here), and some are some of the characteristics that are hard to understand in C language and the errors caused by the inability of learners to have good programming habits. It should be said that the negative examples cited in this book are very representative.

This book is divided into two parts: the upper part of this book, "", discusses common errors and bad styles in code. The lower part of "Metaphysics" discusses the incorrect ideas and understandings held by many people on C language.

The sections in the book are basically independent from each other. Therefore, reading this book does not need to be gradual, but what pages can be read by turning.

  • Who and who should not read this book

For those who use Chinese textbooks to learn C language, this book should be a valuable reference for beginners and professional programmers. Because the content of this book is exactly what the textbook does not talk about or what the textbook has wrong.

If you want to deal with what level of examination, you don't need to waste money. After reading this book, you may find that there are many mistakes in the exams.

  • Read Guide

(Delete it here)

Although it took nearly three years to write this book, I found that this time was still very short. Therefore, although this is a wrong book, it may also inevitably have errors.

If you find any errors or errors while reading, contact the author and notify the author: pmerofc@126.com. Thank you!

This book will be posted on the author's blog: http://blog.chinaunix.net/uid/22996974.htmland http://www.cnblogs.com/pmer /.

  • Thank you

I am deeply grateful for the help and support of many people in the writing process of this book.

Special thanksStarwing83His profound understanding of programming languages and rich practical experience not only benefited the author, but also improved and enriched the book in many aspects. Chapter 28 of the book, chapter 10 of chapter 10, and chapter 1 of Chapter 1Starwing83Written by netizens.

Special thanksOwnwaterlooThanks to the consistent support and help of netizens, his accurate understanding of the Standards and profound insights into program design have unlocked many of my doubts and made the author feel confused on many issues. Chapter 19 "Question 19"OwnwaterlooWritten by netizens.

Special thanksPhantom GodDuring years of joint discussion, he enthusiastically and selflessly gave the author a lot of support, help, and inspiration. Many ideas in this book benefit fromPhantom GodHe also collected a lot of information for this book, made a lot of informative research work for this book, and corrected many mistakes of the author.

I am particularly grateful to many netizens for their moral support, spiritual encouragement, and technical help during their long-term discussions. They are:Mutant mouse, shan_ghost, Cai wanzhao, dog balloon, egg, March 27, tempname2, x5miao, madoldman, walleeee, chiyut, davelv, huangzhenfan, peatrow snow, yijiecun, wait_rabbit, mmmx, excellent program _, assiss, gccer, 8, supermegaboy, a.com, noword2k, hahahajerry007, ztz0223, ray001, FERA, unixlinuxsys, cokebol, success, fail, example, hellioncu, example, X2, lylesong, supersuper8, example, Kabie, pkkj, rosxini23, wolfkin, milujite, example, cnhbdu, yesbsd, kouu, example, water_wf, gz80, toniz, example, example, airjordanforce, example, yug1129, example, www1862, zxrjkl, peijue, example, x75yan, example, example, xxwpk007, Geel, letter 26, milk tea DSK, abc97601_17, rain_fish, amarant, financial edition, normal, grand508, star1983653, bill15, makeit, liexusong, pandaiam, txg531, Sichuan, yiku, bukake, wuxb45, Sichuan, cjaizss, greensnow, Beijing, example, patient learning, example, l_kernel, geruihai, erlangs, Baidu, cuxxxcu, ip200, Baidu, dglwx, jhinux, xwxfirst, timesu, srdgame, Baidu, liupingforarm, cosine, 192 redwolf, cobras, small_bee, gyarenas, jack4010, startn, rainysky, jimmyixy, litanhe, rubyish, demon-hunter, Taobao, bigxu, xfoucs, fashion farmers, fashion, hukb_cu, Taobao, sukora ux400, craneflyfly, autoasm, Time View, jokday, asuka2001, NAMIII, Jeung, btdm123, lilery, workshop, nketc, kanonind, ybh37, dahan16, digdeep126, keytounix, Guangming-Ambassador, GTV, soforthhe, djsxut, oilgeo, waiting for salvation, hbmhalley, china_ssl, solu, Baidu, Baidu, Warcraft _ lover, 0xc1988, inzahgi, Wulin Meng pig, Baidu, wgm001, and Royal Zhang millennium old wolf, musezh2, gooderfeng, hal9000, jack_jack, big stone, hichina Yiyi, ivony, Yuxi, nscboy, minvt, underuwings, artech, ini_always, xiaobin, xujif, Leon sharp, Liu Bo ping, linyilong, Haoran Zhengqi 89, backag, happycat1988, ygcao, RHS, zlinux, xiao_jin_yu, bubble Teng, songsharp, szwe, eeeyes, + V, linxr, Andreas, crown baibo juice, jiuyuan Mountain, huohe, luotong, programmer K, hachihe, rockyoung, czcz1024, Edgar Wang, magicdict, hoodlum1980 neil Chen), casual games, sunriseyuen, chasefornone, feilang, glshader, credo, wcut, houqidian, Parry, xiekun605746, elder brother. net, virus-beautycode, Zhuang jinfeng, I think I am Wind, mien ng, soap, greenhand2008, homer_simpson, zsea, Baidu, ctou45, shamo0303, gussing, eflay, Wang Xiaobing, l23cy, winter-CN, Lee. kevin, sunny Ryan, warehouse of Hell, Richard Zeng, pulihe, Han, cncolder, Chen Yuguo, presence or not, key Yao, codebumb, ☆kaizi, darklx, Caohe, hechongtian, and Jackie Lin lithium, taobaofen, leizisdu, zzuxiaolei, bitsky, Jeffrey Zhao, Zookeeper soul, Hainan. hu Yong, sm11e, I do not write code is lonely, lzyzizi, Xiao, Xiao, Mu + head, zy498420, complete, five stars, Simon-Zhu, beckfun, Tony Zhou, lxlylm, Chen zihan (vczh) zdd, xchat, frank_hust, floating rain, six mountain stars, magichu, tianfang, deep blue liusha, viperchaos, Ghost, Xiangxiang Yujie, riccc, Jeff Wong, wincss, reavics, weiwelcome0, Muse, lx458004975, Jing meiru, volcanol, passing by autumn, Xu shaoxia, Nanjing. wang qingpei, Nobel, Yan Yanmei...... This list may be missing, and we apologize to the missing netizens.

Special thanksChinaunix ForumI have accumulated the main materials of this book in the Forum's "stunned by others" post. The good technical atmosphere of this Forum makes the accumulation of materials in this book smooth.

Special thanksBlogIn this forum, part of the content or draft of the series "C detoxification" published by the author. Many netizens have received useful feedback on this forum.

This book has read a lot of online technical materials during the writing process, and has gained a lot of inspiration. I would like to express my gratitude to these authors.

A friend sorted out a document based on the post "stunned by others", which brought great convenience and help to the writing of this book. I would like to express my gratitude here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.