Read the C programming language (6)

Source: Internet
Author: User

Chapter 4 functions and functionsProgramStructure and content are getting deeper and deeper, and the gains are getting bigger and bigger. I wrote an article "Where should the program be conceived before writing ?"ArticleMentioned the program design issues. In section 4.1 of this chapter, the author gives an example of program conception. For a program that prints a row containing a specific string in the input, the author divides it into three parts:
While (there's another line)
If (the line contains the pattern)
Print it
All three parts can be written as independent functions. As for why, the author said, "three small pieces are better to deal with than one big one, because irrelevant details can be buried in the functions, and the chance of unwanted interactions is minimized. and the pieces may even be useful in other programs. ". Irrelevant details are hidden in independent functions, and the errors caused by mutual influences are greatly reduced. In addition, such functions can be reused by other programs.

It is easy to make a mistake. For a function whose return value type is omitted, the default return value type is not void, but int, such as main function.

In section 4.2, the author mentioned a problem that is easy to ignore. If a function is defined in a file and called in another file, the function should be declared before the function is called. Otherwise, the function will be implicitly declared when it appears for the first time. In this way, the return value type of the implicitly declared function is int, and the parameter is not limited. Similarly, if no parameter is included in the brackets during declaration, the compiler does not limit the parameter, so it does not perform parameter detection. Therefore, the author emphasizes that if a function has a parameter, the parameter must be declared. If not, the parameter is declared as void.

When talking about the applicability of external variables, the author said, "If a large number of variables must be shared among functions, external variables are more convenient and efficient than long argument lists. "If there is a large amount of data to be shared between functions, it is easier to use external variables than the parameter list. Next, the author gives an example of the inverse Polish calculator, where the stack is implemented using external variables.

"The scope of an external variable or a function lasts from the point at which it is declared to the end of the file being compiled. "The scope of an external variable or function starts from being declared to the end of the file. Therefore, if the external variables or functions used are in another file, they must be declared through extern. If you are in the same file, it depends on the location of the variable or function definition. If the definition is used before it is used, it acts as a declaration. if the definition is used, it must be explicitly declared.

The author explains the origins of header files in section 4.5. Because many files need to declare external variables or functions, and we do not want such declarations to be repeated, we put these public declarations into a file, that is, the header file. There will be some trade-offs here. Generally, a file requires a small amount of external declarations, and it is troublesome to maintain several headers. Therefore, for general programs, generally, all external declarations are put in a header file, while large programs can be organized into multiple header files.

Static keywords can be applied to external variables, external functions, and local variables. If it is applied to external variables or functions, such variables or functions can only be used in the current file and are invisible to other files. If the application is on a local variable, such variables will always exist and will not be created or destroyed as the function enters or exits.

Two interesting points about the Register keyword are: 1. you declare a local variable as register, which can be ignored by the compiler; 2. as long as you declare the register variable, you cannot get its address, whether it is actually put into the register or not. It's a bit unfair to programmers.

# include can be set to # include "FILENAME" or # include . The difference between the two is that the former is first located from the source program directory. If it cannot be found, it is the same as the latter, according to certain rules.

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.