Linux C Advanced



Uniform declaration:

Blog reprint statement:Part of the content of this blog comes from the Internet, books, and various manuals.

         the purpose of content is to facilitate query, summary, backup and open source sharing.

           the source of some reprinted contents is indicated. If there is infringement, please contact the blog and delete it. Thank you!

Baidu cloud disk extraction code:Unified extraction code:ziyu

1、 Linux

1.1 embedded

Embedded: computer application centered system with tailorable software and hardware.

1.2. What is Linux

  • Linux is a multitasking and time-sharing operating system
  • GNU GNU is Not Unix It is an organization;
  • GPL It’s an agreement.

1.3 Linux distribution

  • Linux itself refers to aOperating system kernel, only the kernel is unusable.
  • Our system uses an operating system that contains a kernel and a collection of useful applications, which is called Linux distribution.

    Ubuntu and RedHat are different distributions of Linux.
  • Red Hat The server
  • unbutu Good desktop performance “Humanitarian”.
  • debian

1.4 Linux architecture

-------------------------------------------- 	 [0-3G] 	 0XC000 0000

1.5 virtual 4G memory

Please add a picture description

------------- 	 4G

1.6 shell command

whoami 	 Who am I?

1.7 software package management

1.7.1 dpkg: (offline installation)

rxvt        _ 1%3a2.6.4-14               _ i386      .deb

Package installation:

  sudo dpkg -i Full name of software package


   sudo dpkg -r Software name

To clear a package’s configuration file:

  sudo dpkg -p Software name

List of information related to the software:

  sudo dpkg -L Software name

Query version number:

  sudo dpkg -l Software name

Query the status of software installation package:

  sudo dpkg -s Software name

Forced installation:

  sudo dpkg -f Software name

Virtual machine custom scaling window size

  virtual machine — > Preferences –> Display;

  update vmare tools

     click virtual machine — > Update (reinstall) VMware Tools

       jump to an interface with the compressed package VMware tools-10.3.10-13959562.tar.gz,

     unzip the installation package in the home directory tar -xvf VMware tools-10.3.10-13959562.tar.gz (press enter until the end)

       generate a directory — VMWare_ tools_ Distrib, CD into this directory, and then execute. / vmare_ Install. PL is the executable file;

1.7.2. Copy Windows files to Ubuntu

(1) . dragging method:

   directly drag the file to the terminal and add MV to move it to a path (MV ‘/ TMP / VMware DND / vuaytt / char_arr.c’. / my. C)

    or right-click copy on widgets, and then right-click on the terminal on Ubuntu — > Paste filenames will also have a path. Similarly

  add MV before to move to the current; ( mv ‘/tmp/VMwareDnD/VUayTT/char_arr.c’ ./)

(2) , create shared folders

   virtual machine – > Settings – > Options – > shared folder – > always enable – > select a folder in windows as a shared folder

  terminal access: CD / MNT / HGFs / myshare

Failed to set shared folder. Problem solved: property, set compatibility; Virtual machine key.

1.8. Graphical interface configuration and distribution network

1.8.1. Edit — > network editor — > Change settings — > bridge mode — > select corresponding network card — >

The virtual machine -- > sets -- > bridging mode.

1.8.2、apt External network required

Software source:


Download path:


Update source:

   sudo apt-get update

Download a software:

  sudo apt install software name:


  sudo apt get remove software name


  sudo apt get clean software name

Download source code:

  sudo apt get source software name

1.9 compression and archiving of documents

1.9.1 compression

For files

1.9.2 decompression

gunzip 1.c.gz 	-->	1.c
bunzip 1.c.bz2	-->	1.c
unxz 1.c.xz		-->	1.c

1.9.3 archiving

Archive: for a directory 	 "Turn a directory into a file"

1.9.4 the essence of compression is archiving and compression


1.10 contents related to documents

1.10.1. VI three modes

New file VI toch

(1) , command line mode

Copy: NYY

(2) , insert mode

a 	/	i 	/	b

(3) . bottom line mode


1.10.2. Viewing documents

cat head tail  	 Nore less

1.10.3. > redirection

cat 1.c > 2.c 	// Putting the contents in 1. C into 2. C is equivalent to copying

1.10.4 grep search

Grep "string" parameter file


Find path – name file name


cut -d : -f 1,2 filename

1.13. Modify file permissions, users and groups.

ls -l

