c language learning notes (continuously updated)

Posted by sgt.wolfgang on Fri, 04 Mar 2022 20:35:08 +0100


One c file can only have one main function; There can be more than one in a project c documents.

1. Data type

charCharacter data type
shortShort
intinteger
longLong integer
long longLonger integer
floatSingle-precision floating-point
doubleDouble precision floating point number

View data type bytes

Unit in computer:

Bit - bit byte - byte = 8bit kb - 1024byte mb - 1024kb

gb - 1024mb

2. Variables and constants

2.1 variables

Variable classification: local variable, global variable

//Global variable - {} external definition
int a = 100;
int main()
{
	//Local variable - {} internal definition
	//When the names of local variables and global variables conflict, local variables take precedence
	//It is not recommended to write the names of global variables and local variables the same
	int a = 10;
	printf("a = %d\n",a);//a = 10
	return 0;
}

2.1.1 scope and life cycle

In addition, you can use two c file, just declare variables

life cycle
The life cycle of a variable: the time period between the creation and destruction of a variable
Life cycle of local variables: the life cycle of local variables starts when entering the local scope and ends when leaving the local scope
Life cycle of global variables: the life cycle of the whole program

2.2 constants

2.2.1 literal constants


It has no actual meaning, but it exists.

2.2.2 const modified constant

A is a variable. After defining a=20, print out a=20

int main()
{
	//A is a constant variable - with constant attributes (attributes that cannot be changed)
	const int a = 10;
	printf("%d\n",a);
	return 0;
}

After being modified by const, a cannot be modified

**Myth: * * constant is required when creating an array. In this case, int n = 10;n is a variable. Even if const is used and N is a constant variable, the essence of n is still a variable, so an error is reported.

2.2.3 #define defined identifier constants

#include <stdio.h>
#define min 100
int main()
{
	int a = min;
	//min = 2000;// Identifier constants cannot be modified
	printf("a = %d\n",a);
	return 0;
}

2.2.4 enumeration constants


You can assign the initial value. When you assign 2, it is 2, 3 and 4

3. String

Definition: a string of characters caused by double quotation marks

End with string flag \ note:

The string of arr1 is abc ABC \ 0.. There is \ 0 after printing to c. printing will not continue
The number after arr2 prints abc is unknown. Continue printing
After manually adding a \ 0, we fully prove that \ 0 is the end flag of the string

When we calculate the length of a string, \ 0 is not the content of the string, but just a flag.

An error will be reported when introducing strlen. Just insert it in front

#include <string.h>


At this time, the length of arr1 is 3 and the length of arr2 is a random value

4. Escape character

#include <stdio.h>
int main()
{
	printf("(are you ok??)"); //??)  -  ]   Three letter word
    //printf("are you ok\?\?)");// Use \ escape, so? Can be successfully printed out
	return 0;
}//Some compilers cannot display three letter words
printf("%d",100);Print integer
printf("%c",'a');//Print character
printf("%s","ab");//Print string


String length

\t an escape character, count one \ 32 count one, length 14

#include <stdio.h>
#include <string.h>
int main()
{
    printf("%d\n",strlen("c:\test\328\test.c"));
	return 0;
}

4.1 binary conversion

(1) Convert decimal to octal

[1].67D=101Q

67 = 26 + 20 = 1 000 001 = 1 0 1 (convert to binary first)

1000000 1

[2] . 10.68d = q (accurate to 2 decimal places)

In the integer part, according to the method of [1], the decimal part is accurate to a few digits after the decimal point, the decimal part is multiplied by 8 several times, and then read from top to bottom.

10D=23+21=1 010=12Q

0.68 * 8 = 7.44, take 7

0.44 * 8 = 3.72, take 3

10.68D=12.73Q

(2) Octal to decimal

130Q=88D

1 3 0 =1 *8^2+3 *8^1+0 *8^0=88

(3) Hex to decimal

23daH=D

23da=2 *16^3+3 *16^2+d *16^1+a *16^0=9178D

