Initial C language (fourth class hour)

Table of contents

1. Array

1. Definition: a combination of a group of elements of the same type

2. The subscript of the array

3. The use of arrays

Two, the operator

1. Arithmetic operators

2. Shift operator

3. Bitwise operators

4. Assignment operator

5. Unary operator

6. Relational Operators

7. Logical operators

8. Conditional operator (ternary operator)

9. Comma expressions

10. Subscript references, function calls, and structure members


1. Array

1. Definition: a combination of a group of elements of the same type

  display method:

int arr[10]={0,1,2,3,4,5,6,7,8,9};

arr: array name 10: the number of elements in the array {stored data}

  We first give the template: int arr[number of elements]={element data};

Arrays can also be defined like this:

char arr[10]//定义一个字符数组,用来存放字符元素(未初始化)
double arr[]={0,1,2,3,4,5};//定义一个浮点数师数组,元素个数可以不写,会根据存放的数据个数自己生成
int a[10]={0};//初始化一般赋值0,若初始化的数据个数不够元素个数,则后面元素数据自动补0

The third is called incomplete initialization, such as:

int a[4]={1,2};//这个数组可以存放四个数据元素,但只给它赋值了1,2;则剩下的自动赋值为0

2. The subscript of the array

    Each element of the array has a subscript, and the subscript starts from 0.

int a[4]={1,3,4,6};
int arr[4] 1 3 4 6
subscript 0 1 2 3
3. The use of arrays

   For example, if we want to print the 3 elements in the array, we need to find the subscript 1

printf("%d",arr[1]);

If you need to print the entire array, you need to use a loop

int i=0;
while(i<4)
{
printf("%d ",arr[i]);
i++;

}

Two, the operator

1. Arithmetic operators

+(Add) -(Subtract) *(Multiply) /(Divide) %(Remainder)

printf("%d",3+5);//打印结果为8
printf("%d",5-3);//打印结果为2
printf("%d",3*5);//结果为15
printf("%d",3/5);//打印结果为0
printf("%d",5/3);//结果为1
printf("%lf",5.0/3);//结果为1.6666
printf("%d",5%3);//结果为2

Division sign /: If both sides are integers, the result is divisible. If at least one of the two sides is written as a decimal, the result will also be calculated to the decimal part.

Remainder (modulus)%: used to calculate the remainder.

2. Shift operator

>>   <<

don't need to know

3. Bitwise operators

& ^  |

don't need to know

4. Assignment operator

= +=  -=  *=   /=   &=  ^=   |=   >>=    <<=

5. Unary operator

! - + & sizeof ~ -- ++ * (type)

1) ! logical inverse operation 

  For example, a!=5 means that a is not equal to 5; it means the opposite

2) - Negative values

int a=-1;
printf("%d\n",-a);//结果为1

3) + Positive values     ​​generally have little effect, and cannot turn negative numbers into positive numbers

int a=-1;
printf("%d\n",a);//结果为-1
printf("%d\n",+a);//结果为-1

4) & get address

(described later)

5) The type length of the operand of sizeof (in bytes)

(later)

6) ~ Reverse the binary bitwise of a number

(later)

7) -- pre-position, post-position --      is to implement a subtraction operation on itself

int a=5;
//后置--
printf("%d\n",a--);//先打印a,所以打印结果为5,再让a-1,所以此时a变为4
//前置--
printf("%d\n",--a);//先a-1,所以此时a为4,再打印a,打印结果为4

8) ++ pre, post ++     ++ is to implement a subtraction operation on itself

int a=5;
//后置++
printf("%d\n",a++);//先打印a,所以打印结果为5,再对a+1,所以此时a变为6
//前置++
printf("%d\n",++a);//先a+1,所以此时a为6,再打印a,打印结果为6

9) * indirect access operator

(described later)

10) (type) mandatory type conversion

int a=(int)3.14//将3.14强制转换为整形3

 date cannot be converted

6. Relational Operators

> >= < <= != ==1)> 
1)> greater than sign

2)>= greater than or equal to

3) <= less than or equal to

4) != is used to test for inequality

5) == is used to test for equality

7. Logical operators

     &&    ||

1) && logical AND (and)    

  a&&b requires both a and b to be true for a&&b to be true

2) || logical or (or)

a||b only needs one of a or b to be true, then a||b is true

Purpose: used for conditional judgment, not like this a<=x<=b, only || 

8. Conditional operator (ternary operator)

   exp1?exp2:exp3

Parsing: If exp1 is true, execute exp2; if it is false, execute exp3

int a=3;
int b=5;
int max=a>b?a:b;//如果a>b,则最大值为a,否则为b

9. Comma expressions

exp1,xep2,exp3,.....expn

Features: It is calculated sequentially from left to right, and the result is the value of the entire expression.

int a=3;
int b=4;
int c=1;
int d=(a=2+c,b=a-1,c=a+b);
//先计算a=2+c,此时a=3,再计算b=a-1,此时b=2,再计算c=a+b,所以此时c=5,所以d=5.

10. Subscript references, function calls, and structure members

[]    ()   .    ->

Guess you like

Origin blog.csdn.net/2301_77053417/article/details/131676269