C language library function instructions (including operators)

Keywords: C


preface:

1. When any character is stored, its ascii code value (integer int type) is stored in memory.
2. Write the function according to the document

3. Library function, user-defined function (with function name, return value type and function parameters)

1.getchar, putchar function

Example 1:

int main()
{
	int ch = getchar();
		putchar(ch);
	return 0;
}

Example 2:

int main()
{
	int ret = 0;
	char password[20] = { 0 };
	printf("Enter the password first\n");
	scanf("%s", password);
	printf("Please confirm( y/n)");
	ret = getchar();
	if (ret =='y')
	{
		printf("correct");
	}
	else
	{
		printf("error");
	}
	return 0;
}

Hypothesis 1: if we enter 123456 and press enter, there will be no execution process of getchar function, and the result is:

Explanation: the execution logic is different from the logic we want: password and getchar are input functions. The input function should first detect the input buffer, which may put some data. When the input function receives data, the scanf function first reads the input buffer (there is nothing at first, waiting for input) , once there is something in the input buffer, the scanf function takes away a part. After taking it away, the scanf function passes and the getchar function arrives (for example, after you enter 123456, hit the Enter key to make the scanf function read 123456, and the buffer remains \ n), and then getchar reads \ n without waiting.

Solution: (solve how to make there is no '\ n' in the input buffer) read '\ n' with a getchar()

int main()
{
	int ret = 0;
	char password[20] = { 0 };
	printf("Enter the password first\n");
	scanf("%s", password);
	getchar();//Read '\ n'
	printf("Please confirm( y/n)");
	ret = getchar();
	if (ret =='y')
	{
		printf("correct");
	}
	else
	{
		printf("error");
	}
	return 0;
}

Hypothesis 2: after adding getchar (), when we enter 123456 (space) ABCD, the result is:


Analysis: enter 123456 ABCD, the scanf function reads 123456, getchar reads spaces, and ret=getchar() reads' A ', so the print error.

<font color="orae" size="3">Solution: add a loop until you read to'\n'Jump out of the loop.

int main()
{
	int ret = 0;
	int ch = 0;
	char password[20] = { 0 };
	printf("Enter the password first\n");
	scanf("%s", password);
	while ((ch = getchar()) != '\n')
	{
		;
	}
	printf("Please confirm( y/n)");
	ret = getchar();
	if (ret =='y')
	{
		printf("correct\n");
	}
	else
	{
		printf("error\n");
	}
	return 0;
}

Add a question:

int main()
{
	int ch = 0;
	while ((ch = getchar()) != EOF)
	{
		if (ch < '0' || ch > '9')
			continue;
		putchar(ch);
	}
	return 9;
}

In this code, if (CH < '0' | ch > '9') is interpreted from the perspective of asc code value

Operator

Note: as long as the operation is performed, the complement in memory is used, but the premise of this complement is that the concept of original inverse complement exists only for integers, and non integers are not the concept of original inverse complement.

Classification:

Arithmetic, shift, bit, assignment, unary, relation, logic, condition, comma (expression), subscript reference, function call, structure member

Arithmetic operators: +, -, *, /,%

1. Example of division:

main()
{
	int a = 5 / 2;//Quotient 2 more than 1,5 / 2 get quotient
	printf("a=%d\n", a);
}


   why isn't a equal to 2.5? Because the numbers at both ends of the division sign are 5 and 2, both of which are integers. If you perform integer division, you won't get decimal results.
   if any number at both ends of the division sign (/) is a decimal, then the division of floating-point numbers is executed, and this can be saved with floating-point numbers (i.e., double,%lf).

main()
{
	double a = 5.0 /2;//Shang 2 Yu 1
	printf("a=%lf\n", a);
}

2. Mold taking example

For modulo, the numbers (operands and operands) on both sides of% must be integers. The remainder after division is returned.

main()
{
	int a = 5 %2;//Shang 2 Yu 1
	printf("a=%d\n", a);
}

Supplement:

1. In addition to the% operator, several other operators can act on integers and floating-point numbers
2. For the / operator, if both operands are integers, perform integer division. As long as there are floating-point numbers, perform floating-point division.

Shift operator:

< < left shift operator
>>Right shift operator
Note: their operands must be integers, and the bits here are binary bits

1. Shift right operator

<1> Arithmetic shift right: discard the right and fill the original sign bit on the left (the original positive number is filled with a 0, the original negative number is filled with a 1)
<2> Logical shift right: discard on the right and fill 0 on the left
So what kind of shift does the compiler use?
The compiler adopts arithmetic shift, and the code proves that:

main()
{
	int a = -1;
	int b=a >> 1;//Shift right one bit
	printf("%d\n", b);
}


