In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "C language internal and external functions and compilation preprocessing methods". The editor shows you the operation process through practical cases, the operation method is simple, fast and practical. I hope this article "C language internal and external functions and compilation preprocessing methods" can help you solve the problem.
Internal function and external function
The basic unit of the C language program is the function. In the program composed of multi-source program files, the definition and call of the function must also consider the program scope that can be called.
Functions are global in nature, and the purpose of defining functions is to be called by other functions. If the declaration is not returned, the functions defined in a source program file can be called either by other functions in this file or by functions in other source program files. According to whether the function can be called by other source program files, the function can be divided into internal function and external function.
1. Internal function
If a function can only be called by other functions in its source program file, it is called an internal function. When defining an internal function, add the static keyword before the function header
Static type identifier function name (formal parameter list)
{
}
two。 External function
If you use the extern keyword before the function header when defining a function, the function is an external function. It can be called by functions in other source program files, in the general form as follows:
Extern type identifier function name (formal parameter list)
{
}
For example:
Extern void process (int a, int b)
{
……
}
The C language stipulates that if you do not use the static or extern keyword description when defining a function, the function defaults to an external function, that is, as opposed to using the extern description.
In other source program files that need to call external functions, you need to prototype the called functions using the extern keyword. For example: extern void process (int a pencil int b)
Compilation preprocessing
The compilation preprocessing commands of C language mainly include macro definition, file inclusion and conditional compilation. Preprocessing commands begin with "#", there is no semicolon at the end of the command, and each preprocessing command must occupy a separate line.
1. Macro definition
(1) No-parameter macro definition
The general form of a nonparametric macro definition is as follows:
# define Macro name character sequence
If a macro definition is used in the source program, during compilation preprocessing, all "macro names" that appear in the source program are automatically replaced with "character sequences", which is a plain text replacement, also known as macro replacement.
For example, three no-parameter macro names are defined below:
# define PI 3.14
# define R 10
# define AREA PI*R*R
(2) there is a macro definition.
The general form of the definition of a participating macro is as follows:
# define Macro name (Parameter Table) character sequence
When the parameter macro definition is replaced, the character sequence is used to replace the source program where the macro name appears, and the arguments in the macro reference are used to replace the formal parameters in the macro definition.
For example, a macro name with parameters is defined as follows.
# define MUL (a) (b) (a) * (b)
Macros with parameters can be used in the source program using the following statements:
Int Xerox 3, YBG 4, z
Z=MUL (XBI y)
Note: it is usually necessary to add parentheses to the parameters in the character sequence when there is a parameter macro definition, so as to avoid errors when carrying out macro replacement, such as z=MUL. The statement after macro replacement is: zroomxaccoun3quoyMurray 1: 10;.
(3) the file contains
File inclusion is also a compilation preprocessing command, which is used to introduce all the contents of the included file into the source program file that uses the file inclusion command. The file contains commands in the following two forms:
# include or # include "file name"
When the file inclusion command uses angle brackets, the compiler looks for the included file in the system default path; when using the double reference form, the compiler first looks for the included file in the current working directory of the user program, and if the file cannot be found, then go to the system default path to find the file.
Here are a few examples of files that contain commands:
# include / / contains the predefined header files of the system
# include "user.h" / / contains custom header files
# include "func.c" / / contains custom source program files
(4) conditional compilation
Typically, all statement lines in the source file need to be compiled and generated into the target file. If the source program needs to decide whether part of the source code participates in compilation according to specific conditions, you need to use the conditional compilation preprocessing command.
There are three main forms of conditional compilation preprocessing instructions in C language.
1. # if instruction
General form:
# if expression
Program segment 1
# else
Program segment 2
# endif
2. # ifdef instruction
General form:
# ifdef Macro name
Program segment 1
# else
Program segment 2
# endif
3. # ifndef instruction
General form:
# ifndef Macro name
Program segment 1
# else
Program segment 2
# endif
This is the end of the introduction of "C language internal and external functions and compilation preprocessing methods". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.