3.1 arithmetic operators

 operator operation example result + Plus sign +7 7 - minus sign a=7;-a -7 + plus 9+7 16 - reduce 9-7 2 * ride 9*7 63 / except 7/7 1 % Surplus 9%7 1 ++ ++ Auto increment (before): value after operation Auto increment (later): take value first and then operate a=7;b=++a a=7;b=a++ a=8;b=8 a=8;b=7 -- -- Self subtraction (before): calculate first and then take value Self subtraction (after): take the value before operation a=7;b=--a a=7;b=a-- a=6;b=6 a=6;b=7

5.1.1 surplus

```#include<stdio.h>
void main() {
//Because 9 and 7 are integers, the decimals after integer 1 in the result are truncated and only 1 is retained
double a = 9 / 7;
printf("a = %f\n", a);

//If you want to keep the decimal, you must add the decimal part to the number participating in the operation
double b = 9.0 / 7;
printf("b = %.15f\n", b);

//9% 7 refers to 9-to-7 remainder (mold taking)
int c = 9 % 7;
printf("c = %d\n", c);

//The formula for taking the mold in the computer is a - a / b * b
//-9 - (-9) / 7 * 7
int d = -9 % 7;
printf("d = %d\n", d);

//9 - 9 / (-7) * (-7)
int e = 9 % -7;
printf("e = %d\n", e);

//(-9) - (-9) / (-7) * (-7)
int f = -9 % -7;
printf("f = %d\n", f);
}```

The formula for taking the mold in the computer is a - a / b * b

5.1.2 ++

```#include<stdio.h>
void main() {
int a = 7;
int b = a++;//Equivalent to b = a; a = a + 1;
printf("a = %d  b = %d\n", a, b);
int c = ++a;//Equivalent to a = a + 1; c = a;
printf("a = %d  c = %d\n", a, c);
//++A and a + + can be used independently, but they are completely equivalent, both a = a + 1;
++a;
printf("a = %d\n", a);
a++;
printf("a = %d", a);
}```

1. b = a++;// Equivalent to b = a; a = a + 1;
2. c = ++a;// Equivalent to a = a + 1; c = a;
3. ++A and a + + can be used independently, but they are completely equivalent. Both are a = a + 1

5.1.3 practice

1. If there are still 97 days off, how many weeks and days are there?
```#include<stdio.h>
void main() {
int weeks, days;
weeks = 97 / 7;
days = 97 % 7;
printf("It's still a long way from the holiday % d Week zero % d day", weeks,days);
}```

2. Define a variable to save Fahrenheit temperature. The formula for Fahrenheit temperature to Celsius temperature is: 5 / 9 * (Fahrenheit temperature - 100), and request the Celsius temperature corresponding to Fahrenheit temperature.

```#include<stdio.h>
void main() {
//The formula for converting Fahrenheit temperature to Celsius temperature is: 5 / 9 * (Fahrenheit temperature - 100),
double Fahrenhite = 97;
double Centigrade = 5.0 / 9 * (Fahrenhite - 100);
printf("%f The corresponding Celsius temperature is%f", Fahrenhite, Centigrade);
}```

3.2 relational operators

3.2.1 introduction

1. The result of the relational operator is true, non-0, false, 0
2. Relational expressions are often used in if or loops

3.2.2 relational operator representation

 operator operation Example result == Equal to 9 == 7 false != Not equal to 9 != 7 true > greater than 9 > 7 true < less than 9 < 7 false >= Greater than or equal to 9 >= 7 true <= Less than or equal to 9<=7 false

3.2.3 code

```#include<stdio.h>
void main() {
int a = 9;
int b = 7;
printf("a==b = %d\n",a==b);
printf("a!=b = %d\n", a != b);
printf("a>b = %d\n", a > b);
printf("a<b = %d\n", a < b);
printf("a>=b = %d\n", a >= b);
printf("a<=b = %d\n", a <= b);
}```

3.3 logical operators

3.3.1 introduction

It is used to connect multiple conditions (generally speaking, it is a relational expression). The final result is either true (non-0 representation) or false (0 representation).

3.2.2 logical operator representation

Let variable A be 1 and variable B be 0

 operator describe Example && Logic and operators. The result is true only if both operands are true; Otherwise, it is false (A & & B) is false || Logical or operator. As long as one operand is true, the result is true (a | b) true ! Logical non operator. If the condition is true, make it false ! (a | b) is false