(4) Convert decimal to hexadecimal

27.68h = D (accurate to 2 decimal places)

27=24+23+2^1=1 1010=19

Decimal part 0.68 * 16 = 10.88, take a

0.88 * 16 = 14.08 take e

27.68H=19.aeD

5. Array

Definition: a collection of elements of the same type

Create array

int n = 10;
int arr[n] = {0};//This method is incorrect
//To create an array is to specify a constant size

//When entering an array, no&
scanf("%s",arr);//The array name is an address, but there is no&
char arr[7] = {'a','b'};//Incomplete initialization, remaining defaults to 0

C99 standard introduces a concept: become an array

It supports specifying the size with variables when creating an array, but the array cannot be initialized; Some compilers support this operation.

int n = 10;
int arr[n];

6. Function

6.1 what is a function

6.2 function classification

(1) Library function; (2) Custom function

6.2.1 library functions

When the compiler is released, it carries some library functions. In practice, some operations that occur frequently and in large numbers do not need to be implemented, which improves our efficiency.

https://www.cplusplus.com/
https://msdn.itellyou.cn/


Take strcpy library function as an example

#include <string. h> / / call header file
int main()
{
	char arr1[20] = {};
	char arr2[] = {"abc"};
	strcpy(arr1,arr2);//strcpy is the copy value
	printf("%s",arr1);
	return 0;
}

6.2.2 user defined functions

Like library functions, custom functions have function names, return value types, and function parameters. But the difference is that we can design them ourselves.

Compare the size of two numbers
int max(int x,int y)
{
	if(x>y)
		return x;
	else
		return y;
}
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d",&a,&b);
	int mm = max(a,b);
	printf("%d\n",mm);
	return 0;
}

Exchange two numbers

It can be seen that the exchange was not successful.
Analysis: in the main function, a = 10 and B = 20 have their own addresses, while X and Y in the test function also have their own addresses. Exchanging the values of X and y has nothing to do with the values of a and B, so the exchange failed. At this time, we need to use pointer variables to solve the problem.

Using pointer variables

6.3 function parameters

The parameters used in function definition are formal parameters,
a. B the real parameters transmitted to swap1 and swap2 are actual parameters

6.3.1 actual parameters

The actual parameters can also be expressions, functions, etc

7. Operator

7.1 shift operator

Shift left operator < < shift right operator > >

#include <stdio.h>
int main()
{
	int a = 3;
	//The shift left operator is binary
	int b = a << 1;
	printf("%d\n",b);
	return 0;
}

a=3 is the type of int, and the type of int is 4 bytes, 8 bits per byte, a total of 32 bits. A is converted into binary to 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 11. The whole is shifted to the left, and the vacancy on the right is supplemented with 0. Then the result is 0000000000000000001000000110, b=6

7.2 bit operators

Bitwise operators meaning
&Bitwise AND
|Or by bit
^Bitwise XOR

7.3 assignment operator

a = 3;
a = a + 7;
//a += 7;

7.4 monocular operator

a + b  //+Binocular operator with two operands
 Unary operator: only one operand

7.4.1 ! Operator

7.4.2 ~ operator

int main()
{
	int a = 0;
	//~Reverse by (binary) bit, 0 becomes 1, 1 becomes 0
	printf("%d\n",~a);
	return 0;
}

The integer stored in memory is * * * complement * * *.

There are three representations of an integer in binary: original code, inverse code and complement code

Calculation of negative original code, inverse code and complement, take - 1 as an example:

Binary bit of original code: - 1

The highest bit of an integer represents * * * sign bit * * *, and the highest 1 represents a negative number

10000000 00000000 00000000 00000001 (original code)

Inverse code: the sign bit remains unchanged, other bits are reversed by bit, 1 becomes 0, 0 becomes 1

11111111111111111111111111111111111111110 (inverse code)

Complement: inverse binary sequence * * * plus 1***

11111111111111111111111111111111111111111111111 (complement)

For positive integers, the original code, inverse code and complement code are the same.

