Programming in C language: Analysis of the hello world Program

Source: Internet
Author: User

 

Review and comment of the previous article:

 

1. I talked about learning to think about a new language. What do you think about?

 

2. I talked about the programming process in the previous article. What is the process?

 

3. Have you compiled the helloworld program yourself? Is one edit and compilation successful?

 

After answering the above content, proceed with the following content.

 

This article will provide an in-depth analysis of the Hello world Program

 

To learn a language, we must have a good learning method. This is the best way to learn or write the least statements, and to better understand the language. Remember the word "least. In reality, many programmers do not have this concept and can write thousands of programs, but they still cannot grasp the language. This is because they only see specific statements in their minds, specific functions, and "abstract statements and Abstract Functions", and cannot master the laws and principles of languages, you cannot master the language learning methods and skills. These are especially important for new users. Never learn from them.

 

I don't know what you can summarize in section a. c of the previous article. Someone will say, isn't it just a helloworld statement? Is the simplest program. I agree that this is a simple program, and everyone's focus is on displaying "hello world !" On that statement. However, this simple program contains a lot of important content. Each line is a treasure, and there are headers, which are indispensable.

 

What should we know through this program?

 

1. What is a complete program.

 

# Include "stdio. h"

 

Main ()

 

{

 

Fprintf (stdout, "hello world! \ N ");

 

Exit (0 );

 

}

 

This is the simplest complete program, with only six lines. However, it tells us that a C language consists of two parts: definition and function, at least one primary function. The first line # include "stdio. h" can be seen as the definition part. From the second row to the sixth row, we regard it as the function part. What is the purpose of this composition? We can judge the program we will compile later. If our program is not defined, or the main function is not complete. Some people will say that I don't need to be defined. I can say that, it is just an exception. 99.99% of the applications in C language must be defined. For new users, we will not learn any exceptions first, but we will learn something normal first. After learning this structure, when we write C language, we will be thinking about two questions: what is our definition? What is our function?

 

Remember these two parts first. In the future, we will conduct in-depth analysis on these two parts, for example, how to divide the definition part and how to divide the function part.

 

2. main

 

The main function is also an important part of a language. Some people may ask why the main function is required? Can't I write an aaa function? If you write an executable program, it may not work. To run this program, you must have a unified and unique entry, and the C language entry is main. Isn't it? If there is no main function, you compile function a and function B in the program. How does the program know which one to execute first? Is that the one that runs before? The algorithm is executed first, but there are exceptions (which will be seen in the future ). Therefore, it is stipulated that main is the entry of the program.

 

You told him about main, and he will immediately say yes. Do you really know? To grasp the principle of main, we will consciously find the corresponding entry function when learning other languages. For example, the load and page_Load in the C # form in VFP are the initial entry of a program.

 

Remember that as long as there are multiple functions, there must be a primary function. We can find the primary function and perform the initial processing. Such as initialization.

 

Of course, with the development of the language, the main functions of some languages can be defined by users. However, the principle remains unchanged.

 

3. Program syntax

 

What is the syntax? Grammar is the language provision. It is the law of the language. If it violates this provision, this procedure will not be used, and errors will occur. It seems that our programmers cannot escape the constraints of the Legal Society.

 

1) # include "stdio. h"

 

It means to include files, which is also referenced. The syntax is # include + "" + file name.

 

If you misspell include, add no #, do not pair double quotation marks, add a semicolon to the end.

 

The compilation fails. If something goes wrong, it is illegal.

 

2) main ()

 

The main function is the first entry to the program. The function must be followed by a pair of scratches. If you change to a large scratch number, replace it with a medium scratch number, or only one small scratch number, or add a semicolon after it does not conform to the syntax, an error occurs. The function is followed by a pair of small scratches without semicolons, which is a syntax.

 

3) {,} is the boundary of the function body, which belongs to this function within this time. You cannot have one more scratch.

 

 

 

4. function call

 

The most common method for programming is function calling. Function calling is to avoid repeated coding and use existing encoding. Since function calls in C language are free of charge, can they be called without calling? Although this is a joke. However, calling a function is definitely one of the most important tasks of a programmer. A programmer not only needs to call system functions, but also calls functions of others. More importantly, they need to call their own functions. This program has two function calls:

 

1) fprintf (stdio, "hello world! \ N ");

 

Remember the three parts of function calling: function name + parameter +.

 

