A preliminary understanding of C language: functions, arrays, operators

Table of contents

1. Getting to know the function

2. Getting to know arrays first

1. The form and definition of an array

 2. How to access the array?

3. Operator

  count operator

 1. / (division)

2, % (modulus, remainder)

  shift operator

 1. << left shift operator 

2. >> Right shift operator

logical operator

1. && and

2, || or

  3.! Logical NOT

unary operator

1.! : logical inversion

2.- : Negative value operator

3.+ : Positive value

4.& : Take the address

5. sizeof operator

6.- - Decrement

7.++ Auto increment

8.* dereference operator

9. (type) mandatory type conversion

relational operator

>、>=、<、<=、==、!=

conditional operator

expression1 ? expression2 : expression3

comma expression

expression1, expression2, ..., expressionn


The following lists only the common operators, a preliminary understanding is good

1. Getting to know the function


What is a function? What are functions used for?
First, a function is a module of the C language, which is built piece by piece, has strong independence, and can call each other. In other words, in C language, a function can call n functions, that is, a large function calls a small function, and a small The function in turn calls the "little" function. . This is structured programming, so process-oriented languages ​​are also called structured languages. Second, a function is a collection of a series of C statements that can accomplish a specific function. .Call this function directly when you need this function, without stacking code every time. When you need to modify this function, you only need to modify and maintain this one function.

 

 For example: the application of the main function and sub-function in the figure below: the sum function, when the main function is executed, when the program runs to "int sum = Add (n1, n2)", the external "Add sub-function" will be called to realize the two numbers add up.

2. Getting to know arrays first

1. The form and definition of an array

     "a collection of elements of the same type"

 2. How to access the array?

   Therefore, when the program can access the array, it is in the order of subscripts (0-9)

3. Operator

  count operator

 1. / (division)

In a division operation, if the operands on both sides of the division sign are integers, the result is the part of the quotient

Operand: Refers to the dividend and the divisor

eg: a/ba and b are both integers, if a<b, the result is 0

(The result of dividing a decimal by a large number is 0)

In two integer operations, the C language follows the (round to 0) rule

The so-called rounding towards 0 means rounding towards 0 on the number axis

eg : 10/8=1.25 math operation

        10/8 =1 C language

        Because 1.25>0 does not take the integer 2 when rounding, but takes 1 because 1 is closer to 0 than 2
 

In a division operation, as long as one operand is a floating-point number, it is a division operation of a floating-point number

The so-called division operation of floating-point numbers is that the final result is a floating-point number

​
#include <stdio.h> 
int main()
{
    printf("%d\n",20/10);//结果就是一个是整数 
    //小数除大数
    printf("%d\n",2/19);//结果为0 
    printf("%lf\n",20.0/10);//结果是一个浮点数
    printf("lf\n",20.0/10.0);//结果是一个浮点数    
    return 0;
}​

2, % (modulus, remainder)

The result obtained in the modulo operation is the remainder left after the division of two numbers

The operators on both sides of the remainder operation can only be integers

In modulo arithmetic, a%b results in a when the value of a is less than b

(The result of decimal modulo large number is the decimal itself)

#include <stdio.h>
int main()
{
	//两数进行模运算
	printf("%d\n", 3 % 2); //1
 
	//小数 模 大数 得到的是小数本身
	printf("%d\n", 4 % 5); //3
	return 0;
}

  shift operator

 1. << left shift operator 

When shifting binary bits, what is shifted is two's complement

Shift rules:

How does the left shift operator move: discard on the left and add 0 to the right

Shift operators cannot shift negative bits

#include <stdio.h>
int main()
{
	//左移操作符 <<
	int a = 10;
//a 的原码:00000000 00000000 00000000 00001010
//正数的原码 反码、补码 相同
	int b = a << 1;
//将a左移一位:00000000 00000000 00000000 00010100
// 左移一位之后还是一个正数,原、反、补 相同
// b=20
	printf("%d\n", b);
}

2. >> Right shift operator

What is shifted is the binary bit, and what is shifted is the complement of the binary bit

Shift rules:

1. Arithmetic right shift: the right side discards the left complement sign bit (used by most compilers)

2. Logical right shift: the right side is discarded and the left side is filled with 0

#include <stdio.h>
int main()
{
//0 表示正数,1表示负数
	int a = -10;
//a原码 : 10000000 00000000 00000000 00001010
// 反码: 11111111 11111111 11111111 11110101
// 补码: 11111111 11111111 11111111 11110110
//将补码算术右移一位
	int b = a >> 1;
//补码:11111111 11111111 11111111 11111011
//	    10000000 00000000 00000000 00000100
//原码:10000000 00000000 00000000 00000101
// b=-(1+4)=-5
	printf("%d\n", b);
	return 0;
}

logical operator