For int a=0, binary is 00000000 00000000 00000000 00000000

~a is 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

The output of% d is the original code, so you need to subtract ~ a by 1, the symbol bit remains unchanged, and the bit is reversed,

Get 10000000 00000000 00000000 00000000, and finally print out - 1

7.4.3 sizeof operator explanation

7.4.4 pre + + Post++

int main()
{
	int a = 1;
	int b = a++;//Post + +, use first, then++
	printf("%d\n",b);//b=1
	printf("%d\n",a);//a=2
		
	int c = 10;
	int d = ++c;//Pre + +, first + +, then use
	printf("%d\n",d);//d=11
	printf("%d\n",c);//c=11
	return 0;
}//Pre post same

7.4.7 (type)

3.14 is essentially a double type, so it needs to be cast.

int main()
{
	int a =(int)3.14;
	printf("%d\n",a);
	return 0;
}

7.7 relational operators

>=Greater than or equal to
<=Less than or equal to
!=Not equal to
==Test whether equal to

7.6 logical operators

&&Logic and
||Logical or

7.7 conditional operator (ternary operator)

? :

exp1 ? exp 2 : exp 3

7.8 comma expression

int main()
{
	int a = 1;
	int b = 2;
	int c = 3;
	int d = (b = a + 2,c = b - 1,a = c + 2);
	//Comma expressions are evaluated from left to right
	//The entire expression result is the last expression result
	printf("%d\n",d);//d=4
	return 0;
}
  • Subscript reference operator []
  • Function call operator ()

When calling a function, the () after the function is the function call operator.

8. Keywords

Keywords of c language

1. Provided by C language, you cannot create your own keywords

2. Variable name cannot be keyword

Common keywords

Misunderstanding: define uses #define

Include uses #include as preprocessing instructions

Auto is automatic - every local variable is auto decorated

extern is used to declare external symbols

Register register keyword

#include <stdio.h>
int main()
{
    //A large number of frequently used data are stored in registers to improve efficiency
    register int num = 1;//It is recommended that the value of num be stored in the register. Whether it exists in the register is determined by the compiler
    return 0;
}

8.1 typedef type redefinition

#include <stdio.h>
typedef unsigned int nu;//unsigned int redefined as nu
int main()
{
	unsigned int a = 100;
	nu  b = 200;
	printf("%d\n",a);//a=100
	printf("%d",b);//b=200
	return 0;
}

8.2 static

  • Modify local variables

Modified local variable, modified local variable. (essentially changes the storage type of variables)

void test()
{
	int a = 1;//After completing the program for the first time, a=2 is destroyed, and the variable a=1 is re created for the second time
	a++;
	printf("%d",a);
	
}
int main()
{
	int i = 0;
	while (i<10)
	{
		test();
		i++;
	}
	return 0;
}//Print out 2222
void test()
{
	static int a = 1; //The first time I came out of this program, a=2 was not destroyed, so this line of code is meaningless.
	a++;
	printf("%d",a);
	
}
int main()
{
	int i = 0;
	while (i<10)
	{
		test();
		i++;
	}
	return 0;
}//Print out 23456789
  • Modify global variables

    static modifies global variables so that global variables can only be used inside their own source file (. c)

    Global variables can be used inside other source files because they have external link attributes, but after being modified by static, they become internal link attributes. Other source files cannot be linked to this static global variable.

  • Modifier function

static modifies the function so that the function can only be used inside its own source file and cannot be used inside other source files.

Essentially: static transforms the external link attribute of a function into an internal link attribute.

It is the same as static modifying global variables!!

9. Constants and macros

define is a preprocessing instruction

1.define symbols

#define max 100
int main()
{
	printf("%d\n",max);
	return 0;
}

2.define macro

#define asd(x,y) x + y 
int main()
{
	int a = 1;
	int b = 2;
	printf("%d\n",asd(a,b));//3
	printf("%d\n",3*asd(a,b));//3*1+2=7
	return 0;
}