Remember to write function calls next time. None of them work. Remember, what is the name of a function when you call a function or want to call a function? What are the function parameters? ";" Did you forget to write.

 

In many cases, we often pay attention to the first two and forget the ";" result compilation always fails. If you are a beginner, you must lay a good foundation and avoid this problem in future programming.

 

However, when you have learned different languages, you will often be confused. For example, the statements in VFP are not followed by ";". At this time, you will add a semicolon in the VFP, in the C language without adding points, making people laugh.

 

2) exit (0 );

 

This is a function to exit the program. Inbound and Outbound. With exit, you can interrupt processing in the program at any time and exit the program. It is also important for beginners to exit functions. The value 0 indicates that the return value of the program is 0. In the case of 80%, the return value of the program is useless. I have finished all the operations and returned everything. However, in a large project, when the relationship between programs occurs, the return value may be an important link, so you will learn to use the return value of the program. Fortunately, this is an example in actual practice. Here you will use exit (0. Some older programmers will yell, and I don't need to exit (0), so the program will exit. Yes, there is no exit (0), and the program exits. However, as a complete program, it is necessary to have inbound and outbound operations. Saving a statement is not a problem, but it is not readable. Add exit (0). Regular writing will produce good habits, and good habits will make you more formal.

 

 

 

5. Reference of. h

 

. H files are often a definition file that defines constants, structures, and external data. Some definitions are conditional. Generally, a C language must reference at least three system files. It means to include files, which is also referenced. To put it bluntly, it places some source programs to the outside. In this way, your source program looks more concise. For new users, # include generally contains *. h of some systems, such as stdio. h, which can be read as "Standard io ". For experts, # include will contain some of their own. h

 

# Include is followed by "", <> two methods include files. Generally, "" is enough. Unless you do not need to <> NO. I will stop here to discuss practical issues. The emphasis on language learning is practical, rather than making you a linguistics. As for # include, you only need to know a method to include. You do not need to know the difference between the two. Because, within a few years, it can be said that it is difficult for you to obtain a. h file that is the same as the system. If you want to learn how to search from the system directory, does it make sense to start searching from the current directory? If there are many meaningful things, you may not have time to learn.

 

Remember:

 

1) # include a definition file referenced in general cases. In special cases, you can also reference a function file. Or define a file that combines with a function.

 

2) # include generally references system files. In special cases, you can reference your own definition files and function files.

 

6. Keywords

 

Although the program has only six rows, such as include and main, they are all keywords. The so-called keyword is the name enabled by the system. You cannot get the same name. For example, you cannot create a function named main by yourself. In the future, we will encounter more keywords, such as if, else, And while int char. With this preliminary concept, we can pay more attention to keywords and avoid keywords in the future. Generally, programmers with higher levels often encounter the problem that their names are the same as those of the system. This is because he is getting closer and closer to system programmers, which is rare for new users.

 

7. escape characters

 

Fprintf (stdio, "hello world! \ N "); \ n is very interesting.

 

\ N indicates a line break. N can be recorded as new line. This makes it easier to remember. In the C language, some non-printable control characters are represented by the escape operator and symbol. Together with \ n, there are also \ r carriage return (return) and so on. In addition, for some key characters, \ must be added, for example, \ represents \.

 

Escape Character is also an important concept in C language and is also commonly used.

 

8. Stream Application

 

Fprintf (stdio, "hello world! \ N "); stdio is a standard output and a stream. You can regard stream as a channel. This channel can lead to a device, a file, or the next stream. Stream is very informative. There is a lot of content, and the program stream is displayed on the screen.

 

If you enter./a. out> 1.txt

 

Then the screen does not show hello world !, And hello world! Then, it will go to the 1.txt file.

 

Enter cat 1.txt in the command line.

 

You can see hello world! .

 

This indicates that the stream is redirected to the file.

 

 

 

If you don't see the above summary, you can talk to the novice about the above content, which shows that your foundation is good. On the contrary, you only see hello world! There is nothing to say about how to write this statement. I can only say that you need to strengthen basic training, change your learning methods, think more, and use the smallest program to think more.

 

 

 

If someone asks, I have prepared hello world! What should I compile below? I suggest modifying the program to display hello world three times. At that time, I will judge the program.

 

 

 

I didn't want to perform in-depth analysis on hello world. However, to become a master, you need to understand the principle of building a high level. I still want to use this ground to explain more about the basic things, A good foundation is really good.

Nickname: n216

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.