[C Language] advanced | program structure, advanced
Bytes ------------------------------------------------------------------------------------
Global variables:
// Main. c // Created by weichen on 15/7/14. // Copyright (c) 2015 weichen. all rights reserved. # include <stdio. h> int gAll; // int g2 = gAll; Compilation fails; const int gAll = 10; int g2 = gAll; yes, however, it is not recommended to write void f (int a), void t (void), int main (int argc, const char * argv []) {/* 1. global variables are defined outside the function. global variables have a global lifetime and scope. They are irrelevant to any function and can be used within any function. if the global variable is not initialized during global variable initialization, the value 0 is returned, and the compiler adds the value; if the local variable is in the memory, the pointer is not initialized, And the NULL value is obtained, the global variables can only be initialized with values known at the time of compilation. Their initialization takes place before the main function. when a static local variable is defined as a local variable, the static modifier is added to the local variable. When the function leaves, static local variables will continue to exist and keep their values static local variable initialization will only be done when you first enter this function, when you enter the function later, the static local variables at the time of last exit will be kept as special global variables. They are located in the same memory region. The difference between & gAll and & all is exactly the size of an int, the actual local variables are placed in another place. static variables have a global lifetime. The local scope static in the function indicates a local scope (accessible locally. it is dangerous for the function that returns the pointer to the address of the local variable because it leaves the function, if the local variable does not exist, the address that returns the global variable or the static local variable is safe, and the malloc memory in the function is safe, but the best way to cause problems is to return the passed pointer. tips do not use global variables to PASS Parameters and results between functions. Try to avoid using global variables in Toyota Motor Cases. Using global variables and static local variables is thread-unsafe */printf ("in % s is % d \ n ", _ func __, gAll); // in main is 0 f (gAll); printf ("again in % s is % d \ n", _ func __, gAll); // again in main is 2 t (); // all is 6 t (); // all is 7, not reinitialized to 5, use the variable value t (); // all is 8 return 0;} // global Variable void f (int) {printf ("in % s is % d \ n", _ func __, a); // in f is 0 gAll + = 2; printf ("again in % s is % d \ n", _ func __, gAll); // again in f is 2 int gAll = 1; // re-declare a local variable with the same name as the global variable. At this time, the global variable gAll is hidden printf ("last in % s is % d \ n", _ func __, gAll); // last in f is 1} // static local Variable void t (void) {static int all = 5; int k = 0; all + = 1; printf ("all is % d \ n", all); printf ("& gAll = % p \ n", & gAll ); // & gAll = 0x0000101c printf ("& all = % p \ n", & all ); // & all = 0x100001018 printf ("& k = % p \ n", & k); // & k = 0x7fff5fbff80c}/* int * g (void) {int x = 10; return & x; // return the address of the local variable. Either the compilation fails or the prompt is "warning }*/
Compile preprocessing:
// Main. c // Created by weichen on 15/7/15. // Copyright (c) 2015 weichen. all rights reserved. # include <stdio. h> // const double PI = 3.14159; // method available for C99 # method used before define PI 3.14159 // C99, # define is the pre-Compilation instruction # define FORMAT "% f \ n" # define PI2 2 * PI // pi * 2 # define PRT printf ("% f", PI ); \ printf ("% f \ n", PI2) # define cube (x) * (x) # define RADTODEG1 (x) (x * 57.3) # define RADTODEG2 (x) * 57.3int Main (int argc, const char * argv []) {/* compile preprocessing command # starts with compiling preprocessing commands. They are not components of C language, but C language programs cannot do without them # define is used to define how a macro sees the compilation process (save the temporary files during the compilation process and add the -- save-temps option): gcc main in the terminal. c-o 1.out -- save-temps [main. c-> main. I (files after preprocessing)-> main. s (Assembly Code)-> main. o (target code file) 1.out( Executable File)] tail main. I can see that PI is replaced with a value macro # define <Name> <value> In the following lines. Note that no extra points are allowed at the end, because the statement name is not C and must be a word, the value can be a variety of things. Before the C language compiler starts to compile, the compile Preprocessing Program (cpp) will replace the name in the program with the text with the complete value with gcc -- save-t Emps if a macro value has another macro name, it will be replaced again. If the macro value exceeds one row, the comment after the value of \ macro needs to be added at the end of the last row is not considered as a part of the macro value, valid macros without values in C Language annotations # macros such as define _ DEBUG are used for Conditional compilation, there are other compilation preprocessing commands to check whether the macro has been defined (if this part of code is defined, another part of code is not defined) predefined macro _ LINE _ // current code LINE number _ FILE _ // FILE name of the source code containing path _ DATE _ // compile DATE _ TIME _ _ // compile time _ STDC _ // when the program strictly complies with the ANSIC standard, identifier _ STDC _ will be assigned to a macro with 1 parameter # define cube (x) * (x )) a macro can contain multiple parameters. # define MIN (a, B) (a)> (B )? (B): (a) it can also be combined (nested) using other macros that are widely used in code of large programs (which is more efficient than functions in place of functions, but the code size is larger than the function size. For example, the "generate" function has a cultural difference between China and Western countries with the help of the two operators # And # (more macro projects are used in foreign countries) some macros will be replaced by inline functions (With inline, this function is declared rather than defined. When inline is used, it is equivalent to replacing the current call with the code in the function, the code is added but the extra overhead of function calling is reduced. The space is used for time. When to use inline: 2-3 rows of functions that are very small or frequently called in loops, when not using inline: Functions with more than 20 rows or recursive functions) macro defined incorrectly # define RADTODEG1 (x) (x * 57) # define RADTODEG2 (x) * 57. Macro with parameters. Everything must be in parentheses. The whole value must be in parentheses. Every place where the parameter appears must be in parentheses. # define RADTODEG (x) * 57.3) other compilation pre-processing instruction condition compilation error... */printf ("% f \ n", 2 * PI); // 6.283180 printf (FORMAT, PI2); // 6.283180 PRT; // 3.141590 6.283180 printf ("% s: % d \ n", _ FILE __, _ LINE _); // Users/weichen /... /main. c: 66 # It is usually used to debug printf ("% s: % s \ n", _ DATE __, _ TIME _); // Jul 15 2015: 01: 47: 36 # differentiate program versions if (_ STDC _ = 1) {printf ("ANSIC \ n ");} else {printf ("Not ANSIC \ n");} int I = 2; printf ("% d \ n", cube (5 )); // 125 printf ("% d \ n", cube (I); // 8 printf ("% d \ n", cube (I + 2 )); // 64 printf ("% f \ n", RADTODEG1 (5 + 2); // 119.600000 printf ("% f \ n", 180/RADTODEG2 (1 )); // 10314.000000 # We want 180/57. 3, but not actually return 0 ;}
Large program structure:
// Main. c // Created by weichen on 15/7/15. // Copyright (c) 2015 weichen. all rights reserved. # include <stdio. h> int main (int argc, const char * argv []) {/* the code in main () is too long. It is suitable to be divided into several functions and one source code. the c file is too long. It is suitable to be divided into several files. Two independent source code files cannot be compiled to form an executable program project. Create a new project in DevC ++, then, add several source code files to the project. After all the source code files in a project are compiled, the DevC ++ compilation link will have two buttons for IDE compilation and building separately, the former is to compile a single source code file, and the latter is a link compilation unit for the entire project. A c file is a compilation unit. The Compiler only processes one compilation unit at a time and compiles and forms it. o files, which are linked by the linker. If the function called in main is not declared, C treats all parameters and return values of the function as int by default, in this case, it cannot be ensured that the function is correctly used to put the function prototype into a header file. end with h), in the source code (. in file c), # include this header file allows the compiler to know the function prototype during compilation. # include is a compilation preprocessing instruction, which is the same as a macro, before compilation, we processed it and inserted all the text content of the file to its location. the front of the c file # include has two forms to indicate the file to be inserted "" requires the compiler to first in the current directory (. the directory where the c file is located. If not, go to the directory specified by the compiler. <> let the compiler find the file only in the specified system directory (/usr/include) the compiler knows where the header file of its standard library is. The environment variables and the compiler command line parameters can also specify the header file directory where the function is used and defined. # include this header file. except for main. c has the same name. h. Put the prototype of all open functions and the declaration of global variables in # include misunderstanding # include is not used to import stdio into the database. in h, only the prototype of printf is available. lib (windows) or. in a (Unix), the current C language compiler will introduce all the standard libraries by default # include <stdio. h> just to let the compiler know the prototype of the printf function, make sure that the parameter value you provided during the call is the correct type. If a function is not made public to the public, adding static before the function makes it a function that can only be used in the compilation unit (the function is not hope others can use it, only available in the current file) add static before the global variable to make it a global variable that can only be used in the compilation unit where it is located (only global variables available in the current file) difference between variable definition and declaration int I is the definition of the variable extern int I; is the declaration of the variable, cannot be initialized, put in the header file; used to tell the compiler, other variables used I is an existing declaration that does not generate code. function prototype variable declaration structure declaration macro declaration enumeration declaration type declaration inline Declaration definition is something that generates code function global variables only declaration can be put in header files (rules are not legal) otherwise, multiple compilation units in a project have cognominal entities. Some compilers allow several compilation units to have functions with the same name, you can also use the weak modifier to emphasize that such a structure with the same name exists in the same compilation unit cannot be repeatedly declared. If your header file contains a structure declaration, it is difficult for this header file to be # include multiple times in a compilation unit. Therefore, you need to "standard header file structure" and use Conditional compilation and macros, ensure that this header file will only be # include once in a compilation unit # ifndef _ MAX_H _ # define _ MAX_H _..... # endif # pragma once can also play the same role, but not all compilers support */return 0 ;}
Link: http://www.cnblogs.com/farwish/p/4647044.html
@ Blackeye poet <www.farwish.com>