At this time, if 3*asd(a,b) wants to get 9, modify it as follows:

#define asd(x,y) ((x)+(y))

10. Pointer

A memory unit is a byte

int main()
{
	char a = 'h';//a the space to be allocated in memory is 1 byte
	printf("%p\n",&a);//%p print address
	char * pe = &a;//pe is used to store addresses, and its scientific name is pointer variable
	//*Description pe is a pointer variable
	//Char indicates that the object pe refers to is of char type
	//If it is of type int, the space allocated by a in memory is 4 bytes, and the printed address is the first of the four bytes
	return 0;
}

*Dereference operator (monocular operator)

int main()
{
	int a = 10;
	int*pa = &a;
	*pa = 20;
	//*Dereference operation * PA is to find pa through the address in PA
	printf("%d\n",a);//a=20
	return 0;
}

Pointers are used to store addresses

How much space the pointer needs depends on how much space the address needs to be stored

Same pointer size!!

32 bit - 4 byte

64 bit - 8byte

The compiler here is 64 bits, so it is 8 bytes

11. Structure

12. Branch statement

Statement definition: by semicolon (;) Separated is a statement

12.1 if statement

if(expression)
	sentence;

if(expression)
	Statement 1;
else
	Statement 2;
	
//Multi branch
if(Expression 1)
	Statement 1;
else if(Expression 2)
	Statement 2;
else if(Expression 3)
    Statement 3;
else
	Statement 4;
//When multiple statements are controlled after if else, {} can be brought. Otherwise, one statement will be executed by default

Misunderstanding:

(1) The problem of writing operators on both sides of an expression at the same time

Age = 60 > 18 is true, so the left is 1. At this time, 1 < 26 is true, and youth is printed. This method is incorrectly written.

Correct writing:

else if (age >= 18 && age < 26)

(2) else matching problem

We mistakenly think the result is haha, but the real answer is really empty. Why?

At first glance, else is aligned with the first if statement, but else only matches the latest if statement. The compiler automatically aligns the code by default, as shown in the figure below, so it returns null.

Reflection: code writing should be standardized at ordinary times; You can use {} to avoid misunderstanding.

Book: high quality c/c + + Programming

Exercise 1: print out 1-100 Odd number between
int main()
{
	int i = 0;
	for(i = 1;i <= 100;i++)
	{
		if(i % 2 == 1)
			printf("%d  ",i);
	}
    //For (I = 1; I < = 100; I + = 2) / / the first digit is 1, plus 2. Numbers such as 1, 3, 5, 7 and 9 can be generated at one time
	return 0;
}


Exercise 2: input three numbers and output them in descending order
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	scanf("%d %d %d",&a,&b,&c);
	if (a < b)
	{
		int t = a;
		a = b;
		b = t;
	}
	if (b < c)
	{
		int t = b;
		b = c;
		c = t;
	}
	if (a < c)
	{
		int t = a;
		a = c;
		c = t;
	}
	printf("%d %d %d\n",a,b,c);
	return 0;
}
//When using scanf with% d, remember not to bring \ n
//int t can only be rendered in if statement. When it appears on scanf, t will carry the value in the previous statement in the next loop
//When changing the order of a, B and C during printing, the if loop statement should be adjusted slightly

Exercise 3: find the greatest common divisor of two numbers
int main()
{
	int m = 0;
	int n = 0;
	scanf("%d %d",&m,&n);
	//Find the smallest of the two numbers and decrease them
	int max = m > n ? m : n;
	while(1)//The goal is to keep it circulating
	{
		if (m % max == 0 && n % max == 0)
		{
			printf("The maximum common divisor is:%d\n",max);
			break;
		}
		max--;
	}
	return 0;
}
Method 2: rolling phase division
int main()
{
	int m = 0;
	int n = 0;
	int t = 0;
	scanf("%d %d",&m,&n);
	while(t = m % n)//When the remainder is 0, the cycle ends, and the maximum common divisor is n
	{
		m = n;
		n = t;
	}
	printf("The maximum common divisor is%d\n",n);
	return 0;
}


