First knowledge of C language [3]

6 selection statement

int main() 
{
	int coding = 0;
	printf("Please enter 0 or 1:\n");
	scanf("%d",&coding);
	if (coding == 1)
		printf("true");
	else
		printf("false");
	return 0;
}

7 circular statement

  • while statement
  • for statement
  • do-while Statements
int main() {
	int iq = 0;
	printf("Please enter your iq: \n");
	while (scanf("%d\n", &iq)!=EOF) 
	// or while (scanf("%d\n", &iq) == 1)
	{
		if (iq > 140)
			printf("Genius!");
		return 0;
	}
}

If you enter two numbers in scanf, change to while (scanf ('% d \ n', &iq) = = 2)

8 function

int Max(int a, int b) {
	if (a >= b)
		return a;
	else (a < b);
		return b;
}

int main() {
	int a = 0;
	int b = 0;
	int max = 0;
	printf("Please enter two integers:");
	scanf("%d %d", &a, &b);
	max = Max(a, b);
	printf("The larger of these two numbers is:%d", max);
	return 0;
}

9 array

  1. 9.1 array definition
//array define
int arr1[10] = {1,2,3,4,5,6,7,8,9};
char ch[] = {'a','b','c','d'};
  1. Array subscript
    Subscript counting starts from zero and can be accessed through subscript. If there are ten elements, the number in the following table is 0-9

  2. Use of arrays

//array define
int arr1[10] = {1,2,3,4,5,6,7,8,9};
char ch[] = {'a','b','c','d'};

int main() {
	char arr2[6] = {'0'};
	int i = 0;
	for (i=0;i<10;i++) 
	{
	printf("%c\n", ch[i]);
	}
	return 0;
}

10 operator

·Basic operators: + - * /%
·Shift operator: > ><<
·Bitwise operators: &|^
·Assignment operator: = + =-=
·Unary operator (only one operand):! - +& Sizeof ~ – + + * (type): force type conversion eg.(int) to directly discard the number after the decimal point
·Relational operator: > > = < < =! ===
·Logical operators: & &||
·Conditional operator: exp1? exp2 : exp3
If exp1 is true, take exp2; If exp1 is false, exp3 is taken
·Comma expression: exp1, exp2, exp3,..., expN
From left to right, the result of the whole expression is the result of the last expression

11 common keywords

  • Keyword typedef: type redefinition
typedef unsigned int uint_32;
int main() {
	unsigned int num1 = 0;
	uint_32 num2 = 0;
	return 0;
}

num1 and num2 are of the same type

  • Register keyword register
    General variables are stored in memory. If register int num = 0, it is recommended to put the value of num in the register, but it is ultimately determined by the compiler. Therefore, the address cannot be retrieved (only memory has an address)

  • Keyword static
    static is used to modify variables and functions
    Can modify local variables, global variables, functions

  • When modifying local variables, the storage type of local variables is changed; Originally, the whole local variable is stored in the stack area and is modified by static and stored in the static area. Therefore, even if it is out of the scope, the variable will not be destroyed, and the life cycle is as long as that of the program.

  • When decorating a global variable, the external link attribute of the global variable will be changed, so that the variable can only be used in this source file, not in other source files.

  • Modifying a function is similar to modifying a global variable.

12 #define constants and macros

Macros are generally named in uppercase to distinguish them

//Define identifier constants
#define MAX =100
//The reason why (x)+(y) has parentheses is that there may be expressions when defining macros
#define ADD(x,y) ((x)+(y))

13 pointer

1. Memory is divided into small memory units. The size of a memory unit is 1 byte
2. Each memory cell has a number, which is also called address / pointer
3. The address / pointer can be stored in a variable, that is, the pointer variable
4. Through the address stored in the pointer variable, the space pointed to by the pointer can be found

int main() {
	int num = 10;
	&num;
	printf("%p\n", &num);
	int* p;//p is a pointer variable, and the object is of type int
	p = &num;
	*p = 20;//Change the value of num to 20
	printf("%d\n", num);
	return 0;
}

The address takes the address of the first byte (the smallest address)
Pointer size: 4 bytes (32 bits) or 8 bytes (64 bits)

14 structure

struct stu
{
	char name[20];
	int age;
	char sex[5];
};

int main() {
	//Initial structural information
	struct stu s = { "Zhang San", 23, "male" };

	//There are two access modes or ->
	//printf("name = %s age = %d sex = %s\n", s.name, s.age, s.sex);
	//When writing, the array does not need to take the address character. It is the address itself
	//Do not use newline character after scanf input!!!
	scanf("%s %d %s", s.name, &(s.age), s.sex); 
	struct stu* ps = &s;
	printf("name = %s age = %d sex = %s\n", ps->name, ps->age, ps->sex);
	return 0;
}

Keywords: C Back-end

Added by scoman on Thu, 13 Jan 2022 11:48:36 +0200