Ask the question: why not the 31st power of negative 2?
(citing brother Peng) "what moves in the memory is the complement, and the official print is the source code." < \ font >
Specific analysis:
1. First, there are three binary representations of integers: original code, inverse code and complement code.
2. The second is that the complement is stored in the memory, so when a shifts, it shifts the complement (for positive integers, the original, inverse and complement are the same)
3. Again, it is the original inverse complement of a (a=-1, negative).
Original: because it is a negative number, write 1 in the highest position
10000000000000000000000000000001
Reverse: (relative to the original code, the sign bit remains unchanged, and other bits are reversed by bit)
1111111111111111111111111111111111110
Complement: (inverse code + 1)
1111111111111111111111111111111111111

2. Shift left operator

Discard on the left and supplement 0 on the right

Supplement:

For shift operators, do not move negative digits, which is not defined in the standard. For example:
int num=10;
num>>-1//error

Bitwise operators:

&  bitwise AND
|  bitwise OR
^  bitwise XOR
Note: their operands must be integers. The bits here are binary bits.

Bitwise AND:

Two numbers (a, b) are binary transformed and compared in the same bit. If one 0 is 0, it is 1 before it is 1. (abbreviation: one false is false, and all true is true)
Code example:

Supplement: there are several code examples for calculating the complement of a number:

main()
{
 int num,i;
 int count=0;
 scanf("%d", &num);
 for (i = 0; i < 32; i++)
 {
 	if ((num>>i)&1==1)
 		count++;
 }
 printf("count=%d\n", count);
}

Bitwise OR:

As long as one is 1, it is 1, and two are 0 at the same time, it is 0. (short note: one true is true, and all false is false)
Code example:

Bitwise XOR

The binary bits corresponding to two numbers (a,b) are 0 if they are the same and 1 if they are different.

Add a test question: do not create temporary variables, exchange the values of 2 int numbers
a=a^b;
b=b^a;
a=b^a;

Assignment operator:

Re assign the value to the variable (note that the assignment operator is a = sign, and judging whether it is equal is two = signs)

Continuous use of operators

a=x=y+1; assign y+1 to X and then assign x to a (this is called the continuous use of assignment operators)

Compound assignor

+=, - =, * =, / =,% =, > > =, < < =, & =, | =, ^ = (these operators can be written as compound effects. Code example:)

int x=10;
x=x+10;
x+=10;//Compound operator
//For the same reason as other operators, this is more concise.

Monocular operator:

What is unary? There is only one operand. What unary operators do we have?
! logical inverse operation (true becomes false, false becomes true < fixed to 1 >)
-  negative sign
+  positive value
&  get address
*  dereference operator
Size of memory space occupied by sizeof   variables (including arrays) (in bytes)
~  bitwise negation of a number
--   front and rear
++  front, rear++
(type)   cast type

Use of sizeof

Example 1:

main()
{
	int a = 10;
	char c = 'r';
	char* p = &c;
	int arr[10] = { 0 };
	printf("%d\n",sizeof a);//Integer 4 bytes, a can omit parentheses
	printf("%d\n",sizeof(int));//The result of calculating the size of a is the same as that of its type. int cannot omit parentheses
	printf("\n");
	printf("%d\n",sizeof(c));//character
	printf("%d\n",sizeof(char));
	printf("\n");
	printf("%d\n",sizeof(p));//The pointer size is 4 or 8
	printf("%d\n",sizeof(char*));
	printf("\n");
	printf("%d\n",sizeof(arr));//The size of the array. There are 10 elements in the array. Each element is an integer, and each integer is 4 bytes, so 4 * 10 = 40
	printf("%d\n",sizeof(int[10]));//int[10] is the type of array (remove the array name from the array)
	printf("\n");
}

result:

Example 2:

main()
{
	short s = 0;
	int a = 10;
	printf("%d\n", sizeof(s = a + 5));//No matter what type A is, as long as it is placed in s, it can only become a short integer. The result is calculated by s, and S is two bytes
	printf("%d\n", s);//S can put down 15, so 0 is output because the expression in sizeof (s=a+5) will not be actually calculated, and the value of s remains unchanged
}

result:

++Use of front and rear

example:

(types): cast types

For example:
int a =(int)3.14;

Relational operator

>
>=
<
<=
==  used to test "equality"
!=   used to test "inequality"

Logical operator

&&  logic and
||  logical or

Distinguishing logic from bitwise

Logic and, logic or concerns whether the number itself is true or false
Bitwise and, bitwise or concerned with the binary sequence of this number

Depth interpretation