Exercise 4: print 1000-2000 Leap year in
 Judge whether it is a leap year?
1.Can it be divided by 4 and not by 100
2.Can be divided by 400
 Method 1:
int main()
{
	int year = 0;
	for (year = 1000;year <= 2000;year++)
	{
		if (year % 4 == 0)
		{
			if (year % 100 != 0)
			{
				printf("%d ",year);
			}
		}
		if (year % 400 == 0)
		{
			printf("%d ",year);
		}
	}
	return 0;
}
Method 2:
int main()
{
	int year = 0;
	for (year = 1000;year <= 2000;year++)
	{
		if (((year % 4 == 0)&&(year % 100 != 0))||(year % 400 == 0))
		{
			printf("%d ",year);	
		}
	}
	return 0;
}


Exercise 5: print 100-200 Prime between
 Method 1:
int main()
{
	int n = 0;
	for (n = 100;n <= 200;n++)
	{
		int i = 0;
		for(i = 2;i < n;i++)
		{
			if(n % i == 0)
			{
				break;//If the number of them is 0 after taking the modulus, it indicates that it is not a prime number and jumps out of the current program
			}
		}//----Come here
		if(n == i)
		{
			printf("%d ",n);
		}
	}
	return 0;
}
Method 2:
You can also use another idea, such as 101. We just need to find 2 - sqrt(101)Between the numbers to try
//#include <math.h>
int main()
{
	int n = 0;
	for (n = 100;n <= 200;n++)
	{
		int i = 0;
		int flag = 1; 
		for(i = 2;i < n;i++)
		//for(i = 2;i <= sqrt (n);i++)
		{
			if(n % i == 0)
			{
				flag = 0;
				break;
			}
		}
		if(flag == 1)
		{
			printf("%d ",n);
		}
	}
	return 0;
}

12.2 switch statement

Error prone point: switch(m) m must be integer
Int char long are all integers, while float and double are not integers.

#include <stdio.h>
int main()
{
	
	int day = 0;
	scanf("%d",&day);//Format the input, and you cannot add \ nor spaces at will
	switch (day)
	{
	default://No sequence
		printf("error");
		break;
	case 1:
	case 2:
	case 3:
	case 4:	
	case 5:
		printf("weekdays");
		break;
	case 6:
	case 7:
		printf("rest");
		break;	
	}
	return 0;
}

Example:

Results: M = 5, n = 3

Myth 1: without break, case 1, 2 and 3 should go down in turn;

Myth 2: when n walks in case 2 under the nested switch statement, the break of case 2 just jumps out of the nested switch statement, not the whole program (when in the switch statement), and case still needs to continue to execute.

13. Circular statement

while do...while for

13.1 while statement

while(expression)
	Circular statement;


After using while, it will print all the time

break and continue

In a while loop, break is used to permanently terminate the loop

It's not over. It's in a dead circle.

In the while loop, continue is used to skip this loop, skip the code after continue (print, i + +), and go directly to the judgment part of while to see whether to carry out the next loop.

13.1.1 getchar


When reading, an error is encountered or the file ends, and EOF is returned. When read correctly, the ascii value of the character is returned. ascii value is an integer; Second, when getchar returns, it may be EOF. EOF is essentially - 1, and - 1 is also an integer. The return type of getchar is int.

The EOF file end flag is essentially - 1

int main()
{
	int ch = getchar();//Get character
	//printf("%c\n",ch);
	putchar(ch);//The characters printed by Putchar are similar to the previous sentence
	return 0;
}
perhaps
int main()
{
	int ch = 0;
    //CTRL + Z -- end of getchar reading
	while ((ch=getchar()) != EOF)
		putchar(ch);
	return 0;
}

Usage scenario

Scenario 1: after inputting 123, all subsequent confirmation passwords are directly output.

123 enter after analysis. At this time, the compiler reads 123\n when reading. At this time, scanf only takes 123 away and \ n remains in the buffer. Therefore, getchar defaults to our input. At this time, printing out the input fails.