3.2.3 code

3.2.3.1 logic and operators

```#include<stdio.h>
void main() {
//double score = 97;
//double score = 79;
double score = 59;
if (score >= 80) {
printf("excellent");
}
else if (score >= 60 && score < 80) {
printf("good");
}
else {
printf("unqualified");
}
}```

```#include<stdio.h>
void main() {
int a = 9, b = 7;
if (b < 8 && a++ > 9) {// In a + + > 9 judgment, judge a > 9 first, and then a++
printf("ok1\n");
}
printf("a = %d\n", a);

int c = 9, d = 7;
if (d < 8 && ++c > 9) {// ++In c > 9 judgment, judge c > 9 first, and then + + c
printf("ok2\n");
}printf("c = %d\n", c);

int e = 9, f = 7;
if (f < 6 && ++e > 9) {// If the result of the first judgment is false during the & & operation, the subsequent conditions will not be judged
printf("ok3\n");
}printf("e = %d", e);
}```

3.2.3.2 logic or operation

```#include<stdio.h>
void main() {
int a = 9, b = 7;
if (a > 7 || b++ > 7) {//In logic and, if the result of the first judgment is true, the subsequent conditions will not be judged
printf("ok\n");
}
printf("b = %d\n", b);
}```

3.2.3.3 logical non operation

```#include<stdio.h>
void main() {
//int score = 97;
int score = 79;
int result = score > 80;
if (result) {
printf("ok1\n");
}
if (!result) {//When the result is reversed to true
printf("ok2");
}
}```

3.3.1 introduction

Assignment operator is to assign the value of an operation to a specified variable

3.3.2 assignment operator representation

operatordescribeExample
=Assign the right operand to the left operand

A = B + C is equivalent to

The value of B + C is assigned to A

+=The addition and assignment operator adds the left operand to the right operand, and then assigns it to the left operand

A += B is equivalent to A = A + B

-=Subtraction and assignment operator, which subtracts the operand on the left from the operand on the right, and then assigns it to the operand on the leftA -= B is equivalent to A = A - B
*=The multiply and assign operator multiplies the left operand by the right operand and assigns it to the left operandA *= B is equivalent to A = A * B
/=The division and assignment operator removes the operands on the left and assigns the operands on the right to the operands on the leftA /= B is equivalent to A = A / B
%=The modulo and assignment operator takes the left operand to the right operand, and then assigns it to the left operandA% = B is equivalent to a = a% B
<<=Shift left and assignment operatorC < < = 2 is equivalent to C = C < < 2
>>=Shift right and assignment operatorC > > = 2 is equivalent to C = C > > 2
&=Bitwise AND and assignment operatorC & = 2 is equivalent to C = C & 2
^=Bitwise exclusive or and assignment operatorC ^= 2 is equivalent to C = C ^ 2
|=Bitwise OR and assignment operatorC | 2 is equivalent to C = C | 2

3.3.3 code

```#include<stdio.h>
void main() {
//Exchange the values of a and b
int a = 9;
int b = 7;
int temp = a;//Set a temporary variable to hold the value of A
a = b;//Assign b to a
b = temp;//Assign temp to b, that is, copy the original value of a to b to complete the exchange
printf("a = %d\tb = %d", a, b);

//+=Example
int c = 97;
c += a;
printf("c = %d", c);
}```

1. The operation order is from right to left
2. The left side of the assignment operator can only be variables, and the right side can be variables, expressions and constant values
3. In operation, if the precision of the assigned variable is small, it may lead to precision loss

3.4 ternary operators

3.4.1 basic grammar

`Conditional expression ? Condition 1 :Condition 2;`
1. If the conditional expression is non-0 (true), the result of the operation is expression 1;
2. If the conditional expression is 0 (false), the result of the operation is expression 2;

3.4.2 code

```#include<stdio.h>
void main() {
int a = 9;
int b = 7;
int c = a > b ? a++ : --b;//A > b is true, so a + + is assigned to c; A + + performs the assignment operation first, that is, c = a, and then a = a + 1
printf("a = %d\nb = %d\nc =%d\n", a, b, c);

int d = 9;
int e = 7;
int f = d < e ? d++ : --e;//D < e is false, so e -- assigned to f; And E -- first line e = e - 1, and then perform the assignment operation
printf("d = %d\ne = %d\nf =%d\n", d, e, f);

//In ternary operators, if the precision of the assigned variable is less than that of the assigned variable, precision loss will occur
int num1 = a > b ? 1.2 : 1.3;
printf("num1 = %d\n", num1);

//Ternary can be converted to eif else statements
int num2;
if (a > b) {
num2 = 1.2;
}
else {
num2 = 1.3;
}
printf("num2 = % d\n", num2);
}```

