当前位置:网站首页>Sorting out the core knowledge of C language
Sorting out the core knowledge of C language
2022-07-20 16:56:00 【Meteor shower Online】
One 、 Basic data type
Basic data type
data type | String template format output | sizeof() byte |
---|---|---|
int | %d | 4 |
short | %d | 2 |
long | %ld | 8 |
float | %f | 4 |
double | %lf | 8 |
char | %c | 1 |
Hexadecimal formatted output
// %x Hexadecimal
// %o octal
// %s character string
// %#x Print address
sizeof Judge byte length
// The number of bytes occupied by the basic data type
printf("int Occupy %d byte \n", sizeof(int));
printf("char Occupy %d byte \n", sizeof(char));
printf("float Occupy %d byte \n", sizeof(float));
Print function printf,
int i;
scanf("%d", &i); // Console input ,& Fetch address
Input function scanf
printf("i The value of is :%d\n", i);
The system is suspended
system("pause");
Two 、 The pointer
The pointer stores the memory address of the variable .
2.1、 Variable pointer
*: The data type of the pointer is consistent with that of the variable , Namely variable int i Corresponding int *p
&: Take the address operator , Get variable address
int i = 90;
// Pointer to the variable , Create a int Pointer to type
//p The value is i The memory address of this variable
int *p = &i;
printf("%#x\n", p);
Pointer assignment , Also called variable indirect assignment .
i = 89; // Direct assignment
*p = 89; // Indirect assignment
Null pointer NULL.
The default value of a null pointer is 0
int *p = NULL;
printf("%#x\n", p); // Output 0
2.2、 Multi level pointer
The pointer holds the address of the variable
int a = 50;
//p1 Last saved a The address of
int* p1 = &a;
//p2 Last saved p1 The address of
int** p2 = &p1;
printf("p1:%#x\n", p1); //p1:0xbfeff2ac
printf("p2:%#x\n", p2); //p2:0xbfeff2a0
printf("*p2:%#x\n", *p2); //*p2:0xbfeff2ac
Multi level pointer assignment
**p2 = 90;
2.3、 Array
ids: Array name
&ids[0]: The address of the first element of the array
&ids: Array address
All equal
// Arrays are stored continuously in memory
int ids[] = {
78, 90, 23, 65, 19 };
// Array variable name :ids Is the first address of the array
printf("%#x\n", ids); //0xbfeff2a0
printf("%#x\n", &ids[0]); //0xbfeff2a0
printf("%#x\n", &ids); //0xbfeff2a0
Pointer arithmetic p++、p–: Move sizeof( data type ) Bytes
int* p = ids;
// The addition of the pointer
p++; //p++ Move forward sizeof( data type ) Bytes
printf("p Value :%#x\n", p); //p Value :0xbfeff2a4
printf("p Point to :%d\n", *p); //p Point to :90
Assign a value to an array through a pointer
int uids[5];
// In earlier versions
int* p = uids;
for (int i=0; p < uids + 5; p++,i++)
{
*p = i;
}
Equivalent to ( In the oldest C There are no brackets in the language array[i] Of )
int uids[5];
for (int i = 0; i < 5; i++)
{
uids[i] = i;
}
2.4、 A function pointer
The function name is the function address
int msg(char* msg, char* title)
{
printf("title:%s,msg:%s\n",title,msg);
return 0;
}
int main(int argc, const char * argv[]) {
printf("%#x\n", msg); //0x3ec0
printf("%#x\n", &msg); //0x3ec0
return 0;
}
A function pointer
// Function return value type , The name of the function pointer , Parameter list of function
int (*fun_p)(char* msg, char* title) = msg;
fun_p("content", "title"); //title:title,msg:content
// Equivalent to
msg("content","title"); //title:title,msg:content
Function pointer as input parameter .
The figure below msg Function input parameter is minus function 、10、20
// Candidate methods 1
int add(int a, int b)
{
return a + b;
}
// Candidate methods 2
int minus(int a, int b)
{
return a - b;
}
// Function into the reference 、m value 、n value
void msg(int(*func_p)(int a, int b), int m, int n)
{
printf(" Execute callback function ...\n");
int r = func_p(m, n);
printf(" Execution results :%d\n", r);
}
void main()
{
msg(minus, 10, 20);
}
2.5、 Section
Case study : Generate an array with random numbers , Write a function to find the smallest value , And return the address of the smallest number , Print it out in the main function
Random number generation
// Initialize the random number generator , Set seeds , The seeds are different , Random numbers are different
// The current time is used as the seed A signed int -xx - > +xx
srand((unsigned)time(NULL));
rand() % 100;//100 Within the scope of
The whole algorithm
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// minimum value
int* getMinPointer(int ids[], int len)
{
int i = 0;
int* p = ids;
for (; i < len; i++)
{
if (ids[i] < *p)
{
p = &ids[i];
}
}
return p;
}
// Generate random number
int* generateArray(){
int ids[10];
// Initialize the random number generator , Set seeds , The seeds are different , Random numbers are different
// The current time is used as the seed A signed int -xx - > +xx
srand((unsigned)time(NULL));
for (int i = 0; i < 10; i++) {
ids[i] = rand() % 100;//100 Within the scope of
printf("%d\n", ids[i]);
}
return ids;
}
void main()
{
int* ids=generateArray();
int* p = getMinPointer(ids, sizeof(ids) / sizeof(int));
printf("%#x,%d\n",p, *p);
}
2.6、 Several ways to write pointers and data
For arrays :
a+i // Equivalent to &a[i]
*(a+i) // Equivalent to a[i]
The following is derived
int a[] = {
78, 34, 73, 25, 80, 90 };
//a The first address
//a Equivalent to &a[0],a+1 Equivalent to &a[1]
// Conclusion : a+i Equivalent to &a[i]
//*a Equivalent to a[0],*(a+1) Equivalent to a[1]
// Conclusion : *(a+i) Equivalent to a[i]
// The value of the element and the address of the element
verification
int a[] = {
78, 34, 73, 25, 80, 90 };
int* p = a;
for (int i = 0; i < 6; i++)
{
printf("%d,%#x\n", a[i], &a[i]); //78,0xbfeff2a0
printf("%d,%#x\n", *(a + i), a + i); //78,0xbfeff2a0
printf("%d,%#x\n", p[i], &p[i]); //78,0xbfeff2a0
printf("%d,%#x\n", *(p + i), p + i); //78,0xbfeff2a0
}
2.7、 Two dimensional array
Traverse the output
void main()
{
int a[2][3] = {
95,82,56,17,29,30 };
for(int i=0;i<2;i++){
for (int j=0; j<3; j++) {
printf("%d,%#x ",a[i][j],&a[i][j]);
}
printf("\n");
}
}
// 95,0xbfeff2a0 82,0xbfeff2a4 56,0xbfeff2a8
// 17,0xbfeff2ac 29,0xbfeff2b0 30,0xbfeff2b4
Two dimensional array a、*a、&a The difference between :
Means | Memory address | Sizeof size | |
---|---|---|---|
a | Row pointer , Is the pointer to the first row of the array | 0xbfeff2a0 | 12 |
*a | Pointer to the first element of the first row of the array | 0xbfeff2a0 | 4 |
&a | A pointer to a two-dimensional array | 0xbfeff2a0 | 24 |
verification
printf("%#x,%#x,%#x\n", a, &a, *a);//0xbfeff2a0,0xbfeff2a0,0xbfeff2a0
// here sizeof Measure the length of the value pointed
printf("%d,%d,%d\n",sizeof(*a),sizeof(*&a),sizeof(**a));//12,24,4
The pointer moves
//a Is a line pointer , Is the pointer to the first row of the array ,a+1 The pointer of the second line , And so on
printf("%#x,%#x\n", a, a + 1);//0xbfeff2a0,0xbfeff2ac
//*a Is the pointer to the first element in the first row of the array ,*a+1 Is the pointer to the second element in the first row of the array
printf("%#x,%#x\n", *a, *a + 1);//0xbfeff2a0,0xbfeff2a4
//*(a+1) Is the second of the array (1) Line first (0) Pointers to elements
printf("%#x,%#x\n", *(a + 1), *(a + 1) + 1);//0xbfeff2ac,0xbfeff2b0
Take the first... Of the array 2 That's ok , The first 3 Elements
printf("%d\n", a[1][2]); //30
printf("%d\n", *(*(a + 1)+2)); //30
3、 ... and 、 Dynamic memory allocation
3.1、C Language memory allocation :
1. The stack area (stack)
Automatically assigned , Release
2. Heap area (heap)
The programmer manually assigns the release , operating system 80% Memory
3. Global area or static area
4. Character constant area
5. Program code area
3.2、 The stack area (stack) Memory allocation
Use basic data type keywords to create .
// Distribute 40M Memory ,Stack Overflow
//windows Next , Stack memory allocation 2M( Determined constant ), Limit exceeded , Tips stack overflow error
int a[1024 * 1024 * 10];// One int by 4 byte
3.3、 Heap area (heap) Memory allocation
Use malloc、realloc Keywords open up memory
int* p = malloc(len * sizeof(int)); // open up
int* p2 = realloc(p, sizeof(int) * (len + addLen)); // Capacity expansion
Static memory allocation , The amount of memory allocated is fixed , problem :1. It's easy to exceed the maximum stack memory 2. In order to prevent insufficient memory, more memory will be opened up , Easy to waste memory
Dynamic memory allocation , While the program is running , Dynamically specify the memory size to be used , Hand release , After the memory is released, it can be reused ( Similar to running water )
3.3.1、malloc open up
Cycle open release 40M Memory
#include <unistd.h>
void main()
{
while (1) {
sleep(3);
//40M Memory
int* p = malloc(1024 * 1024 * 10 * sizeof(int));
printf("%#x\n", p);
sleep(3);
// Release
free(p);
sleep(3);
}
}
practice : Create an array , Dynamically specify the size of the array
void main()
{
// Static memory allocation creates arrays , The size of the array is fixed
int a[10];
int len;
printf(" Enter the length of the array :");
scanf("%d", &len);
// Open up memory , size len*4 byte
int* p = malloc(len * sizeof(int));
srand((unsigned)time(NULL));
for (int i = 0; i < len; i++)
{
p[i] = rand() % 100;
printf("%d,%#x\n", p[i], &p[i]);
}
// Manual memory release
free(p);
}
3.3.2、realloc Capacity expansion
There are two ways to reallocate memory :
narrow , The reduced part of the data will be lost
expand ,( Successive )
1. If there is required memory space behind the current memory segment , Expand this memory space directly ,realloc Return to the original pointer
2. If there are not enough free bytes after the current memory segment , Then use the first memory block in the heap that can meet this requirement , Copy the current data to a new location , And release the original database , Return the new memory address
3. If the application fails , return NULL, The original pointer is still valid
// The code is on top malloc Then write on the basis of development
int addLen;
printf(" Enter the increased length of the array :");
scanf("%d", &addLen);
// Out of memory , Expand the memory space just allocated
int* p2 = realloc(p, sizeof(int) * (len + addLen));
if (p2 == NULL)
{
printf(" Reassignment failed , The world is so big , There's no room for me ...");
}
// Reassign
i = 0;
for (int i = 0; i < len + addLen; i++)
{
p2[i] = rand() % 100;
printf("%d,%#x\n", p2[i], &p2[i]);
}
// Manual memory release
if (p2 != NULL) {
free(p2);
// After the release ( The pointer still has a value ), Set the pointer NULL, Sign release complete
p2 = NULL;
}
Four 、 character string
4.1、 How to store strings
stay java Middle string String It's not modifiable , Can only be copied again .
stay c There are two expressions in the string :
A character array : Modifiable
Character pointer : Do not modify the
4.1.1、 A character array
There are three main ways for character arrays to represent strings
char str[] = {
'c','h','i','n','a','\0'}; //shina,0xbfeff2ba
char str[6] = {
'c','h','i','n','a' }; //shina,0xbfeff2ba
char str[10] = "china"; //shina,0xbfeff2ae
// You can modify
str[0] = 's';
printf("%s,%#x\n", str,str);
Initialization for string arrays that do not specify a length , Will read errors
// error shina\320\363\357\277\367,0xbfeff2bb
char str[] = {
'c','h','i','n','a'};
4.1.2、 Character pointer
// Memory is arranged consecutively
char* str = "how are you?";
*str = 'y';// Re assigning a value to a string pointer will result in an error
Use pointer addition , Intercepting string
str += 3;
while (*str) {
printf("%c",*str);
str++;
}
// are you?
Out of commission %s Output
printf("%s,%#x\n", str,str);//,0x3f9a
4.2、 String common functions
4.2.1、strcpy Copy
char *a = "china";
char dest[50];
// hold a Copy the string to dest Array
strcpy(dest, a);
4.2.2、strcat Splicing
// hold b String concatenation to a Back
strcat(dest, b);
printf("%s\n", dest);//china is powerful!
4.2.3、 Match lookup
strchr Look up a single character
Find the first match of a given character in a string
Only one character can be found
char* str = "I want go to USA!";
char *p = strchr(str, 'w');
if (p) {
printf(" Index position :%d\n", p - str);// Index position :2
}
strstr Check multiple characters
You can check multiple characters
char *haystack = "I want go to USA!";
char *needle = "to";
char *p = strstr(haystack, needle);
if (p) {
printf(" Index position :%d\n", p - haystack);// Index position :10
}
5、 ... and 、 Structure
data type : A structure is a type of construction data , Integrate different data types into a custom data type , similar java Medium bean.
Mode of operation : Structure is stack memory , Unwanted malloc, There is no need to release memory manually
struct Man
{
// member
char name[20];
//char* name;
int age;
//int (*func)();
};
5.1、 Initialize structure
type1: Assign values when building
struct Man m1 = {
"Jack", 21};//Jack,21
printf("%s,%d\n",m1.name,m1.age);
type2: Initialize the literal of the object
Be careful : Here for name The assignment of cannot be written m1.name = “Jack”; If the pointer string is modified, an error will be reported
struct Man m1;
strcpy(m1.name, "rose");
//sprintf(m1.name, "Jason");// Used to write formatted data to a string
m1.age = 23;
printf("%s,%d\n",m1.name,m1.age);//rose,23
type3: Assign a value from another structure
Structure is stack memory , here m1 And m2 Mutual isolation .
struct Man m2 = m1;
m1.age = 25;
printf("%s,%d\n",m2.name,m2.age);//rose,23
5.2、 Several ways of writing structure
type1: Named structures
struct Man{
char name[20];
int age;
} m1,m2={
" Zhang San ",19};
void main(){
strcpy(m1.name, " Li Si ");
m1.age=18;
printf("%s,%d\n",m1.name,m1.age);
printf("%s,%d\n",m2.name,m2.age);
}
type2: Anonymous structure
Control the number of structural variables ( Limited Edition ), Equivalent to a single case
struct
{
char name[20];
int age;
}m1;
type3: Nested structure
struct Teacher{
char name[20];
};
struct Student{
char name[20];
int age;
struct Teacher teacher;
};
void main(){
struct Student student1;
strcpy(student1.name," Zhang San ");
student1.age=19;
strcpy(student1.teacher.name," Miss li ");
printf("Student %s,%d Our teacher is %s\n",student1.name,student1.age,student1.teacher.name);
}
//Student Zhang San ,19 My teacher is Miss Li
type4: Structure nested anonymous structure
struct Student{
char name[20];
int age;
struct Teacher{
char name[20];
} teacher;
};
5.3、 Pointer and structure array
struct Man
{
char name[20];
int age;
};
5.3.1、 Traversal of structure array
struct Man
{
char name[20];
int age;
};
void main()
{
struct Man mans[] = {
{
"Jack", 20}, {
"Rose",19} };
struct Man* p = mans;
for (; p < mans + 2; p++)
{
printf("%s, %d\n", p->name, p->age);
}
}
//Jack, 20
//Rose, 19
5.3.2、 Calculate the length of the structure array
sizeof(mans) / sizeof(struct Man) //2
struct Man mans[] = {
{
"Jack", 20}, {
"Rose",19} };
for (int i = 0; i < sizeof(mans) / sizeof(struct Man); i++)
{
printf("%s, %d\n", mans[i].name, mans[i].age);
}
5.4、 Size of structure ( Byte alignment )
The size of the structure is exquisite 《 Byte alignment 》: The size of the structure variable , Must be an integer multiple of the widest base data type .
effect : It can improve the efficiency of reading
Basic information
4+8+ The whole number added =16
// length 16
struct Man
{
int age;
double weight;
};
void main()
{
struct Man m1 = {
20, 89.0};
printf("%#x, %d\n", &m1, sizeof(m1));//0xbfeff2b0, 16
}
4+4+4+4+4=20, No alignment required
// length 20
struct Man
{
int a;
struct Woman{
int b;
int c;
int d;
int e;
} f;
};
Brain burning
4+4+8=16, No alignment required
// length 16
struct Man
{
int age;
int height;
double weight;
};
4+8: First alignment , It needs to be supplemented 4
4+8+4: Second alignment , Add another 4
Finally 24
// length 24
struct Man
{
int age;
double weight;
int height;
};
5.5、 Structure and dynamic memory allocation
p->name="Jack";
// Here the arrow function is equivalent to
(*p).name="Jack";
struct Man
{
char *name;
int age;
};
void main()
{
struct Man* m_p = (struct Man*)malloc(sizeof(struct Man) * 10);
struct Man* p = m_p;
// assignment
p->name = "Jack";
(*p).name="afd";
p->age = 20;
p++;
p->name = "Rose";
p->age = 20;
struct Man* loop_p = m_p;
for (; loop_p < m_p + 2; loop_p++)
{
printf("%s,%d\n", loop_p->name, loop_p->age);
}
free(m_p);
}
//afd,20
//Rose,20
6、 ... and 、typedef Type takes alias
6.1、 Alias the basic data type
//Age int Alias for type
typedef int Age;
//Age int Alias for type pointer
typedef int* Ap;
6.2、 The structure takes an alias
struct Man
{
char* name;
int age;
};
typedef struct Man JavaMan;
typedef struct Man* JM;
Abbreviation
typedef struct Woman
{
char* name;
int age;
} W, *WP; //W yes woman Another name for the structure , WP yes woman Alias for structure pointer
Test structure alias 、 Structure pointer alias
void main()
{
JavaMan m1 = {
"Jack",20};
JM p = &m1;
// Structural variable
W w1 = {
"Rose", 20 };
// Structure pointer '
WP wp1 = &w1;
printf("%s,%d\n", w1.name, w1.age);//Rose,20
printf("%s,%d\n", wp1->name, wp1->age);//Rose,20
}
6.3、 If there is no name, it can be aliased
The structure can
typedef struct {
char* name;
int age;
} Student;
void main(){
Student student;
student.name="zhangsan";
student.age=18;
printf("%s,%d\n",student.name,student.age);//zhangsan,18
}
6.4、 Pointer member of struct function
Girl The structure is similar to Java Class in ,name and age Similar to properties ,sayHi It's similar to the method
struct Girl
{
char* name;
int age;
// A function pointer
void(*sayHi)(char*);
};
void sayHi(char* text)
{
printf("%s\n",text);
}
void main()
{
struct Girl g1;
g1.name = "Lucy";
g1.age = 18;
g1.sayHi = sayHi;
g1.sayHi("hello");
}
Use typedef Take the alias
typedef struct Girl
{
char* name;
int age;
// A function pointer
void(*sayHi)(char*);
} Girl;
typedef Girl* GirlP;
void sayHi(char* text)
{
printf("%s\n",text);
}
void main()
{
Girl g1 = {
"Lucy", 18, sayHi};
GirlP gp1 = &g1;
gp1->sayHi("Byebye");//Byebye
}
7、 ... and 、 Consortium
Different types of variables occupy a section of memory ( Cover each other ), Only one member of the joint variable exists at any time , Save memory
The size of the consortium variable = The number of bytes occupied by the largest member
union MyValue
{
int x;
int y;
double z;
};
void main(){
union MyValue d1;
d1.x=90;
d1.y=100;
d1.z=11.1;
// The last assignment is valid
printf("%d,%d,%lf\n",d1.x,d1.y,d1.z);
}
8、 ... and 、 enumeration
By default, the value will be assigned 123. therefore
enum Day
{
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6
};
// Equivalent to
enum Day
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
void main()
{
// Enumeration values , Must be a value in parentheses
enum Day d = Saturday;
printf("%#x,%d\n",&d,d);
}
Nine 、IO operation
9.1、 Read text file
fopen: Open file
fgetc: Reading documents ( Be careful :windows Medium fgetc And macos Medium fgetc Dissimilarity )
char *path="/Users/zhanglei/Desktop/NewFile.txt";
// open
FILE *fp=fopen(path, "r");
if (fp==NULL) {
printf(" File opening failure ...");
return;
}
/*windows Use this */
// // Read
// char buff[50];// buffer
// while (fgetc(buff,50,fp)) {
// printf("%s",buff);
// }
/*macos Use this */
char ch;
while ((ch=fgetc(fp))!=EOF) {
printf("%c",ch);// perhaps putchar(ch);
}
fclose(fp);
9.2、 Write text file
fputs: write file
char *path="/Users/zhanglei/Desktop/NewFile2.txt";
// open
FILE *fp=fopen(path, "w");
if (fp==NULL) {
printf(" File opening failure ...");
return;
}
char *text="[email protected], Chenghuacai , Learn the way 8\n Science and technology wealth center A";
fputs(text, fp);
// Closed flow
fclose(fp);
9.3、 File replication
fread: Read bytes
fwrite: Write Bytes
char *read_path="/Users/zhanglei/Desktop/NewFile.txt";
char *write_path="/Users/zhanglei/Desktop/NewFile2.txt";
// Read the file b The character represents the operation binary binary
FILE *read_fp = fopen(read_path,"rb");
// Written documents
FILE* write_fp = fopen(write_path, "wb");
// Buffer zone
char buff[50];
// The length of data read each time
int len=0;
while ((len=fread(buff, sizeof(char), 50, read_fp))!=0) {
// Write the read content to a new file
fwrite(buff, sizeof(char), len, write_fp);
}
// Closed flow
fclose(read_fp);
fclose(write_fp);
9.4、 Get file size
fseek(fp, 0, SEEK_END): Move the pointer to the end of the file
ftell(fp): Get pointer offset , That is, the file size
char *read_path="/Users/zhanglei/Desktop/NewFile.txt";
FILE* fp=fopen(read_path, "r");
// Reposition the file pointer
//SEEK_END end of file ,0 Offset
fseek(fp, 0, SEEK_END);
// Returns the current file pointer , Displacement relative to the beginning of the file
long fileSize=ftell(fp);
printf("%d\n",fileSize);//162
9.5、 Text file encryption and decryption
Exclusive or operation ( reversible )
The rules :1^1=0, 0^0=0, 1^0=1, 0^1=1 A fellow 0, Different for 1
fputc write in , Don't write it wrong here fputs
void cryptFun(char norma_path[],char crypt_path[]){
// Open file
FILE* normal_fp=fopen(norma_path, "r");
FILE* crypt_fp=fopen(crypt_path, "w");
// Read one character at a time
int ch;
while ((ch=fgetc(normal_fp))!=EOF) {
// write in ( Exclusive or operation )
fputc(ch^9, crypt_fp);
}
// Closed flow
fclose(normal_fp);
fclose(crypt_fp);
}
void main(){
char *norma_path="/Users/zhanglei/Desktop/NewFile.txt";
char *crypt_path="/Users/zhanglei/Desktop/NewFile_crypt.txt";
char *decrypt_path="/Users/zhanglei/Desktop/NewFile_decrypt.txt";
// encryption
cryptFun(norma_path,crypt_path);
// Decrypt
cryptFun(crypt_path,decrypt_path);
}
9.6、 Binary file encryption and decryption
When reading data in binary file , Read character by character
strlen: Measure string length , Need guidance #include <string.h> library
rb: Read bytes
wb: Write Bytes
void cryptFun(char norma_path[],char crypt_path[], char password[]){
// Open file
FILE* normal_fp=fopen(norma_path, "rb");
FILE* crypt_fp=fopen(crypt_path, "wb");
// Read one character at a time
int ch;
int i=0;
// The length of the password
int pwd_len=strlen(password);
while ((ch=fgetc(normal_fp))!=EOF) {
// write in ( Exclusive or operation )
fputc(ch^password[i++ % pwd_len], crypt_fp);
}
// Closed flow
fclose(normal_fp);
fclose(crypt_fp);
}
void main(){
char *norma_path="/Users/zhanglei/Desktop/liuyan.png";
char *crypt_path="/Users/zhanglei/Desktop/liuyan_crypt.png";
char *decrypt_path="/Users/zhanglei/Desktop/liuyan_decrypt.png";
// encryption
cryptFun(norma_path,crypt_path,"iloveyou");
// Decrypt
cryptFun(crypt_path,decrypt_path,"iloveyou");
}
Ten 、 precompile
C Language execution process
1、 precompile ( Preprocessing ): Prepare for compilation , Complete the replacement of code text ( Macro definition 、 Macro replace 、 Precompiling instructions )
2、 compile : Form object code (.obj)
3、 Connect : Associate object code with C Function library connection merge , Form the final executable
4、 perform
10.1、define Command function
Define signs
#ifdef __cplusplus // Identity support C++ grammar
Defining constants ( Easy to modify and read )
#define MAX 100
void main(){
printf("%d\n", MAX);//100
}
Definition “ Macro functions ”
You can dynamically replace function names
void dn_com_jni_read() {
printf("read\n");
}
void dn_com_jni_write() {
printf("write\n");
}
#define jni(NAME) dn_com_jni_##NAME();
void main()
{
// Replace :dn_com_jni_write();
jni(write);//write
}
10.2、 Log output example
Definition LOG、LOG_I、LOG_E Three macro functions
FORMAT// The first entry
...// Represents multiple parameters
__VA_ARGS__// Variable parameters ( stay windows The next variable may need to be added ##, namely ##__VA_ARGS__)
// Log output
//__VA_ARGS__ Variable parameters
#define LOG(FORMAT,...) printf(FORMAT, __VA_ARGS__);
// Logs will have levels
#define LOG_I(FORMAT,...) printf("INFO:"); printf(FORMAT, __VA_ARGS__);
#define LOG_E(FORMAT,...) printf("ERRO:"); printf(FORMAT, __VA_ARGS__);
void main()
{
LOG("%s,%d\n","Jason",18);//Jason,18
LOG_I("%s\n","message");//INFO:message
LOG_E("%s\n", "fire");//ERRO:fire
}
Upgraded version
Macro functions LOG_I、LOG_E Reuse the LOG Macro functions
#define LOG(LEVEL, FORMAT, ...) printf(LEVEL); printf(FORMAT, __VA_ARGS__);
#define LOG_I(FORMAT, ...) LOG("INFO:", FORMAT, __VA_ARGS__)
#define LOG_E(FORMAT, ...) LOG("ERRO:", FORMAT, __VA_ARGS__)
void main()
{
LOG(" error level :","%s,%d\n","Jason",18);// error level :Jason,18
LOG_I("%s\n","message");//INFO:message
LOG_E("%s\n", "fire");//ERRO:fire
}
10.3、 Define signs , Resolve repeated references
adopt #define AH To solve the repeated cycle include The problem of ( stay c The introduction of loops in the language will cause compilation errors ).
#ifndef AH
#define AH
#include "B.h"
#endif
Or concise grammar
#pragma once
Code example .MyCLanguage.c part
#include "A.h"
void printfA()
{
printf("print A");
}
void printfB()
{
printf("print B");
}
void main()
{
printfA();
}
A.h part
// If there is no definition AH, Definition AH
//#ifndef AH
//#define AH
//#include "B.h"
//#endif
#pragma once
#include "B.h"
void printfA();
B.h part
//#ifndef BH
//#define BH
//#include "A.h"
//#endif
#pragma once
#include "A.h"
void printfB();
边栏推荐
- 小程序毕设作品之微信小程序点餐系统毕业设计(2)小程序功能
- Detailed explanation of kernel function of SVM
- 网易云信音视频能力中台,全速助力银行业数字化转型升级
- Faith never dies, love when burning | Zhongchuang computing power participates in the hacker marathon
- Application of Worthington core collagen protease & Analysis of references
- Twelve kinds of wealth in life
- TiDB数据库
- Keep your Eyes on the Lane: Real-time Attention-guided Lane Detection
- PTH & PTT & PTK of Intranet penetration (domain control)
- Thoroughly uncover how epoll realizes IO multiplexing
猜你喜欢
随机推荐
Pager class
[FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes
小程序毕设作品之微信小程序点餐系统毕业设计(7)中期检查报告
小程序毕设作品之微信小程序点餐系统毕业设计(3)后台功能
Completable future asynchronous processing
ES 源代码阅读(一)「建议收藏」
php获取树形结构任意父节点下的所有子节点
B. Mark the Dust Sweeper(思维)
Faith never dies, love when burning | Zhongchuang computing power participates in the hacker marathon
Asp.Net Core CMD常用指令
第三十三基础:各种浏览器下的页面元素xpath获取方法
A page widgetization practice
乳酸脱氢酶(LDH)活性检测试剂盒 丨Abbkine详细说明书
【性能优化】MySQL性能优化之存储引擎调优
第三十二基础:页面元素定位方式xpath----轴定位方式
Digital triangle (Luogu p1216) basic DP
crontab定时任务通过脚本执行jar过程中,遇到jar包执行无效的坑
CIR 工业自动化雷达
【毕业设计】基于微服务框架的电影院订票管理系统
内网渗透之PTH&PTT&PTK(域控)