Scenario 2:

Make improvements based on scenario 1. You only need to solve the problem. Enter password after getchar()

It seems successful at this time, but in reality, the password we enter may also have letters, symbols and so on
For example, when we enter 123 asds, the problem in scenario 1 also occurs, and the subsequent confirmation password appears together.

Analysis: after inputting 123 asds, the information read by the computer is 123 asds\n, scanf also only takes 123, while getchar can only take one character. There are still many characters in the buffer. Therefore, the computer defaults that we have entered the value of ch and directly feeds back the input failure.

Scenario 3:

To improve the above situation, because we need to enter after entering the password, it defaults to \ n when reading, so we only need to use \ n as the sign of the end of password input.

13.2 for statement

	 Initialization judgment     adjustment
for(Expression 1;Expression 2;Expression 3)
    Circular statement;

break and continue are in the for loop

//break
int main()
{
	int i = 0;
	for(i = 1;i <= 10;i++)
	{
		if (i == 5)
			break;
		printf("%d ",i);
		
	}
	return 0;
}
//1 2 3 4
 The result is undoubtedly the same break stay for In the cycle, the cycle is permanently terminated.

//continue
int main()
{
	int i = 0;
	for(i = 1;i <= 10;i++)
	{
		if (i == 5)
			continue;
		printf("%d ",i);
		
	}
	return 0;
}
//1 2 3 4 6 7 8 9 10
 Analysis: in for Loop, execute first i=1,Subsequent execution i <=10 ,If it is less than, execute printing and print after printing i++,and i=5 When executing continue After that, the statement behind it will not be executed and printed 5, and directly jump to the next step i++

At this time, the three parts of the for loop are omitted and will not be printed in segments.



Analysis: when i=0, run J = 0, J < 3, print, j + + repeat 3 words, then i + +, i=1, i=1 < 2, also execute the for loop of J, and print six times in total

Analysis: the key is that when i=0, j is added from 1 to 3, and then i + +. At this time, i = 1 < 2. When executing the for loop of J, j is no longer the initial 0, but the 3 left last time, so it will not be printed.

int main()
{
	int i = 0;
	int k = 0;
	for(i = 0,k = 0;k = 0;k++,++i)//There is no essential difference between K + + + I and K + + + + I. they are all the same
		k++;
	return 0;
}
Q: how many cycles?
    
Answer: 0
    for Judgment statement in loop k = 0  here=Not judgment, but assignment. At this time k = 0 False, so cycle 0 times.
practice: n!
int main()
{
	int n = 0;
	int w = 1;
	int i = 0;
	scanf("%d",&n);
	for(i = n;i>0;i--)
	{
		w *= i;//Equivalent to w = w * i
	}
	printf("%d\n",w);
	
	return 0;
}
Exercise: enter a number n,calculation n!+...+1!
int main()
{
	int w = 1;
	int i = 0;
	int j = 0;
	int sum = 0;
	int n = 0;
	scanf("%d",&n);
	for(j = 1;j <= n;j++)
	{
		for(i = 1;i <= j;i++)
		{
			w = w * i;
		}
		sum += w;
	}
	printf("%d\n",sum);
	
	return 0;
}
When n=3 Time
 Error prone: not given w Reassign, in execution j=3 Keep the last time when j=2 Value at w=2
    j=1	i=1	w=1*1	sum=0+1=1
    j=2	i=1	w=1*1
    	i=2	w=1*2=2	sum=1+2*1
    j=3 i=1 w=2*1	//Now w has an initial value of 2
    	i=2	w=2*2*1
    	i=3	w=2*3*2*1=12	sum=15(Wrong answer)
Amendment:
int main()
{
	int w = 1;
	int i = 0;
	int j = 0;
	int sum = 0;
	int n = 0;
	scanf("%d",&n);
	for(j = 1;j <= n;j++)
	{
		w = 1;  //a key!!! Before calculating the factorial of n, initial w to 1
		for(i = 1;i <= j;i++)
		{
			w = w * i;
		}
		sum += w;
	}
	printf("%d\n",sum);
	return 0;
}
//sum=9