-Rw-rw-r -- 1 farsight farsight 18672 June 15 08:48 4. Linux_ Advanced. C
LS - L list attributes
r:4 	 w:2 	 x:1 		 Maximum authority: 777

(1) Modify permissions:

The first method is to modify permissions 		***
The second method to modify permissions: 		

(2) Modify user:


1.14. Soft link and hard link

1.14.1. Soft link: equivalent to Windows shortcut

Create soft connection file: ln - s source file name symbolic connection file name

1.14.2 hard connection: alias the file

Ln source file name connection file

1.15 wildcards

* : 	 Match all characters

1.16 process

An execution of a program


ps -aux

ps -aux | grep a.out Query a.out this process

Top dynamic detection process

kill 4797 // Kill process number — > Kill a process

1.17 usage of man query function, library function and command

man -a  		 Query all

1.18 user management

1.18.1. Add user

adduser 	 user name

1.18.2 switching users

Su user name

1.18.3. Delete user

Deluser user name

1.18.4 shutdown and restart

sudo shutdown -h now 		 Shut down immediately


Sudo usermod - Ag farsight mytest adds the user mytest to the farsight group;

1.18.6. Change password

sudo passwd user1 	 Set the password for the user named user

1.19 disk related commands

1.19.1. View disk related information

sudo fdisk -l

  /dev/sda: System disk –> Disk /dev/sda: 21.5 GB, 21474836480 bytes

1.19.2. View disk partition information

df -h

1.19.3. Mount USB flash disk on Ubuntu: USB/2.0 three

Virtual machine -> Removable device -> flush disk

Create in home directory: MKDIR ~ / Udisk

Mount: sudo mount /dev/sdb1 ~/udisk

You can enter the Udisk to access the U disk:


   sudo umout /dev/sda1 ~/udisk

1.20 network knowledge

Domain name resolution server:

1.21 environmental variables

env 		 View environment variables

2、 Shell

2.1 essence: an ordered collection of shell commands

Put the shell script in a file, then this file is the shell script

2.2 shell command line interpreter




bash –> ubuntu

2.3 shell script

2.3.1. The suffix is. Sh

Eg: (1) create a directory mydir in the home directory

2.3.2 shell script is an interpreter language and does not need to be compiled

2.3.3 operation

(1) Create script : vi

Add at the beginning: #/ bin/bash

(2) Modify permissions:

chmod 777 Modify to executable file

(3) Execute script


2.4. Shell script variables

2.4.1 definition of variables

There is no data type, and it does not need to be defined in advance for direct use;

When assigning a value to a variable, there must be no spaces around the equal sign

You do not need to add ‘;’ to each line of statement

See the meaning of variable name;

The default value assigned to variables is string;

2.4.2 reference of variables



2.4.3 when assigning values to variables, double quotation marks and single quotation marks can be added or not:

The difference between single quotation marks and double quotation marks:

2.4.4. Delete variable: after a variable is deleted, it cannot be used again

unset var1 		

2.4.5 command replacement

The command is assigned to a variable

2.4.6 position variables

$0 	$ one 	$ two 	$ 3...

2.4.7 shell processing string

(1) Copy of string


(2) , string connection


(3) . length of string