1. In ternary operators, if the precision of the assigned variable is less than that of the assigned variable, precision loss will occur
2. Ternary can be converted to eif else statements

3.4.3 practice

Output the maximum of the three numbers

```#include<stdio.h>
void main() {
int a = 9;
int b = 7;
int c = 97;
int temp = a > b ? a : b;
temp = temp > c ? temp : c;
printf(" a,b,c The maximum value in is%d", temp);
}```

3.5 operator priority

1. Only three directions of combination are from right to left, and the rest are from left to right, namely assignment operator, monocular operation and monocular operation
2. Comma operators have the lowest priority
3. The approximate order of priority is arithmetic operator > relational operator > logical operator (except logical non, which has higher priority) > assignment operator > comma operator

3.6.1 concept of identifier

1. The character sequence used by C language in naming various variables and functions is called identifier
2. Any place where you can name yourself is called an identifier

3.6.2 naming rules of identifiers

1. It consists of 26 English letters in case, 0-9_ Or \$composition
2. Number cannot start
3. Keywords and reserved words cannot be used, but they can be included
4. C language is strictly case sensitive and has unlimited length
5. The identifier cannot contain spaces

3.6.3 specification of identifiers

1. Similar case sensitive identifiers shall not appear in the program
2. All macro definitions, enumeration constants and constants (read-only variables) are named in uppercase letters, and words are separated by underscores
3. Don't forget to initialize variables. When defining variables, the compiler does not necessarily empty this memory. Its value may be invalid data. When running the program, it will exit abnormally
4. Variable name and function name: when composed of multiple words, the first word is lowercase, and the first letter of each word is uppercase from the second word

3.6.4 keywords

Keyword definition: a string with special meaning given by C language for special purpose

3.7 keyboard input statement

3.7.1 introduction

In programming, if you need to receive the data input by the user, you can use the keyboard to input statements to obtain it.

3.7.2 steps

1. #include<stdio.h>
2. Using the scanf function
3. Receive input using appropriate format parameters

3.7.3 code

```#include<stdio.h>
void main() {
char name[20] = "";
int age;
double sal = 0.0;
char gender ;

printf("Please enter a name");//Prompt the user for information
scanf_s("%s", name, sizeof(name));//scanf_s("%s", name) means to receive a string and store it in the name array

scanf_s("%d", &age);//The information entered by the user is stored in the address pointed to by name, so you need to use&

scanf_s("%lf", &sal);//When using scanf, the parameter corresponding to double is% lf

scanf_s("%c", &gender, sizeof(gender));//The first scanf receives a carriage return
scanf_s("%c", &gender, sizeof(gender));//The second scanf receives user data

printf("\n Name is%s\t Age is%d\t Salary is%f\t Gender%c", name, age, sal, gender);
}```

3.8 practice

1. Define the number of seconds to save the variable, and print out xx hours, xx minutes, xx seconds
```#include<stdio.h>
void main() {
int seconds = 0;
int hours = 0;
int minutes = 0;
int leftSeconds = 0;
printf("Please enter the number of seconds:");
scanf_s("%d", &seconds);
hours = seconds / 3600;
minutes = seconds % 3600 / 60;
leftSeconds = seconds % 3600 % 60;
printf("%d hour%d branch%d second", hours, minutes, leftSeconds);
}```

2. Sort the three integers and output them in the order from small to large

```#include<stdio.h>
void main() {
int a = 0;
int b = 0;
int c = 0;
int temp = 0;
scanf_s("%d%d%d", &a, &b, &c);
if (a > b) {
temp = a;
}
else {
temp = b;
b = a;
a = temp;
}
if (a > c && b > c) {
printf("The largest of the three numbers is%d，And their order from large to small is%d %d %d", temp, a, b, c);
}
else if ( a > c &&  b < c){
printf("The largest of the three numbers is%d，And their order from large to small is%d %d %d", temp, a, c, b);
}
else {
printf("The largest of the three numbers is%d，And their order from large to small is%d %d %d", c, c, a, b);
}
}```

Keywords: C

Added by simonj on Fri, 07 Jan 2022 08:28:13 +0200