array :
What is an array : Combination of variables , Is a way to batch define variables
definition : type Array name [ quantity ];
int num1,num2,num3,num4,num5;
int arr[5];
use : Array name [ subscript ];
subscript : Start from scratch Range :0~ quantity -1
ergodic : And for Cyclic combined use , Using circular variables i As the index of the array
initialization : type Array name [ quantity ] = {1,2,3,4,5,…};
1, Arrays, like ordinary variables, have random default values , For security, the array is initialized
2, This initialization syntax can only be used when defining statements , And can only be assigned one by one , Cannot assign values as a whole
3, Too much initialization data , The compiler generates a warning and discards excess data
4, Insufficient initialization data , The compiler automatically complements 0
5, Data can be omitted during initialization , Write braces only , Equivalent to initializing all members to 0
6, The length of the array can be omitted during initialization , The compiler will automatically count the number of array elements and tell the array
sizeof(arr)/sizeof(arr[0]) = Number of elements in the array
Total bytes of the array / Number of bytes of array elements = Number of elements in the array
Array out of bounds :C For the sake of program efficiency, the language does not check whether the subscript of the array is out of bounds Consequences of array out of bounds : 1, business as usual 2, Segment error 3, Dirty data
Two dimensional array :
A one-dimensional array is equivalent to arranging variables in a row , Access by number
A two-dimensional array is equivalent to arranging variables into a matrix , Accessed by row and column numbers
definition : type Array name [ Number of rows ][ Number of columns ]; int arr[3][5]; [0,0][0,1][0,2][0,3][0,4]
[1,0][1,1][1,2][1,3][1,4] [2,0][2,1][2,2][2,3][2,4] use : Array name [ Row subscript ][ Column subscript ]; arr[1][2]
Row subscript : 0~ Number of rows -1 Column subscript : 0~ Number of columns -1 ergodic : Need and double layer for Cyclic fit , The outer layer is generally responsible for traversing rows , The inner layer is generally responsible for traversing columns for(int i=0; i< Number of rows ;
i++) { for(int j=0; j< Number of columns ; j++) { printf("%d ",arr[i][j]); } printf("\n"); }
initialization : type Array name [ Number of rows ][ Number of columns ] = {{ first line },{ Second line },{ Third line },...}; practice 4: Define a 5*5 And initialize , Find the coordinates of the maximum value in the array
practice 5: Define a 5*5 And initialize , Find the coordinates of the minimum value , Calculate the sum of the numbers around the location ?
Variable length array :
When defining an array, use variables as its length , The length of the array is uncertain during code compilation , When the array definition statement is executed, its length is determined , Once determined, it cannot be changed
advantage : The length of the array can be determined according to the actual situation , Achieve the purpose of saving memory
shortcoming : Because initialization is completed at compile time , At this time, the length of the variable length array is uncertain , Therefore, it cannot be initialized
Binary conversion :
Why binary , octal number system , hexadecimal ?
Because of the present CPU Only high and low levels can be recognized , Only binary data can be used for calculation
Although binary can be calculated directly by computer , But it's not easy to write , record , Therefore, the data is recorded in the file in octal for convenience
along with CPU The number of digits is increasing , Octal cannot meet the demand , So hexadecimal was developed to represent data ,
Due to historical reasons, octal cannot withdraw from the historical stage
Decimal to binary :( Decimal to other decimal )
Remainder method : use 2 Data remainder , Then continue to seek surplus from the business , Know Shang Wei 0 end , The remainder generated in the process is the binary of the data ( Reverse order )
n %2 remainder
merchant %2
…
127 %2 1 63 %2 1 31 %2 1 15 %2 1 7 %2 1 3 %2 1 1 %2 1 0 0 Binary :01111111 Right seeking method :
data - 2^(n-1) If you can reduce , Then the second n Bit 1, Otherwise 0 137 128 64 32 16 8 4 2 1 1 0 0 0 1 0 0 1 Manual calculation : 79
28 63 119 practice 1: Enter a positive integer m, Displays the of the data n Base system (n>=2), If n>=10, go beyond 10 The numbers are represented by letters 10 A 11 B
Binary to decimal :( Other decimal to decimal )
Binary data ride 2^(n-1) Summation of results
10011101 128+16+8+4+1 157
Binary to octal :
Three binary bits are converted to one octal bit
Binary 10 011 001 101 110
octal number system 2 3 1 5 6
Binary to hexadecimal :
Four binary bits are converted to one hexadecimal bit
Binary 10 0110 0110 1110
hexadecimal : 2 6 6 E
stay C In code , with 0 The data at the beginning is octal data , with 0x The beginning is hexadecimal data %o Display data in octal form %x Display data in hexadecimal %#o %#x Display the data in the corresponding hexadecimal
Original code , Inverse code , Complement :
Original code : Binary of data
Inverse code :
The original code of a positive number is its inverse code
The inverse code of a negative number is that the sign bit of its original code remains unchanged , Other bits are inversed by bit
Complement : Data is stored in memory in the form of complement
The original code of a positive number is its complement
The complement of a negative number is its inverse +1
Complement of negative numbers :
1, Convert data to binary
2, Binary sign bit invariant , The rest are inversed by bit to obtain the inverse code
3, Inverse code +1 Get complement
-127
1111 1111
1000 0000
1000 0001 Complement
0000000000000000000000001000 0001 %d --127->129
Complement to data : Unsigned complement is directly converted to decimal data The highest sign is 0, The description is positive , It is also directly converted to decimal data Signed and highest bit is 1, The description is negative 1, Complement -1 Get inverse code
2, Inverse sign bit unchanged , The original code is obtained by bit inversion 3, Convert original code to decimal 11111111 Complement 11111110 Inverse code 10000001 Original code -1 Maximum +1 =
minimum value minimum value -1 = Maximum
Bitwise Operators :& | ~ ^ << >>
A & B By phase and
01101010 0x6A
01110110 0x76
01100010 0x62
A | B By phase or
01101010 0x6A
01110110 0x76
01111110 0x7E
~A Bitwise negation
01101010 0x6A
10010101 0x95
A^B Bitwise XOR Same as 0, The difference is 1
01101010 0x6A
01110110 0x76
00011100 0x1C
A<<n hold A The complement of moves to the left n position , Left discard , Right complement 0
01101010 0x6A << 4
10100000 0xA0
A>>n hold A The complement of moves to the right n position , Right discard , Left complement sign bit
11101010 0xEA >> 3
11111101 0xFD
practice 2: Enter an integer n, Take it 4~7 Bit set to 1010, Other bits remain unchanged 4 Bit first and 0 , Again or 1010 printf("%d\n",n & ~(0xf<<4)
| (0xA << 4)); 00000000 00001111 00000000 11110000 11111111 00001111 xxxxxxxx
0000xxxx 0xA << 4 00000000 10100000 xxxxxxxx 1010xxxx
function :
Is a piece of code with a certain function , yes C The smallest unit of management code in a language
Encapsulate the code into functions , It is easy to manage and call code
Classification of functions :
Standard library functions
C Language Standards Committee C Language provides some basic functions in the form of functions , Encapsulated in libc.so library , Added by default , Therefore, you need to include header files when using , In function name ( parameter ) To call the function
int isalnum(int c);
int isalpha(int c);
int isdigit(int c);
int islower(int c);
int isupper(int c);
int isxdigit(int c);
int toupper(int c);
int tolower(int c);
int abs(int j);
The following functions are encapsulated in libm.so Math library , When using, you need to add parameters at compile time -lm double sqrt(double x); double pow(double
x, double y); double ceil(double x); double floor(double x); double fabs(double
x); time_t time(time_t *t); function : Return from 1970-1-1 0:0:0 How many seconds have elapsed to runtime int system(const
char *command); function : Call system command system("clear"); int rand(void); function : Returns a random number void
srand(unsigned int seed); function : Random seed , Set the position of random number practice 3: obtain 10 individual [100,1000] Random number in range num =
rand()%901+100 [a,b) rand()%(b-a)+a practice 4: Randomly give a winning number of two-color ball Red ball 6 individual :1-33, Cannot repeat rand()%33+1
blue ball 1 individual :1-16 rand()%16+1 System function System function is not a function , Only some functions provided by the operating system in the form of function interface
memory management , signal processing , file IO, file management , Process management , Process communication , Thread management , Thread synchronization , Network Communications Third party library function Fees provided by third parties , Open source library functions github MD5
XML JSON Custom function : For easier code management , Reduce redundancy and encapsulate code into functions be careful : A function should be controlled as far as possible 50 Line left and right , One function, one function
Function declaration : The purpose of function declaration is to tell other code the calling format of the function return type Function name ( type 1 Variable name 1, type 2 Variable name 2...);
1,C Function names in languages are generally all lowercase , Separate with underline 2, If no parameters are required, it is recommended to write void, Don't leave it empty 3, If no return value is required, write void
4, If there is a function definition before the call , Then the function declaration can be omitted Implicit declaration :
When a function is called, there is no function declaration or definition , The compiler guesses the format of the function , The parameter list will guess based on the data provided at the time of call , The return value is guessed as int type How to avoid : Before calling , Provides a declaration or definition of a function
Function definition : return type Function name ( type 1 Variable name 1, type 2 Variable name 2...) { Function body ; return val; }
be careful :return Function of statement :1, Returns a value to the caller 2, End function execution function call : Function name ( Argument );
The return value is placed at the location of the call , It can be recorded with variables , It can also be displayed directly , If not, the record will be lost Function parameter transfer : 1, Data is transferred between arguments and formal parameters in the form of assignment ( One way value transfer )
2, Parameters defined within a function , Only belong to the function where it is located , This function cannot be used in the
3,return Statement actually places data in a common area ( Functions and callers share ), If you don't write return sentence , What the caller gets from this area is a random piece of garbage data
4, When an array is used as an argument to a function , Length will be lost , Additional length parameters are required to pass the length 5, The array is passed " Address transfer ", Functions and callers of functions can share arrays
practice 5: Implement a function , Find the maximum value in the array practice 6: Implement a function , Sort arrays
practice 7: Implement a function , Finds whether a value exists in the array , If it exists, the index of the data in the array is returned int find_arr(int arr[],int len,int
val);
After class practice
1, Implement a function , Determine whether an integer is prime , Call it to display 100~10000 All primes between
#include<stdio.h> int issushu(int n) { int i,m=1; for(i=2;i<n;i++) { if(n%i==0)
{ m=0; } } if(m==1) { printf("%d ",n); } } int main() { int i; for(i=100;i<=
10000;i++) { issushu(i); } return 0; }
2, Enter two dates (yyyy-mm-dd), Calculate the number of days between two dates
#include<stdio.h> int runyear(int year) { if(year%400==0||year%4==0&&year&100!=
0) { return 1; } else return 0; } int day(int *day1,int *day2) { int month[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31}; int isdays=0,i; int *tmp; if(day1[0]==
day2[0]) { if(day1[1]==day2[1]) { isdays=day1[2]-day2[2]; isdays=(isdays<0)?-
isdays:isdays; } else { if(day1[1]<day2[1]) { tmp=day1; day1=day2; day2=tmp; }
for(i=day2[1]+1;i<day1[1];i++) { isdays+=month[i]; } isdays+=month[day2[1]]-day2
[2]+day1[2]; if(day2[1]<=2&&day1[1]>2) { if(runyear(day1[0])) isdays++; } } }
else { if(day1[0]<day2[0]) { tmp=day1; day1=day2; day2=tmp; } for(i=day2[0]+1;i<
day1[0];i++) { if(runyear(i)) isdays+=366; else isdays+=365; } for(i=day2[1]+1;i
<=12;i++) { isdays+=month[i]; } isdays+=(month[day2[1]]-day2[2]); if(day2[1]<=2)
{ if(runyear(day2[0])) isdays++; } for(i=1;i<day1[1];i++) { isdays+=month[i]; }
isdays+=day1[2]; if(day1[1]>2) { if(runyear(day1[0])) isdays++; } } return
isdays; } int main() { int isdays; int i=0; int day1[3],day2[3]; printf(
" Please enter two dates :\n"); scanf("%d-%d-%d",&day1[0],&day1[1],&day1[2]); scanf("%d-%d-%d",&
day2[0],&day2[1],&day2[2]); isdays=day(day1,day2); printf(" differ %d day ",isdays);
return 0; }```
Technology