echo $(#string)

(4) . string extraction

strstr()      str1[] = "hello world"  str2[]= "world"

2.5. Shell processing array

Array definition:

Arr = (1 2 3 4) assign values to the array in turn; The subscript of the array also starts from 0.

arr=([0]=aaa [2]=bbb) For some elements in the array

Note: the place not assigned is empty (not zero).

2.5.1. Array reference: the subscript of the array also starts from 0

${array name [subscript]}

2.5.2. Reference the whole array

${arr[*]} Or ${arr [@]}

2.5.3 find the length of array elements

echo ${#arr1[0]} // Arr1 [0] length of the entire array

2.5.4 find the number of assigned elements in the whole array

echo ${#arr1[@]}

2.5.5 empty array

unset arr[0] Empty this element

unset arr[*] Empty the entire array

2.5.6. Arr = (${arr [*]} “how are you”) add content after the array

Arr = (“hello” ${arr [*]}) add content to the array


2.6 read input from the terminal

Read variable

2.6.1. If two variables are entered

Usage: read VAR1 var2

2.6.2. Read – P “prompt” var // Enter something suggestive

2.6.3、read -a arr (array name) // Enter an array

Read – P “please enter an array” – a arr / / read – a arr

echo ${arr[*]}

2.6.4、read -n num var

read -n 5 var // Enter 5 characters and exit directly

2.6.5、read -t Second var // Enter within the specified time, and exit automatically after timeout

2.6.6、read -s var // The entered is not echoed


2.7 shell input

echo $var

2.8 shell operation

+ 	-	*	/	%		

2.8.1、(( ))


((expression 1, expression 2, expression 3)) The result of executing the last expression

Value: $((expression))

(1) When taking variables in the expression, you can add $, or not;

2.8.2、$ [ ]

RET = $[expression]

RET = $[expression 1, expression 2, expression 3,……]

(1) You need to assign the execution result to a variable


(1) It supports integer operation and string related operations

2.8.4 string operation

Matching string:

expr match $var “sub” // Match the string from the beginning, and return as many numbers as there are the same characters


2.9、if else

If [condition]

2.9.1 number size judgment

-eg  	 be equal to

2.9.2 logical judgment

&& 	-->		- a

2.9.3 judgment of string

-n Judgment string is not empty

-z The judgment string is empty

= Judge string equality

!= Unequal judgment strings

> Compare the size of the string


if test -n “$var1” -a -n “$var2”

1 parameter in front

2. The reference of variable needs to be added with “”, otherwise an error will be reported when the input is empty.

Exercise: input two strings from the terminal to judge whether they are empty. If not, compare the size;

2.9.4 document judgment


-b Determine whether the file exists and whether it is a block device file

-s Determine whether the file exists and whether it is a socket file

-p Determine whether the file exists and whether it is a pipeline file

-f Judge whether the file exists and whether it is an ordinary file

-c Determine whether the file exists and whether it is a character device file

-L Judge whether the file exists and whether it is a linked file

-d Determine whether the file exists and whether it is a directory file

-e Determine whether the file exists,

-s Judge whether the file exists and whether the file is empty. If the file size is greater than 0, return the true value.

Exercise: enter a file name. Determine whether it is an ordinary file or a directory file

2.9.5 judging the authority of documents

-w  	 Judge write permission

2.9.6 time stamp of judgment document

-nt Compare the timestamp updates of two files

-ot Compare two files with older timestamps

Exercise: enter a file name and judge whether you have write permission. If so, write HelloWorld in this file. If you don't have add permission, write it in again;
Exercise: enter two file names to determine the timestamp

2.9.7 case expression in

Parameter 1
Eg: echo 'enter a number between 1 and 4:'
* 	-->	 All
Exercise: simulate the process of downloading a software

2.10 circulation: While [condition]

Exercise: write an addition calculator, input a value to accumulate all the time, and enter exit to exit;
Usage 1:
Usage 2:

2.11 function

General form of function:

Function function name ()



(1) Shell function has no formal parameters and no return value (it can also have)
Function call:
	function add(){
	echo $sum
	function add1(){
	add1 50 60
	echo $sum

	function add2(){
		local sum=$(($1+$2))
		return $sum
	add2 $1 $2
	echo $?

3、 C Advanced

3.1 essence of C language

Operating virtual memory

3.2 how to allocate memory in C language

3.2.1 data type

(1) . basic data type

int float char short long double

(2) User defined data types

Array: continuously open up space of the same data type in memory

(3) . pointer type

(1) Basic usage

3.2.2 programmers manually open up memory space

malloc free

#include //Malloc contains header files 					 			 

3.3 function

3.3.1 function form

Return value function name (parameter)



3.3.2 function parameter transfer

pass by value

Address delivery

3.3.3 function encapsulation

Main. C: main function, function call

Fun. C: various methods

Fun. H: function declaration, structure

3.3.4 pointer function: the return value is a function of an address

3.3.5 function pointer: in essence, it is a pointer pointing to a function

VI specify copy: Shift + mouse selection

3.3.6. Function pointer array: essentially, it is an array that stores a function pointer.

3.3.7 recursive function: call yourself. Note that there must be termination conditions.

3.3.8 callback function: ordering, waiter, chef

Callback function modelPlease add a picture description


3.4.1. Compile the code with GCC

  • 1>Pretreatment

    GCC – E 1. C – O 1. I preprocessing: used to replace all header files and macro definitions with their real contents. After preprocessing, the text file is still obtained, but the file volume will be much larger.

  • 2>compile

    GCC – S 1. C – O 1. S generates assembly language source code files

  • 3>assembly

    GCC – C 1. C – O 1. O converts the assembly code of the previous step into machine code in binary format

  • 4>link

    GCC 1. C generates executable files and links multiple target files and required library files (. So, etc.) into the final executable files

3.4.1. Use GDB to debug the code

You need to add a – g parameter when compiling

gcc 08malloc.c -g

Running programs using GDB

gdb a.out

Get help:


view code

L the first 10 lines are displayed by default

L you can look down at the following 10 lines

L – display 10 lines forward

Add Breakpoint

B line number

How to view breakpoints

info b

Remove Breakpoint

Delete breakpoint number

Run code:

R runs the code and stops at the breakpoint

View data

P variable

Watch variable

Single step operation

N single step operation. When a function is encountered, the function execution process will not be displayed

S single step operation. When a function is encountered, the function execution process is displayed

Continue running

C continues to run downward without error and stops at the breakpoint

Exit debugging:

Q exit

3.5 storage type

Storage type (6)auto static register extern volatile const

3.5.1 Auto: when defining variables, the default storage type is auto.

Global and local variables:

#include int x=10;

3.5.2. Extern: it can be used to declare that variables or functions are in other files

extern int a, b;
extern int fun(int , int );	

int main()
	printf("a = %d\n", a);
	printf("b = %d\n", b);
	printf("a+b = %d\n", fun(a,b));

int a = 12, b = 13;

#include int fun(int a, int b)
	return a+b;


(1) . prolong the life cycle of variables

#include void func(void)

(2) . scope limitation (the object can be a function or a variable)

Variables or functions decorated with static can only be used in the current file

3.5.4 const: read only variable

const int a=10;

a=100; // Error, const modified variable is not allowed to be modified

(1) , const decorated local variable — > can change the value stack area

(2) . const modifies the global variable — > the read-only area where the value cannot be changed

#include const int b=500; // Read only area

(3) , const modifier pointer

#include int main(int argc, const char *argv[])

3.5.5 register: (register)

register int a;

Variables of type register defined are executed faster than those without register.

Question 1: the execution rate of register type variables is fast. Can all variables be defined as register type variables?

Answer: No, there is a limit on the number of registers in the chip. For example, there are only 40 registers on the A53 hardware platform.

Question 2: can const and volatile be used simultaneously when defining variables?

Answer: Yes, read-only status register

3.5.6 volatile:

volatile int a;

(1) , compiler optimization-o3

volatile int *a=&b; // Prevent the compiler from optimizing the code

*a = 110;

*a = 120;

(2) Prevent the CPU value data from being fetched from the cache register

The variable decorated with volatile is fetched from memory every time

(3) When variables need to be defined in the development process, volatile needs to be added

  1. When defining the hardware address. (e.g. status register)

   2. Volatile needs to be added when the non automatic variable accessed in the interrupt processing function

  3. The same variable accessed by multiple threads

For example, write the value 0x12345678 to the address 0x80000000

volatile unsigned int *p=(volatile unsigned int *)0x80000000


4、 Makefile

4.1. What is makefile

A file to manage the project

Makefile can show whether you have the development ability of large projects

4.2. What is make

It is a project manager to manage more files

Is an executable program that calls the compiler to compile by reading the contents of makefile

(the essential work is still being done by GCC)

Compile the file according to the file timestamp (file update time)

Only the modified files are compiled instead of all, which saves time.

Makefile is the only configuration tool read in by make

4.3. Experience makefile

All: / / --- label

4.4 rules of makefile

Target: dependent files


out:main.o fun1.o fun2.o
	gcc main.o fun1.o fun2.o -o out
	gcc -c main.c -o main.o
	gcc -c fun1.c -o fun1.o	
	gcc -c fun2.c -o fun2.o

4.5 variables

Save complicated operation and facilitate replacement

Similar to variables in shell scripts




more details makefileReference documentsLearn makefile with meBaidu cloud download link:

Jump: previous article, linux c foundation!

To be updated…

Jump: next, data structure and algorithm!

Jump: next, data structure and algorithm!

Jump: start

Recommended Today

Appium foundation – 28. Operation mode of WebView

catalogue 1. First understand what hybrid is 2. Identify WebView 3. Context context 4. Switching between WebView and native PAGE 5. Comprehensive exercise All operations we mentioned before are operations on native pages. In the mobile app, in addition to the native page, there areWebviewPage (i.e. H5 page), let’s talk about rightWebviewPage operation. 1. First […]