C language advanced summary

The c language is only for the development of unix, and it is for high efficiency. C
++ is for the object-oriented language. It is compatible with the existing code. The
system is generally written in multiple languages . int typedef usage: typedef int INT32 typedef unsigned char BYTE typedef struct _demo { short s; BYTE b1 ; BYTE b2; INT32 i ; The value of the variable is changed. The default type of the local variable is static: the local variable is stored in the static area of ​​the program. The file scope identifier The scope of the modified global variable is only in the declared file. The scope of the modified function is only in the declared file. register: indicates that the variable is stored in the register. It is only a request for a register variable but not necessarily a successful request. The register variable must be a value acceptable to the CPU register
































The address of . It is only initialized once in a system with high real-time performance requirements. Statement usage: if - else: (two-way branch) The condition of if is a logical value. The bool variable cannot be directly used in the condition. The comparison between ordinary variables and 0 should appear on the left side of the comparison symbol (avoid BUG and prevent it from being written as an assignment symbol). Float cannot directly perform 0-value comparison. It needs to define the precision switch: (multi-way branch) The case statement branch must have a break, otherwise it will cause It is necessary to add the default statement for branch overlap to deal with special cases . The value in the case statement can only be integer or character type . Sequential analysis: arrange each statement according to It is more suitable to deal with the real default case of if used for range judgment. Loop statement: do while for do - while is executed at least once Use do - while(0) in the case where it is only executed once (apply for memory to release memory to avoid memory leaks) , it must be used when using while Pay attention to what the condition is. break means to terminate the execution of the loop and jump out of the block continue means the median value. This loop body enters the next loop execution. Keyword analysis: goto:









































Disable goto , enter the cold palace void: decorate the return value and entry parameter of a function, cannot define variable types , only pointers of the same type can be assigned to each other void pointers can be used as lvalues ​​to receive pointers of void* can be used as the entry type of functions to receive Any type of pointer parameter extern: declare externally defined variables and functions, used to compiler to compile extern "C" in the c method sizeof name: the compiler's built-in indicator is not a function Calculate the memory size occupied by the corresponding entity, and its value is compiled in Sizeof int; is not allowed sizeof (int); is allowed, so it is uniformly written as sizeof(); It was wronged to become a function const: const is only useful to the compiler and is useless at runtime Variables will occupy memory The space uses the pointer to operate the address or the value of the array of const type that can change the value will also be changed The const-modified variable is the read-only nature or the variable const-modified pointer: const int* p; int const* p; int* const p; const int* const p; the left number and the right point remain unchanged


































When const appears to the left of the * sign, the data pointed to by the pointer is constant.
When const appears to the right of the * sign, the pointer itself is a constant. The const-modified function parameter indicates that the value of the parameter does not want to be changed in the function body. The const-modified function return value indicates that the return value cannot be Variable used for returning pointers volatile: compiler warning pointer Tells the compiler that it must go to memory every time to fetch the value of a variable without optimization Decorate variables that may be accessed by multiple threads Decorate that may be changed by unknown factors Enum: enum is A custom type enum default constant adds 1 to the previous value in turn The first value defaults to zero  Variables of enum type can only take discrete values ​​at the time of definition The define macro is just a simple replacement and cannot be debugged It is a specific type The constant typedef: rename an existing data type, no new type is generated , signed unsigned cannot be expanded, define is just a string replacement, no alias concept Symbol skills: Comments: the compiler will delete comments when compiling At the same time, use spaces instead. The compiler thinks that the contents enclosed in double quotes are all strings. Double slashes are no exception . /*...*/ type comments cannot be nested /* as the beginning of a comment until it encounters * / Accurate and easy to understand and concise prompts to explain exactly why










 











 













Continuation character and escape character:
Continuation character \:
a powerful tool to indicate the behavior of the compiler to continue the content of the next line.
Pay attention to adding spaces
. When connecting words, be careful not to add spaces . : Indicates no echo character  \n Newline \t Tab appears in a character or string Single quote & double quote: Single quote means character constant Represents integer 'a' One byte 'a' + 1 = 'b' Double quote means String constant represents a pointer "a" Two bytes "a" + 1 = '\0' (pointer operation) Cannot assign and compare characters and strings Logical operators: Short-circuit rule: || evaluates from left to right as True stop && evaluate from left to right as false stop ! : "!" Only recognize 0, see 0, return 1 , return 0 when the value is not 0. Ternary operator: return a value not a variable after a logical judgment Bitwise operator: avoid bitwise operator logical operators and mathematical operators appearing in the same expression as much as possible Expressing the order with () Left shifting n bits is equivalent to multiplying by 2^n



































Right shifting n bits is equivalent to dividing by 2^n a ^ a = a; operator analysis: ++: i = 3; (++i) + (++i) + (++i) = 16 / 18; i = 3; k = (++i, i++, i+10) = 15 //Comma expression evaluates the last expression from left to right i +++ 1  The compiler does as much as possible from left to right Contains character precedence and type conversion: ------------------------------------------------------ ---------------- FAQ | Expressions ---------------------------- ----------------------------- . has a higher priority than * -> removes this problem  | *pf [] has a higher priority higher than * | int *app[] function() higher than * | int *fp() == != higher priority than bit operations | (val & mask != 0) == != higher priority than assignment operator |



















c = getchar() != EOF
arithmetic operator is higher than shift operator | mab << 4 + lsb
comma operator has the lowest precedence of all operators i = 1, 2;
---------- ------------------------------------------------ C language The implicit type conversion of: low type to high type conversion of actual parameter to formal parameter conversion of the right side of the assignment to the left side of the assignment return expression is converted to the return value type char -> int -> unsigned int -> long -> unsigned long -> double   | short -> float compile preprocessing compile preprocessing: compile process: preprocessor - compiler - assembler - linker gcc -E test.c -o test.i gcc -S test.i -o test.s gcc -C test.s -o test.o single-step compilation technology to eliminate errors The job of the linker is to link each independent module into an executable program. Static linking is completed when the compiler completes dynamic linking at runtime. Macro definition usage analysis:










 















#define The code after this line can use this macro constant.
Macro expressions are like functions, but they are not functions. They are more powerful and error-prone. Macro expressions are just
simple text replacement with side effects.
Do not write expressions in macro parameters.
Extended usage of C language
Compiler does not know the existence of macro expressions
Use actual parameters instead of formal parameters without any calculation The actual parameters can be just data types
Macro expressions have no calling overhead
Macro expressions cannot have recursive definitions #undef end #define Definition domain Powerful built-in macro __FILE__ : print file name __LINE__ : cannot be placed in a function to call macro code block can be used Log  macro : print time contains <time.h> macro code block multiple statements and multiple lines: do{} while (0) + continuation character Conditional compilation use analysis: #if #else #endif : It does not work when the compiler works gcc -DC=1 -E test.c -0 test.i //Pre-predict at the command line When compiling, specify C=1 #include: embed the content of the existing file into the current folder. Indirect inclusion will also generate the action of embedding the content of the file, so use #ifndef #define

























The #endif 
conditional compilation technique only covers the case of preventing multiple expansions and redefinitions once. Application : Different product lines use the same code. Distinguish between the debug version and the release version of the compiled product #error and #line: #error pragma: generate a Compilation error message Stop compiling #error message No need for double quotes #warning message Generating a compilation warning does not stop compiling #line pragma: used to force a new line number and compile file name and renumber the source code #line number filename filename can omit #pragma: Compiler directives are used to instruct the compiler to perform some specific actions Directives are compiler and operating system specific Not portable between different compilers The compiler will ignore unrecognized directives Different compilers will interpret the same pragma in two different ways #pragma message The message parameter has a similar function in most compilers The message parameter outputs information at compile time to the compile output window The message can be used for the version of the code Control Knowing if the compiled code is what you want #pragma pack




























Memory alignment Different types of data are not sequentially arranged in memory.
CPU reads from memory are not continuous block-by-block reads. Only read and write even address pragma(4) tells the compiler the default alignment. Structure members The alignment parameter is an integer multiple of the alignment parameter used by all its members. # and ## operators use parsing: preprocessor start character # used to convert macro parameters to strings during pre-compilation # exist inside the macro to use print Called function name ##Used to glue two characters at compile time ##Define the structure type #define STRUCT(type) typedef struct _tag_##type type; struct _tag_##type



















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324854249&siteId=291194637