1. && and


#include <stdio.h>
int main()
{
	//&:有0为假 有为1为真
	int a = 7;
	int b = 20;
	int c = a && b;
	printf("%d\n", c);//最后结果为0

	return 0;
}

2, || or

#include <stdio.h>
int main()
{
	int a = 10;
	int b = 20;
	int c = a || b;
	printf("%d\n", c);

	return 0;//结果为1
}

  3.! Logical NOT

! logical inverse operator

Let true become false, false become true

unary operator

An operand with only one operand

1.! : logical inversion

2.- : Negative value operator

3.+ : Positive value

4.& : Take the address

5. sizeof operator

The sizeof operator is an operator, to distinguish it from the function

sizeof is mainly used to calculate the size of the data type in memory (in bytes)

When calculating the size of the variable, the parentheses of sizeof can be omitted

6.- - Decrement

Pre---: first --, then use (decrement the variable by 1 first, and then use it)

Post--: use first, then-- (use first, then let the variable decrement by 1)

#include <stdio.h>
int main()
{
	//前置--
	int a = 10;
	int b = --a;
	//此代码执行了两部操作
	// 第一步:a=a-1;
	// 第二步:b=a;
	printf("%d\n", b);//b的值为9
 
	//后置--
	int c = 20;
	int d = c--;
	//此代码也执行了两步操作
	// 第一步:d=c;
	// 第二步:c=c-1;
	printf("%d\n", d);//d的值为20
	return 0;
}

7.++ Auto increment

Pre-++: use ++ first (increment the variable by 1 first, and then use it)

Post ++: first use post ++ (use first, then let the variable increase by 1)

#include <stdio.h>
int main()
{
	//前置++
	int a = 10;
	int b = ++a;
	//分为两步
	// a=a+1;
	// b=a;
	// 先让a自增1 再使用a 把a的值赋给b
	printf("%d\n", b);//b的值为11
 
	//后置++
	int c = 20;
	int d = c++;
	//分为两步
	// d=c;
	// c=c+1;
	//先使用c 把c的值赋给d 再让c自增1
	printf("%d\n", d);//d的值为20
	return 0;
}

8.* dereference operator

The dereference operator, also known as the indirection operator, works on pointers

Function: Dereferencing the pointer variable is to get the content pointed to by the pointer variable

9. (type) mandatory type conversion

Mandatory type conversion is the conversion of one type of data into another type of data

Note: Truncation occurs when converting from a large type to a small type

When the float or double type is forced to int type: the truncation occurs and the integer part is taken

When the char type is forced to int type: get the corresponding ASCII code value

#include <malloc.h>
#include <stdio.h>
 
int main()
{
	// float 转 int 截断取整数部分 
	float a = 3.14f;
	int b = (int)a;
	printf("%d\n", b);// b为3
 
	// double 转 int 截断取整数部分 
	double d = 4.225;
	int e = (int)d;
	printf("%d\n", e);// e为4
 
	// char 转 int  取到ASCLL值
	char c = 'A';
	int f = (int)c;
	printf("%d\n", f);// f 为 65
 
	//动态内存开辟
	// 将在堆区上开辟的空间强制转化为int* 类型 赋给 指针p 
	int* p = (int*)malloc(sizeof(int)* 13);
 
	//时间戳产生随机数
	//将time 函数的返回值强制转化成 unsinged int (无符号整型) 
	// srand 函数的参数是一个无符号整型所以强制类型转换
	srand((unsigned int)time(NULL));
	int x = rand();//产生一个 0 - 32767 的随机数
	printf("%d\n", x);//每次打印出现的结果都不一样
 
	return 0;
}

relational operator

>、>=、<、<=、==、!=

Just like judgments in mathematics,

But there is one more != (not equal): to judge whether two numbers are not equal

conditional operator

expression1 ? expression2 : expression3

expression 1? Expression 2: expression 3;

Conditional operator, also called ternary operator, because it has three operands

Execute expression 2 when expression 1 is true, otherwise execute expression 3

#include <stdio.h>
 
int main()
{
	//输入两个数打印两个数中的较大值
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	
	int max = (a > b ? a : b);
	printf("%d\n", max);
 
	//a > b ? printf("%d\n", a) : printf("%d\n", b);
	return 0;
}

comma expression

expression1, expression2, ..., expressionn

Comma expressions are multiple expressions separated by commas

Comma expressions are executed from left to right

The result of the comma expression is the last expression

#include <stdio.h>
int main()
{
	int a = 10;
	int b = 20;
	int c = 30;
	
	//d的结果就是逗号表达式中最后一个表达式的结果
	//也就是将c的值赋给d
	int d = (a, b, c);
	printf("%d\n", d); // d为30
}

Guess you like

Origin blog.csdn.net/fcccfffg/article/details/132010949