1,HelloWorld
<!-- Guide Package --> #include <stdio.h> <!-- main Function, argc: Number of parameters entered, argv: String array, each item represents a specific parameter --> int main(int argc, char *argv[]){ // Print printf("Hello World!\n"); return 0; }
2. VIM compile and run C program
<!-- Step 1: create a new one.c file --> vi helloworld.c <!-- Step 2: compile.c Documents( mac The following is used: clang compiler, linux The following is used: gcc Compiler) --> -g: output debug Information,-o: Output executable clang -g -o helloworld helloworld.c <!-- Step 3: run after successful compilation --> ./helloworld
3. Basic types commonly used in C language
type | explain |
---|---|
Integer: short, int, long | 16 bit 2 bytes, 32 bit 4 bytes, 32 bit 4 bytes |
Floating point type: float, double | 32-bit 4-byte, 32-bit 4-byte |
Character type: char | 8-bit 1 byte |
Unsigned: void | Generally, it is not used. It is only used when the pointer is used |
- Sample code
#include <stdio.h> int main(int argc, char *argv[]) { int a = 100; float b = 5.98; char c = 'Y'; printf("a=%d\n", a); printf("b=%f\n", a); printf("c=%c\n", a); return 0; }
4. Constants and variables
- Variable: a small space is allocated in memory, which can be changed at any time according to our needs.
- Constant: a small space is allocated in memory, which cannot be changed once assigned.
- int a = 0; // Variable, which can be re assigned
- const int len = 256; // Constant definition
- Memory management:
5. Pointers and arrays
- Pointer: memory address: void *, char*
- Array: char c[2], int arr[10]
- Example code:
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int *a, *b; a = (int *) malloc(sizeof(int)); b = (int *) malloc(sizeof(int)); *a = 1; *b = 2; int c[3] = {1, 2, 3}; printf("addr of a:%p, %p, %d\n", &a, a, *a); printf("addr of b:%p, %p, %d\n", &b, b, *b); printf("addr of c:%p, %p, %d, %d, %d\n", &c, c, c[0], c[1], c[2]); return 0;; }
6. Structure
struct st{ int a; //Member a int b; //Member b };
- Example code:
#include <stdio.h> struct st { int a; int b; }; int main(int argc, char *argv[]) { struct st sst1; struct st sst2; sst1.a = 10; sst1.b = 20; sst2.a = 30; sst2.b = 40; printf("struct content is :%d, %d\n", sst1.a, sst1.b); printf("struct content is :%d, %d\n", sst2.a, sst2.b); return 0; }
7. Enumeration type
enum em{ red_color = 0, green_color, black_color };
- Example code:
#include <stdio.h> enum e_type { red = 0, greed, blue }; int main(int argc, char *argv[]) { enum e_type et; et = red; printf("the color is %d\n", et); et = blue; printf("the color is %d\n", et); return 0; }
8. Arithmetic operation and comparison operation
- Arithmetic operation: [+, -, *, /,%]
- Comparison operation: [>, = =, <, > =, < =,! =]
- if/else statement
- for statement
- while statement
9. Function
void func(int a){ ... }
Code example:
#include <stdio.h> int sum(int a, int b) { return a + b; } int main(int argc, char *argv[]) { int result = sum(3, 5); printf("3+5=%d\n", result); return 0; }
10. File operation
- File type: FILE* file;
- Open file: FILE* fopen(path, mode);
- Close file: fclose(FILE *);
#include <stdio.h> int main(int argc, char *argv[]) { FILE *file; char buf[1024] = {0,}; file = fopen("1.text", "a+"); fwrite("Hello, World!", 1, 13, file); // Place the corner marker at the beginning of the file rewind(file); // Read the contents marked with 1 to 26 in the file content corner into buf fread(buf, 1, 26, file); fclose(file); printf("buf:%s\n", buf); return 0; }
11. Pointer in C language
-
Physical meaning of pointer?
- It is an address in memory
- Operation of pointer itself
- Operation of the content indicated by the pointer
-
How does the operating system manage memory?
- Stack space: each time we enter a function, the operating system will help us allocate a small space. When we exit the function, the operating control will help us reclaim this small space. (generally, the size is about 4M to 8M)
- Heap space: once you allocate heap space in a function, as long as you know its address, you can still access it in other functions; And the memory allocation and release of heap space are operated by yourself. (in general, the size is several G)
- Memory mapping: files used on the disk can be mapped into memory. After mapping, as long as we modify the files in memory, the files in the disk will change accordingly, which greatly improves our access to IO. (commonly used in databases)
-
Memory allocation and release?
- Allocated memory: void* mem = malloc(size);
- Free memory: free(mem);
- Note: both of the above are operated in heap space.
-
Memory leak?
- Constantly apply for memory from the system.
- The requested memory is different and will not be released.
- Avoid: if you apply for heap memory, be sure to release it.
-
Wild pointer?
- Occupying other people's memory is called a wild pointer. Generally speaking: this pointer has been released. After it is released, others will apply for memory and take it to others for use. At this time, you will take this pointer to access. In fact, this pointer is already a wild pointer, which is not controlled by you and belongs to others. If you still get this pointer to access, it will lead to program carsh.
-
Function pointer?
- Return value type (* pointer variable name) ([formal parameter list]);
int func(int x);/* A function declaration */ int (*f)(int x);/* Declare a function pointer */ f = func;/* Assign the first address of func function to pointer f */
- Example code:
#include <stdio.h> sum(int a, int b) { return a + b; } sub(int a, int b) { return a - b; } int main(int argc, char *argv[]) { int sumResult; int subResult; int (*f)(int, int); f = sum; sumResult = sum(3, 5); f = sub; subResult = sub(sumResult, 5); printf("3+5=%d\n", sumResult); printf("8-5=%d\n", subResult); return 0; }
12. Compiler
- gcc/clang -g -O2 -o test test.c -I... -L... -l
- -g: Debug information in output file
- -O: Perform Instruction Optimization on the output file (O1: no instruction optimization, O2: Instruction Optimization at level 2)
- -o: Output file
- -c: No output file is required
- -1: Specify header file
- -50: Specify library file location
- -l: Specify which library to use (note that this is a lowercase L)
- Compilation process?
- precompile
- compile
- Link, dynamic link / static link
- Example code:
Step 1: create a.c File vim add.c #ifndef __MY_LIBRARY__ #define __MY_LIBRARY__ int add(int a, int b){ return (a+b); } #endif // __MY_LIBRARY__ Step 2: compile but do not need to generate or delete files clang -g -c add.c Step 3: generate static library libtool -static -o libmylib.a add.o Note: in linux Generate static library under ar Command, for dynamic library gcc;Mac Next use libtool Step 4: write.h Header file and called.c file - .h Header file( add.h) #ifndef __MY_LIBRARY__ #define __MY_LIBRARY__ int add(int a, int b); #endif - Invoked.c Documents( testlib.c) #include <stdio.h> // Double quotation marks represent the priority of searching in the local directory. Of course, you can also use < > #include "add.h" int main(int argc, char* argv[]){ printf("add=%d\n", add(3, 3)); return 0; } Step 5: compile and specify the location of header file and library file, etc clang -g -o testlib testlib.c -I . -L . -lmylib - there.Represents the current file path - Another two-step implementation method is as follows: (the same as the previous step) - clang -g -c testlib.c - clang -o testlib testlib.c -I . -L . -lmylib Step 6: operation and results - function:./testlib - result: add=6
13. Debugger principle
-
The debuggers used under Linux and MAC are different. GDB is used under Linux and LLDB is used under Mac. Although they are different, their basic principles are the same.
-
Compile and output programs with debugging information (- g).
-
Debugging information includes: instruction address, corresponding source code and line number.
-
After the instruction is completed, call back to the debugger.
-
GDB/LLDB common commands:
Command | GDB | LLDB
—|---|—
Set breakpoint | b | b
Run program | r | r
Single step | n | n
Full implementation | c | c
Jump into function | s | s
Jump out of function | finish | finish
Print content | p | p
View the currently set breakpoint list | break list | break list
Exit | quit | quit -
Operation example:
Step 1: transfer testlib Load into debugger lldb testlib Step 2: set the breakpoint to main function b main Step 3: use various commands in the above list to operate by yourself r/s/finish/n/c/p wait... Step 4: view the generated information ls testlib* cd testlib.dSYM/Contents/Resources/DWARF dwarfdump testlib(use dwarfdump Tools (view)