The characteristic of logic and is that if the result calculated on the left is false, we don't calculate whatever is on the right. Similarly, the characteristic of logic or is that if the left is true, the right is not counted. Code examples are as follows:

main()
{
	int i = 0, a = 0, b = 2, c = 3, d = 4;
	i = a++&&++b&&d++;//The result of a + + is 0, and the relationship between & & is that as long as one is 0, whatever is on the right is false, then it will not be executed later, + + b and d + + will not be executed, and a will increase by 1 after use
	printf("a=%d\n b=%d\n c=%d\n d=%d\n", a,b,c,d);
}

The result is:

Conditional operator (also known as ternary operator)

exp1?exp2:exp3
Explanation: if the result of expression 1 is true, the result of expression 2 is the result of the whole expression. If the result of expression 1 is false, the result of expression 3 is the result of the entire expression.

comma expression

A comma separated expression: exp,exp2,exp3
Comma expressions are executed from left to right. The result of the whole expression is the result of the last expression.

Subscript references, function calls, and structure members

1. [] subscript reference operator

Operand: an array name + an index value

2. () function call operator

Accepts one or more operands. The first operand is the function name, and the remaining operands are the parameters passed to the function

3. Visit members of a structure

.   structure, member name
->  structure pointer - > member name

Code example:

//Create a structure type - struct Stu
struct stu
{
	//Member variable
	char name[20];
	int age;
	char id[20];
};
main()
{
	//Using a variable of type struct Stu, a student object s1 is created and initialized
	struct stu s1 = { "Zhang San", 20, "20210910" };
	struct stu* ps = &s1;
	//Structure pointer - > member name
	printf("%s\n", ps->name);//ps is a pointer to an object
	printf("%d\n", ps->age);
	printf("%s\n", ps->id);
	//Structure variable. Member name
	printf("%s\n", s1.name);
	printf("%d\n", s1.age);
	printf("%s\n", s1.id);
}

Expression evaluation

The order in which expressions are evaluated is partly determined by the priority and associativity of operators. Similarly, the operands of some expressions may need to be converted to other types during evaluation.

Implicit type conversion

integral promotion

The integer arithmetic operation of C is always performed at least with the precision of the default integer type.
To achieve this precision, the characters and short integer operands in the expression are converted to normal shaping before use, which is called integer lifting.

Significance of integer promotion

Example 1:

char a,b,c;
a=b+c;
The values of b and c will be promoted to normal shaping, and then the addition operation will be performed.
After the addition operation is completed, the result can be truncated and then stored in a.

main()
{
	char a = 3;
	//00000000000000000000000000000011
	//00000011-a
	char b = 3;
	//01111111-b
	char c = a + b;
	//10000010 is placed in c, the high bit is the sign bit, and the negative number is converted to the original inverse complement (different from the positive number)
	//11111111111111111111111110000010 complement
	//11111111111111111111110000001 - inverse code
	//1000000000000001111110 - original code
	printf("%d\n", c);//-126
}

How to improve the integer?

Integer promotion is promoted according to the sign bit of the data type of the variable

Integer promotion of negative numbers

char c1=-1;
There are only 8 bits in the binary bit (complement) of variable c1:
1111111
Because char is a signed char
Therefore, when shaping and lifting, the high supplementary symbol bit is 1
The results after promotion are:
11111111111111111111111111111111

Integer promotion of positive numbers

char c2=1;
There are only 8 bits in the binary bit (complement) of variable c2:
00000001
Because char is a signed char
Therefore, when shaping and lifting, the high supplementary symbol bit is 0
The results after lifting are as follows:
00000000000000000000000000000001

Unsigned integer lifting, high complement 0

Example 2:

main()
{
	char c = 1;
	printf("%d\n",sizeof(+c));
}

As long as c in example 2 participates in expression operation, integer promotion will occur, so sizeof(+c) is four bytes. The result of the expression is:

Arithmetic conversion

Arithmetic conversion is also an implicit type conversion. If the operands of an operator are of different types, the operation cannot be performed unless one of the operands is converted to the type of the other operand. The following hierarchy is called ordinary arithmetic conversion.
long double
double
float
unsigned long int
long int
unsigned int
int
If the type of an operand is low in the above list (small type), it is first converted to the type of another operand (large type) and then the operation is performed.

Properties of the operator

There are three factors that affect the evaluation of complex expressions.
1. Operator priority
2. Associativity of operators
3. Control evaluation sequence
Which of the two adjacent operators should be executed first? Depends on their priorities. If the two have the same priority, it depends on their combination.

operator precedence

Conclusion: if the expression we write cannot determine the unique calculation path through the properties of the operator, there is a problem with this expression.

Posted by Marijnn on Sat, 20 Nov 2021 05:04:09 -0800