Learning C language 7 steps, contrast, see you in which stage?
Refreshing 2021-02-23 02:19:40
C It's a compiled language . If you've used a compiled language before ( Such as ,Pascal or FORTRAN), You'll be familiar with C A few basic steps of the program . however , If you used to use interpretive language ( Such as ,BASIC) Or graphical interface oriented language ( Such as ,Visual Basic), Or not even in any programming language , It's necessary to learn how to compile . Don't worry , It's not complicated . First , In order to let readers have a general understanding of programming , We write the C The process of a program is broken down into 7 A step ( See the picture 1.3). Be careful , This is the ideal state . In actual use , Especially in larger projects , There may be some repetitive work to do , Adjust or improve the previous step according to the situation of the next step .

 Study C Linguistic 7 A step , A comparison , It depends on where you are ?

chart 1.3 programming 7 A step

1 The first 1 Step : Define the objectives of the program

Before you start writing the program , Have a clear mind . If you want the program to do something, you should first make clear what you want to do , Think about what information your program needs , What calculations and controls are to be carried out , And what information the program should report . In this step , There is no specific computer language involved , The problem should be described in general terms .

2 The first 2 Step : Design procedure

After having a conceptual understanding of what the program should accomplish , We should consider how to use the program to complete it . for example , What the user interface should look like ? How to organize the program ? Who are the target users ? How long is it going to take to complete this program ?

besides , And we have to decide in the process ( It could be an auxiliary file ) How to represent data in , And how to process the data . Study C The beginning of language , The problems are very simple , There's no choice . however , As the situation to be dealt with becomes more and more complex , There are more and more aspects that need decision-making and consideration . Usually , Choosing an appropriate way to represent information makes it easier to design programs and process data .

Again , The problem should be described in general terms , Instead of using specific code . however , Some of your decisions may depend on the nature of the language . for example , In terms of data representation ,C I'm a better programmer than Pascal There are more options for programmers in the world .

3 The first 3 Step : Write code

After designing the program , You can write code to implement it . in other words , Translate your program into C Language . Here's what you really need to use C Language place . You can write ideas on paper , But in the end, you have to put the code into the computer . The mechanism of this process depends on the programming environment , We'll talk about some common environments later . generally speaking , Use a text editor to create a source code file . The content of this document is your translation C The language code . Program listing 1.1 It's a C Examples of source code .

Program listing 1.1 C Source code example
#include <stdio.h>int main(void){int dogs;printf("How many dogs do you have?\n");scanf("%d", &dogs);printf("So you have %d dog(s)!\n", dogs);return 0;}

In this step , You should add text notes to your own programs . The easiest way is to use C The annotation tool in the source code to add the interpretation of the code .

4 The first 4 Step : compile

The next step is to compile the source code . Once again, I want to remind readers to pay attention to , The details of compilation depend on the programming environment , We will introduce some common programming environments in a moment . Now? , Let's first explain what happened to compilation from a conceptual perspective .

As mentioned earlier , A compiler is a program that converts source code into executable code . Executable code is the code expressed in the machine language of a computer . This language consists of instructions represented by a numeric code . As mentioned earlier , Different computers use different machine language schemes .C The compiler is responsible for putting C Code is translated into a specific machine language . Besides ,C The compiler also integrates the source code with C library ( The library contains a large number of standard functions for users to use , Such as printf() and scanf()) The code is merged into the final program ( To be more precise , It should be a program called linker that links library functions , But in most systems , The compiler runs the linker ). As a result, , Generate an executable file that users can run , It contains code that a computer can understand .

The compiler also checks C Whether the language program is effective . If C The compiler found an error , No executable file will be generated and an error will be reported . Understanding errors or warnings reported by a particular compiler is another skill for programmers .

5 The first 5 Step : Run the program

Traditionally , An executable is a program that can run . In a common environment ( Include Windows Command prompt mode 、UNIX Terminal mode and Linux Terminal mode ) To run the program in, enter the file name of the executable file , Other environments may have to run commands ( Such as , stay VAX Medium VMS[2]) Or some other mechanism . for example , stay Windows and Macintosh Provides an integrated development environment (IDE) in , Users can go to IDE To edit and execute by selecting an option in the menu or pressing a special key C Program . The final program can be run directly in the operating system by clicking or double clicking the file name or icon .

6 The first 6 Step : Testing and debugging procedures

It's a good sign that the program works , But sometimes there may be running errors . Next , You should check that the program is running in the way you designed it . You will find some mistakes in your program , Computer jargon is called bug. The process of finding and fixing program errors is called debugging . Mistakes are inevitable in the process of learning , So is learning programming . therefore , When you apply what you've learned to programming , You'd better be prepared for your mistakes . As you get more sophisticated , Errors in the programs you write will become less and less noticeable .

There are many opportunities to make mistakes in the future . You may make basic design mistakes , It may have been a mistake to realize a good idea , It's possible to ignore the input check and cause the program to crash , You may misplace the parentheses , It may be misused C Language or typos , wait . Make a list of your future mistakes , This list of errors should be long .

7 The first 7 Step : Maintain and modify code

After creating the program , You find something wrong with the program , Or want to extend the purpose of the program , At this time, we need to modify the program . for example , User input to Zz There was an error in the program when the name at the beginning 、 You come up with a better solution 、 Want to add a better new feature , Or modify the program to run in different computer systems , wait . If the program is written with clear notes and reasonable design scheme , These things are very simple .

8 explain

Programming is not a linear process like description . Sometimes , To go back and forth between different steps . for example , When writing the code, I found that the previous design was impractical , Or come up with a better solution , Or wait for the program to run , Want to change the original design idea . Make text notes on the program to provide convenience for future modification .

Many beginners often ignore the second 1 And the first step 2 Step ( Define program goals and design programs ), Go straight to 3 Step ( Write code ). At the beginning of study , The program is very simple , You can conceive the whole process in your mind . Even if it's wrong , And it's easy to find . however , As the program becomes larger and larger 、 More and more complex , You can't use your head without your hands , And the hidden errors in the program are becoming more and more difficult to find . Final , Those who skip the first two steps tend to waste more time , Because the programs they write are ugly 、 Lack of organization 、 It's hard to understand . The larger the program, the more complex it is , The more work it takes to define and design the program in advance .

Sharpening a knife never misses a woodcutter , We should develop a good habit of planning before writing code , Use paper and notes to record the goals and design framework of the program . In this way, it will be easier to write code 、 Clarity of organization .