Title:c language function and program structure
Tags:c language scope rules, external variables, static variables, register variables, macro definitions
Grammar_cjkruby:true
---
External variables
Variable declarations are used to describe the properties (types) of variables, and variable definitions also cause memory allocations
int sp;double s[MAX];
Declaration Place: Outside function
If the above variables are defined outside all functions, that is, external variables, and for these two external variables sp, S[max], allocate the storage unit and the length of the array, all functions in its source file can use these two external variables.
extern int sp;extern double s[MAX];
Declare two external variables sp, S[max] for the source file, but the difference is that the lengths of the two variables are defined in other source files of the source program, and no re-establishment of variables and allocation of storage units.
In all source files of the source program, an external variable can only be declared in one file, and another cannot be defined elsewhere 相同的外部变量 , if you want to use the variable to access it through a extern declaration.
The length of the array must be specified where the external variable is defined.
static variables
static int sp;static double s[MAX];
Declaration Place: Outside function, inside function
Declare SP, S[max] These two external variables as static variables, except that the functions within the source file can access the SP, S[max], but other files cannot be accessed by the extern static variable sp and S[max].
staticIt is also possible to declare an automatic variable (a variable that is used within a function), but unlike other internal variables, the variable, regardless of whether its function is called by the main function, is always present and does not disappear with the function being called to exit, which means static The internal variables defined can only have their own separate storage space within a particular function.
Register variable
register int sp;register double s[MAX];
Declaration Place: Inside function and formal parameter of function
registerThe function is to tell the compiler that the variable it declares is used more frequently in the program, placing the declared variable in the machine's register, which makes the program smaller and executes faster
Program block
while(***){ int i=1; if(***){ …… } ……}
The curly braces include the statement that is a block, in which the variable is declared to hide the variable with the same name outside the block, the variable declared within the block is independent of the variable outside, and the same static variable is initialized only once.
Initialization
If an external variable is not initialized, it is initialized directly to 0, and the automatic and local variables are not initialized (useless information).
The initialization definition of an external variable must be a constant expression and cannot contain other variables that have already been defined, whereas automatic and local variables can contain
For the initialization of an array, if the number of subsequent initialization expressions is less than the number of elements in the declaration of the outer variable, the number is directly initialized to 0, whereas the local variable throws an error
String array declarations are special and can be used directly "" instead of{}
C language Preprocessor
#include " "And
#include< >The difference
#include(file contains instructions) makes it easier to handle a large number of #define instructions and declarations, allowing the source file to contain the file contents specified by the file name directly.
#include " "Locate the file in the same location as the source file, and if you do not find the file, locate the file according to the appropriate rule.
#include< >Find the file directly according to the appropriate rules
Macro substitution
#define 名字 文本The place where the name appears will be replaced with text, the longer line can be divided into several lines, simply add a backslash at the end of the line to be continued \
#defineThe scope of a macro definition starts with a definition point, and then until the end of the source file
#defineA macro definition cannot be useful for strings inside quotes, such asprintf("YES")
#udefTo cancel a macro definition
#defineCan also be taken with parameters, so that it can be like the actual parameters,#define max(a,b) (a)>(b) ? (a) : (b)
Condition contains
Using conditional statements to determine the pre-processing statements, in the process of preprocessing execution, you can choose to include different code in the compilation process according to the calculated condition value to provide the means.
#if !defined(名字)…… #else / #endif / #elif……
defined(名字)Indicates that when the name is defined, its value is 1; otherwise, its value is 0
#if !defined(HDR)#define HDR #endif
This allows you to include additional header files that each header file depends on
C language function and program structure