◆Key Point 1: Method to Swap Two Variable Values
1) Using a third variable (the easiest method)
2) Using addition and subtraction to swap values (commonly used in interviews **)
The code is as follows:
b = a - b; a = a - b; b = a + b;
3) Using bitwise XOR to swap
The code is as follows:
a = a^b; b = a^b; a = a^b;
◆Key Point 2: The result of the modulus operation % has the same sign as the dividend, and the result of two positive numbers has the sign prefixed
◆Key Point 3: Usage of sizeof
-
sizeof is an operator and should not be mistakenly understood as a function
-
sizeof can be used without ()
-
sizeof can be applied to variables, constants, and data types
-
For data types, parentheses must be added
◆Key Point 4: #include Preprocessor Command and Multi-file Team Development
-
include preprocessor command can be considered as: copying the contents of the following file into the current file
-
In multi-file team development: modules are compiled into .o files and cannot link successfully, they are combined to link and generate the final program.
-
Linking: combines all related .o object files and C language function libraries in the project to generate an executable file
—— Function definitions are placed in .c files, and function declarations are placed in .h files
—— If you want to use a function defined in a .c file, just #include the corresponding .h file
—— The role of .h files: to be copied by others. During compilation and linking, .h files do not need to be considered
◆Key Point 5: Several Initialization Methods for Arrays are as Follows:
int a[3] = {10, 9, 6}; int a[3] = {10,9}; int a[] = {11, 7, 6}; int a[4] = {[1]=11,[0] = 7}; // just know this initialization method
◆Key Point 6: Memory Analysis and Precautions for Arrays
-
Storage space allocation (memory allocation is done from high address to low address, but the elements of an array are arranged from low to high)【Note: Important for future learning】
-
The role of array names is to view the address of elements
-
Be careful not to go out of bounds with arrays
◆Key Point 7: String Knowledge Points
-
“123” is actually composed of ‘1’, ‘2’, ‘3’, and ‘\0’
-
The output of a string is “%s”, and ‘\0’ will not be output
◆Key Point 8: String Processing Function: strlen()
-
It counts the number of characters, not the number of words
-
The counted characters do not include ‘\0’, and one Chinese character counts as three characters
Example: “哈haha” has 7 characters
-
Counts the number of characters starting from a certain address, until it encounters ‘\\0’
The pointer part occupies an important position in C language, so I focused on learning and organizing pointer knowledge:
◆Key Point 9: Format for Defining Pointers
Variable type *variable name
For example: Int *p
◆Key Point 10: Function of Pointers
Can access the corresponding storage space based on an address value
Example:
Int *p; Int a = 90; p = &a; *p = 10; // Assign 10 to the storage space pointed to by p
◆Key Point 11: Cautions in Using Pointers
-
Int *p can only point to int type data
-
Pointer variables can only store addresses
-
Pointer variables that have not been initialized should not be used to indirectly access other storage spaces
◆Key Point 12: Pointers and Arrays
Traverse arrays
int ages[5] = {10, 4, 9, 44, 99}; for(int i = 0; i<5; i++) { printf("%d\n", ages[i]); }
Using pointers to traverse arrays:
int *p; // Pointer variable p points to the starting address of the array p = &ages[0]; // Use pointer to traverse the array for(int i = 0; i<5; i++) { printf("ages[%d] = %d\n", i, *(p + i)); }
Note: Pointer + 1 depends on the pointer type
Note: Accessing arrays
Array name[index]
Pointer variable name[index]
* (p + i)
◆Key Point 13: Pointers and Strings
Two ways to define strings:
1. Using arrays
Char name[] = "Andyzhao"
Characteristics: The characters in the string can be modified
Usage: When the string content needs frequent modification
2. Using pointers
Char *name = "itcast"
Characteristics: The string is a constant, and the characters inside the string cannot be modified
Usage: When the string content does not need to be modified, but this string is frequently used
◆Key Point 14: Preprocessor Directives (Three Types):
Macro Definitions
Conditional Compilation
File Inclusion
1. Macro definitions must be used in pairs and can have parameters:
#define ... #undef
Macros with parameters:
#define sum(v1,v2) ((v1) + (v2)) // Parentheses are necessary
For example:
#define pingfang(a) a*a #define pingfang(a) (a*a)
When calling:
pingfang(10)/pingfang(2) // Incorrect pingfang(5+5) // Incorrect Macros with parameters are more efficient than functions
2. Conditional Compilation (usually checks the value of macros)
#if condition ... #elif condition ... #else ... #endif (very important) otherwise the following code will be invalid
3. File Inclusion:
<> indicates system files, “” indicates custom files
Circular inclusion is not allowed, for example, a.h includes b.h, and b.h includes a.h
◆Key Point 15: The Difference Between static and extern is Whether Cross-file Access is Possible
①. Functions
②. Variables
1. Effect on Functions:
External Functions: Defined functions can be accessed by both this file and other files
Internal Functions: Defined functions can only be accessed by this file
By default, all functions are external (equivalent to having the extern keyword), so it can be omitted
Effect of extern:
Both a complete definition and a reference to an external function are prefixed with extern
When referencing, it is also assumed to be an external function, so extern can also be omitted
Effect of static: defines an internal function
Usage: static return type function name (parameter list)
Cannot be called by other files
In a project, the external function names in this file cannot have the same name as those in other files (error)
Internal functions (static) in this file can have the same name as functions in other files
2. Effect on Variables:
Global variables are divided into two types:
External Variables: Defined variables can be accessed by other files
①. By default, all global variables are external
②. External variables with the same name in different files represent the same variable
③. An external variable is defined without extern, but declared with extern
Similarly, the declaration is not erroneous
Internal Variables: Defined variables cannot be accessed by other files
Internal variables with the same name in different files do not affect each other
(Article link: http://www.jianshu.com/p/26baf827b878)
Follow me
WeChat ID:XNMDJKTX
Providing campus news, interesting anecdotes, and various computer-related knowledge, including systems, software and hardware, training, and technical support for all faculty and students…Only more exciting content you don’t know, only more knowledge you haven’t explored, I’m here, waiting for you!