Learning programming is by no means a simple matter, especially for beginners with zero foundation. It takes four years for computer majors in universities to train a person from scratch, including a bunch of basic courses such as calculus, linear algebra, probability theory, discrete mathematics, combinatorial mathematics, automata, compilation principle, operating system, computer composition principle, and a bunch of professional courses such as C/C++, Java, database, network engineering, software engineering and computer graphics. Finally, cultivate a student who can find a job. Unfortunately, many schools have not done this last item well. According to our survey, many students who come to Asia for Embedding training have almost no foundation. I don't know why. In sharp contrast, given only four months, we also need to start from scratch, and finally cultivate a student who can find a job and ensure that he can find a good job. This is the characteristic of vocational education.
Why did I say "give us four months"? We want to teach for four years, but the length of class hours is not up to us, which is determined by market rules. It takes four months to complete a four-year task. How can we accomplish such an almost impossible task? The answer given by some vocational education is "pragmatism", with the slogan of "Learn if it is useful, don't learn if it is useless", and wantonly belittle the outdated and useless basic courses taught in universities, and only teach practical skills. This kind of hype is very bad. I think every course taught in the university is very useful. Basic knowledge will never be out of date at any time, but fashionable "practical technology" may soon be out of date.
How can a four-year task be completed in four months? Our answer is "optimization". The biggest drawback of the current university curriculum system is that it does not consider optimization at all. Anyone who has been there will think that freshmen and sophomores have learned a lot of math classes, but they don't know why or why. If you don't even know what learning is for, how can you be interested in learning well? When I was a junior and a senior, I used my previous knowledge to find out how useful the mathematics I had learned before was. However, I forgot all about it long ago and returned it to the teacher after the exam. When I went back to study, I found a lot of things that I didn't understand at all before, but now I really understand them, so didn't I waste the first two years? Another shortcoming of the university curriculum system is the lack of flexibility. Each course must take up one semester and be taught by a teacher. There is no communication and cohesion between teachers in different courses. In fact, these courses are interdependent, and forced disassembly is not in line with the laws of human cognition. For example, when I first went to college, I was forced to learn C language in the first half of my freshman year. In fact, C language is a very difficult programming language. It is impossible to learn without understanding the compilation principle, operating system and computer architecture. That half semester was naturally wasted. At that time, almost all computer-related majors in schools were like this. Freshmen have just come to learn C language, and some schools are even crazier. When I came to learn C++, most students thought they knew C language, but in fact they all had a little knowledge. When they actually write code, they often fall into a Bug, but they have no chance to learn C language systematically. Because in the school's view, C language was "finished" for you as early as freshman year, just like a meal, no matter whether you are full or not, you won't be allowed to eat any more. Obviously, if we want to optimize these courses seriously, there is indeed a lot of water to squeeze.
What are the characteristics of this book?
This book is not about C language in isolation, but about compilation principle, operating system and computer architecture. In other words, the content of this book is only based on C language, and it really talks about the principles of computers and programs.
Emphasize the basic concepts and principles, and attach importance to the dependence between concepts in the arrangement order. Every time a new concept is introduced, it only depends on the concept mentioned in the previous chapter, and it will never depend on the concept mentioned in the next chapter. In some places, in order to complete the narrative, they will also quote what will be said later, such as "let's explain it in detail in chapter 20" Any such reference is not a necessary dependence. Just pretend it doesn't exist and keep learning.
Try to ensure that every knowledge point is used before introduction. It is not in line with the cognitive law to introduce a knowledge point prematurely and not use it after the lecture, and readers will soon forget it.
What kind of readers is this book for?
This is a book to learn programming from scratch. Readers are not required to have any programming experience, but they should at least have the following qualities:
Familiar with the basic operation of Linux system. If you don't have this, please refer to other textbooks to learn relevant knowledge first, and then learn this book when you are proficient. Brother Bird's Linux private kitchen is said to be a good book on Linux system management and application. But you don't need to know a lot of system management techniques to learn this book, as long as you can use basic commands and install the system and software package yourself.
Have the mathematics level of graduating from high school. This book will use high school mathematics knowledge. In fact, if you don't have the math level of graduating from high school, you don't have to consider being a programmer. But it doesn't mean that just graduating from high school is enough to be a programmer. Let's just say that reading this book should be no problem. Mathematics is the most important accomplishment of programmers, and computer science is actually a branch of mathematics. If your math is poor, you need to make up for it in the future.
Have the English level of high school graduation. For the same reason.
I am deeply interested in the principles and essence of computers, not studying for employment or high salary, but really interested in learning everything clearly.
Be diligent in thinking. This book tries its best to clarify the dependence between concepts and strive for one-stop learning. Readers don't need to read other books to find a definition of a concept, nor do they need to rummage through this book to find a concept. They just need to learn from front to back in order. However, one-stop learning is not equal to fool learning. Some chapters are difficult and need readers to think actively to understand. This book can save you time, but it can't think for you. Don't expect to learn by skimming like reading a novel.
Why learn this book instead of K &;; rare
C programming language (hereinafter referred to as [k&; R]) is recognized as one of the most classic C language courses in the world, there is no doubt about it. Before the introduction of the C standard, K&; The first edition of R is the de facto C standard. After the release of the C89 standard, K & ampr had a second edition, but it hasn't been updated since then, so it can't reflect the development of C language after C89 and the latest C99 standard. This book has made many supplements in this respect. This book is not so much about C language as about the principles of computers and operating systems, while K &;; R speaks C language only to speak C language, with different emphases and different content arrangements. K &; ampr is well written, with concise code and language, but unfortunately, only those who know C will appreciate it. R is not suitable for beginners, especially students with zero foundation.
What is this book, not what?
This book consists of two parts:
Introduction to c language. This paper introduces the basic C grammar to help readers who have no programming experience understand what a program is, how to write a program, cultivate programmers' thinking habits and find the feeling of programming. The first half is adapted from How to Think Like a Computer Scientist: Learning with C++ (hereinafter referred to as [ThinkCpp]).
The essence of c language. Combining the principles of computer and operating system, this paper explains how to compile, link and run C programs, and comprehensively introduces the grammar of C language. The chapter of bit operation is adapted from teacher Lin's lecture notes; Linked list and binary tree chapters are adapted from teacher Zhu's lecture notes; The chapter of Assembly Language is adapted from Programming from scratch: Introduction to Programming with Linux Assembly Language (hereinafter referred to as [GroundUp]). In the last chapter of this book, it is mentioned that there are two ways to learn programming, one is "bottom-up" and the other is "top-down". These two ways have their own advantages and disadvantages, and we need to combine them. So my idea of writing this book is: the first part is top-down; ; The second part is bottom-up; ; The third part can be regarded as filling the gap in the middle, and all three parts are centered around C language.
This book is positioned at the entry level. Although it has a lot of content, it is not an encyclopedia. Apart from the basic knowledge of C language, it is not in-depth. Many references are listed in the book, which is the starting point for readers to further study. [K & amp; R] Chapter 1 is a whirlwind tour. I briefly summarize the contents of the book and then explain them one by one. This book can also be regarded as a whirlwind tour of the curriculum system of computer specialty. After reading this book, it should be easy for readers to learn those reference materials with a holistic view.
Why do you want to learn C language on Linux platform?
Isn't it good to learn C language with Windows?
You really can't learn C language with Windows. C language is a low-level programming language. To write a good C program, you must be very clear about the working principle of the operating system, because the operating system is also written in C language, and you can directly use the interface provided by the operating system when writing applications in C language. Since you chose this book, you must know that Linux is an open source operating system. You can find the answer to any question from the source code and documentation. Even if you can't read the source code and find the document, it's easy to find an expert to teach you. In various mailing lists, newsgroups and forums, there is always a lack of helpful experts. Windows is a closed operating system, and no one can see its source code except Microsoft employees, so we can only guess its working principle through documents. To make matters worse, Microsoft has always liked to hide useful functions for its own use, rather than making them public as documents. The first part of this book can be studied on Linux or Windows platform, but the second and third parts introduce many principles of Linux operating system to help readers understand C language more deeply, so the latter two parts can only be studied on Linux platform.
Development tools on Windows platform are usually associated with various integrated development environments (IDE), such as Visual Studio and Eclipse. It is really convenient to use IDE, but IDE is definitely not a good thing for beginners. Microsoft likes to promote the concept of fool programming, telling you that you can compile a program by dragging several controls with the mouse and then clicking a button, but which really useful program is dragged out like this? Many people who learn programming from Windows platform have been programming for several years, but all they know is that when they finish programming, they just need to click a button, drag several source files into a project and compile them together. If there are more complicated requirements, they will be dumbfounded, because they only have the concepts of buttons and menus in their minds, without the concepts of compilers, linkers and Makefile, or even command lines. However, these are the basic concepts that beginners should establish in programming. On the other hand, compilers and connectors are closely related to the grammar of C language. Without understanding the working principles of compilers and linkers, it is impossible to really master the grammar of C language. Therefore, IDE does not help you learn, but hinders you from learning. It turns out that learning C programming well only requires learning grammar and compiling commands. Now, with IDE, besides learning grammar and compiling commands, you have to figure out how compiling commands and IDE are integrated, so that you can understand mathematics. IDE makes the already complicated learning task more complicated. The usage habit of Linux users has always been to type commands, supplemented by mouse operations. From the first day of learning programming, you should type commands to compile programs. When you make these basic concepts clear, which IDE do you think is convenient for you to use, but then you may prefer vi or emacs to IDE.
Style description
The font like the fast brown fox jumping over the lazy dog is the code font of this book. The name of this font is Dejavu Sans Mono. Why should I advocate this font? First, it is a font of equal width, so it is suitable for code fonts. Secondly, its 1 is clearly distinguished from L, 0 and O (I find that beginners can easily copy these characters wrong in teaching), so it is more suitable for code fonts than Courier New. Third, it is the default font of my Linux graphics terminal. Using this font typesetting can make reading and watching the screen feel very consistent. I hope readers can feel Dejavu when reading this book.
Bordered like this is the code:
#! /bin/sh
VAR= 1
VAR=$(($VAR+ 1))
echo $VAR
Without borders is the terminal display, including input commands and program running results, such as:
$ VAR= 1
$ VAR=$(($VAR+ 1))
$ echo $VAR
2
In this book, the Shell prompt is represented by $.
Bold words indicate emphasis.
When a noun is defined, its English name is given, such as integrated development environment (IDE). Through the index at the back of the book, you can find out where these definitions first appeared in the book.
Express gratitude/gratitude
The writing of this book has been fully supported by Beijing Asian Games Education Research Center, with special thanks to teachers Li Ming and He Jiasheng. Without the support of the company, I can't have the time and conditions to write this book, and I can't have the opportunity to publish this book online.
Then I would like to thank the students and teachers in Asia before embedding education. I often get useful inspiration in the process of teaching and discussion, which makes this book more perfect. In the process of writing this book, many readers put forward valuable suggestions for this book. Many suggestions are made by enthusiastic netizens through online comments. Some netizens only know ID or email address. I want to thank them here.
Thanks to the teachers who helped me: Li Ming, He Jiasheng, Di, Lang Tieshan, Zhu, Liao Wenjiang, Zhang, Xing, He Xiaolong, Lin, Wei, Guo Tongbin, Wang Bo and Wang Keer.
Thanks to enthusiastic netizens: ddd, Wu Yulei, commapopo, Wei Tian, Tian Yu, Daidai, Deng Nan, Dupont, Zoom. Quiet, teacher, Zhang Yu, Chen, Waterloo, Dong Junbo, Luo Bixiang, Been26544. Juicerococo, Xu Bin, cyy, Linux_Xfce, Feng Haiyun, Hou, churchmice, codycody23, syfeagle, Wang Gongfu, Liu mick, Laciq,,, Chen Yangxi, love_wc3, Yao Lei, Sesame, wadenx, Shen Zhen, sunbingfly, etc.
I also thank Mr. Zhou Jun and Mr. Li Bing of Bowen Viewpoint Information Co., Ltd. of Electronic Industry Press for their strong support and Mr. Li Jiang for his rigorous and meticulous work.
In the process of writing, I encountered many difficulties: busy work, confusion about the future, great pressure in life, insecurity, personal trifles and so on. However, there are so many enthusiastic classmates, teachers, friends and netizens waiting to read the online update of my book and give me advice. I hope I can improve this book, which is my greatest motivation to continue writing. Thank you!
Because the author's level is very limited, he has not written C compiler and C standard library, so omissions are inevitable. If there are any mistakes, readers are welcome to criticize and correct them. Writing a book is a serious matter. Everyone can see the mistakes in the book, and black and white are also involved. Many students I have taught have studied C language in college, even passed Grade 2, but the programs are written in a mess, and even the most basic concepts are wrong. The mistakes in the C language textbooks I studied before are deeply rooted in their minds. Even though I corrected them many times, they still only remembered the wrong concepts I had learned before. Such students with a foundation are not as good as those without any foundation to teach. I was very afraid that what I taught others was wrong, so I studied C99 carefully before I dared to write a book. This book covers a wide range of topics, and my best efforts are not enough to ensure that all the contents in the book are correct. I have to rely on the strength of the community to improve this book, so that I can really be responsible to the readers, so I choose to open this book. .
I hope this book can be your first partner on the way to study.
Song Jinshan
July 22(nd), 2009