Method 2:    
int main()
{
	int w = 1;
	int i = 0;
	int sum = 0;
	int n = 0;
	scanf("%d",&n);
	for(i = 1;i <= n;i++)
	{
		w = w * i;
		sum += w;
	}
	printf("%d\n",sum);
	return 0;
}

13.3 do...while

do
	Circular statement;
while(expression);//Execute first and then judge. The loop body must be executed at least once
  • break

  • Continue

Still trapped in a dead circle

1.Find a specific number in an ordered array, dichotomy
int main()
{
	int arr[] = {1,2,3,4,5,6,7,8,9,10};
	int k = 7;
	int se = sizeof(arr) / sizeof(arr[0]);//Get the number of arrays
	int left = 0;
	int right = se - 1;
	while (left <= right)
	{
		int mid = (left + right) / 2;
		if (arr[mid] < k)
		{
			left  = mid + 1;
		}
		else if (arr[mid] > k)
		{
			right = mid - 1;
		}
		else
		{
			printf("The subscript is:%d\n",mid);
			break;
		}
	}
	if (left > right)
	{
		printf("Can't find\n");
	}
	return 0;
}



2.To show				
    welcome to China
    w##############a
    we############na
    ...
    welcome to China	The effect of writing code

#include <stdio.h>    
#include <string.h>
#include <windows.h>
int main()
{
	char arr1[] = "welcome to China";
	char arr2[] = "################";
	
	int left = 0;
	int right = strlen(arr1)-1;//Using strlen requires string H header file
	while(left <= right)
	{
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		printf("%s\n",arr2);
		//Using Sleep requires calling windows H header file
		Sleep(1000);//Sleep time 1000ms = 1s sleep initial capital
		system("cls");//Clear screen
		left++;
		right--;
	}
	//printf("%s\n",arr2);// For better display
	return 0;
}



3.Write code to simulate the user login scenario, and can only log in three times.(You are only allowed to enter the password three times. If the password is correct, you will be prompted to log in. If you enter the wrong password three times, you will exit the program.
#include <stdio.h>  
#include <string.h> / / when using stamp, you need to call string H header file
int main()
{
	int i = 0;
	char password[20] = {0};
	
	for  (i = 1;i <=3;i++)
	{
		printf("Please input a password:");
		scanf("%s",password);
		//if(password == "123456") / / compare the addresses of the first characters of the two strings, and there is no comparison content   
		//error!!!  Two strings cannot be compared with = = only strcmp can be used
		if (strcmp(password,"123456") == 0)
		{
			printf("Login successful\n");
			break;
		}
		else
		{
			printf("Password error, please re-enter\n");
		}
	}
	if (i == 3)
	{
		printf("Login times have exceeded three, exit the program");
	}
	return 0;
}

13.4 goto statement

goto Statement usage scenario:
	for(...)
	{
		for(...)
		{
			for(...)
			{
				if(disaster)
				{
					goto error;
				}
			}
		}
	}
	error:
		if(...)
Jump out of the whole cycle directly, than break Faster

After inputting that I am a pig, the computer cancels the shutdown, otherwise the computer will shut down after 60 seconds
#include <string. h> / / call the header file when using strcmp
#include <stdlib. h> / / use header file when using system
int main()
{
	char arr[20] = {0};
	//System - execute system commands
	system("shutdown -s -t 60");//Call shutdown,-s shutdown - t to set the number of seconds after shutdown
again:
	printf("Please enter: I am a pig, otherwise the computer will be at 60 s Later shutdown\n");
	scanf("%s",arr);
	if (strcmp(arr,"I'm a pig") == 0)
	{
		system("shutdown -a");
	}
	else
	{
		goto again;//Give me a chance to re-enter
	}
	return 0;
}

Topics: C Programming basic