Article directory
- 1. Data type
- 2. Constants
- 3. Variables
- 4. Operators and expressions
-
- 1. Assignment operators and assignment expressions
- 2. Arithmetic operators and arithmetic expressions
- 3. Comma operator and comma expression
- 4. Find the byte number operator
- 5. Relational operators and relational expressions
- 6. Logical operators and logical expressions
- 7. Conditional operators and conditional expressions
1. Data type
1. Data type conversion
(1) Automatic type conversion
When different types of data are operated together, the compilation system will automatically perform type conversion . The rules for automatic conversion are:
Convert the (low-level) type that takes up less memory space to the (high-level) type that takes up more space to ensure the accuracy of the operation
As shown in the picture:
- The horizontal arrows in the figure represent certain conversions. For example, char type and short type data must be converted to int type first. Float type data will be converted to double type first during operation to improve the calculation accuracy (even if two float type data are added) , also need to be converted into double type, and then added)
- The vertical arrows in the figure indicate the conversion method when the data types used for operations are still different after horizontal conversion. During the conversion process of different data types, the type conversion sequence is not a step-by-step conversion in the direction of the arrow, and there is no need for intermediate type conversion . For example, to perform operations on int type and double type data, first convert the int type data directly into double type, and then perform operations on two data of the same type (double type).
- Conversion only affects the operation result type of the expression and does not change the definition type of the original variable, and its data value will not change in any way.
- When the types of the operands on both sides of the assignment operator are different, type conversion also occurs. The conversion rule is: convert the type of the expression on the right side of the assignment operator to the type of the variable on the left .
(2) Forced type conversion
(type specifier) (expression)
-
Function : Force the result of an expression into the type specified by the type specifier.
For example:#include<stdio.h> int main() { int a=3,b=4; float c; c=(float)(a+b)/2;//将int型 7 强制转换为float型 7.0 printf("c=%f\n",c); return 0; }
2. Constants
1. Direct constant
(1) Integer constant
That is, integers, including positive integers, negative integers, and zero
a. Decimal representation
- It consists of positive and negative signs and numbers 0~9, example -123
b. Octal representation
- Composed of the number 0 (prefix) and the numbers 0~7, example 045
- Generally an unsigned integer
c. Hexadecimal representation:
- It consists of the prefix 0x or 0X and the numbers 0~9 or letters A~F (representing 10~15, both upper and lower case), for example 0x12cD
- Generally an unsigned integer
(2) Real constants
- That is, real numbers, also known as floating point numbers, can only be expressed in decimal in C language
- Real constants default to real numbers of double type. If f or F (such as 1.23f) is added after the number, it is considered to be a real number of float type.
a. Expressed in decimal form
- It consists of integer part, decimal point and decimal part, Example-12.34;
- When the integer part or decimal part is 0, it can be omitted, but the decimal point cannot be omitted, and there must be numbers on at least one side before and after the decimal point, Example 12. and -.123
b. Expressed in exponential form
-
It consists of the mantissa part, the letter e or E and the exponent part. The format is: ±mantissa E (e) exponent, for example -1.2E-2, which means -1.2 x (10) -2 ; 1.2e2, which means 1.2 x (10) 2
-
There must be numbers before and after the letter E (or e) and the exponent part can only be an integer
(3) Character constant
A single character enclosed by single quotes, such as 'A'
- It can be any character in the ASCII character set (that is, it can participate in operations like integers)
- Each character occupies one byte in memory
- The escape character only occupies one byte in memory.
(4) String constant
A sequence of zero or more characters enclosed in double quotes, such as "He MengYuan"
- Space characters, escape characters, null characters and other characters can be used in strings, as well as text characters such as Chinese characters
- String constants occupy a continuous storage unit in memory
- The system automatically adds the end mark ' \0 ' after each string , so a string of n characters occupies n+1 bytes of space.
- You can use the sizeof operator (see 4) to calculate the memory space occupied by a string.
- You can use the strlen() function to calculate the string length
2. Symbolic constants
In C language, an identifier can be used to represent a constant, called a symbolic constant. Symbolic constants must be defined before use. The general form is:
#define 标识符 常量值 //#define 宏定义命令
例如:#define PI 3.14
//在需要3.14时使用PI就可以
3. Variables
1. Character variables
char c1,c2;
- It has numerical characteristics and can participate in operations like integers.
- It is allowed to assign character values to integer variables, and it is also allowed to assign integer values to character variables.
- It is allowed to output character variables as integers, and it is also allowed to output integers as characters.
2. Real type variables
Keywords | type | Number of bytes occupied | Ranges |
---|---|---|---|
float | Single precision type | 4 | 3.4E-38~3.4E+38 |
double | Double type | 8 | 1.7E-308~1.7E+308 |
- Single-precision data can retain 7 significant digits, and subsequent data is invalid.
- Double-precision data can retain 16 significant digits, but in VS it can retain up to 6 decimal digits, and the 7th decimal digit needs to be rounded off.
For example:
#include<stdio.h>
int main()
{
float a = 1234.567689456;
double b = 1234.567895678;
printf("a=%f,b=%f\n", a, b);
return 0;
}
The result is:
a=1234.567749,b=1234.567896
3. Integer variable
Keywords | type | Number of bytes occupied | Ranges |
---|---|---|---|
short int | Short | 2 | -32768~32767(-215~215-1) |
int | integer | 4 | -2147483648~2147483647(-231~231-1) |
long int | long integer | 4 | -2147483648~2147483647(-231~231-1) |
unsigned short | unsigned short | 2 | 0~65535(0~216-1) |
unsigned int | unsigned integer | 4 | 0~4294967295(0~232-1) |
unsigned long | unsigned long integer | 4 | 0~4294967295(0~232-1) |
Note: The above data is only in the Visual C++2010 environment. The memory size occupied by various types of data varies with different compilation systems.
4. Operators and expressions
1. Assignment operators and assignment expressions
(1) Simple assignment operator
equal sign =
int a,b,c=1;
//等价于
int a=1,b=1,c=1;
//但千万不能像下面这样写
int a=b=c=1;
(2) Compound assignment operator
operator | Application examples | equivalent form |
---|---|---|
+= | a+=x | a=a+(x) |
-= | a-=x | a=a-(x) |
*= | a*=x | a=a*(x) |
/= | a/=x | a=a/(x) |
%= | a%=x | a=a%(x) |
(3) Assignment expression
variable = expression
-
If the types of the left and right sides of "=" are different, the system will convert the type on the right into the type on the left according to the principle of assignment type conversion.
For example:#include<stdio.h> int main() { int a; float b=123.456; a = b; printf("%d\n", a); return 0; }
The result is:
123
2. Arithmetic operators and arithmetic expressions
(1) Basic arithmetic operators
operator | + | - | * | / | % |
---|---|---|---|---|---|
name | add | reduce | take | remove | Take remainder |
-
/ Division operator: When calculating a/b, if a and b are both integer types, their quotient is also an integer type , and the decimal part is discarded, for example 1/2=0; if either a or b is a real type, then both a and b are converted to double type , and then divided, and the result is double type. For example 1.0/2=0.5,
see the following code:#include<stdio.h> int main() { float a; a = 1 / 2*3;//计算3的一半 printf("%f\n", a); return 0; }
Our ideal result should be:
1.500000
But for:
0.000000
The correct code should be:
#include<stdio.h> int main() { float a; a = 1.0 / 2*3;//计算3的一半 printf("%f\n", a); return 0; }
-
% Remainder operator: The result is the remainder after dividing two numbers. It is required that the two operands involved in the operation must be integers , and the result is also an integer. Example 7%2=1; The sign of the remainder operation result Same sign as the first operand, example 7%-2=1
(2) Auto-increment and auto-decrement operators
-
Auto-increment operator: adds one to the value of a variable (take i as an example below)
i++: Use it first and then add 1
++i: Add 1 first and then use it
Difference:#include<stdio.h> int main() { int i=1; printf("i=%d\n",++i);//打印i的值 printf("i=%d\n",i++);//打印i的值 return 0; }
operation result:
i=2 i=2
-
Auto-decrement operator: Decrease the value of the variable by one
i- -: Use it first and then subtract 1
- -i: Add 1 first and then use
it Difference: Similar to auto-increment
(3) Arithmetic expression
An expression connected by arithmetic operators, operands and parentheses. The operands can be constants, variables, functions, etc.
- After the expression is evaluated, a certain value and type will be obtained. The type is determined by the specific operator and operand used. For example: there is a definition of "char a=0;int b=1;float c=2;double d= 3;", then the value of the expression "a+b+c+d" is of double type
- The multiplication sign cannot be omitted
- Multiple levels of parentheses are available and only parentheses can be used
3. Comma operator and comma expression
(1) Comma operator
Comma is also an operator. Its function is to connect multiple expressions to form one expression, that is, comma expression.
(2) Comma expression
expression 1, expression 2,…, expression n
-
Execution order: Calculate the value of each expression in sequence from left to right, and use the value of the last expression as the value of the entire comma expression
-
Not all places where commas appear are comma expressions. Sometimes commas are just used as separators for variables .
Code example:#include<stdio.h> int main() { int i,j=7; float k = 5; i = (j = j + 2, j / k); printf("i=%d\n",i); return 0; }
operation result:
i=1
4. Find the byte number operator
sizeof(expression) or sizeof expression
sizeof(data type name)
Code example:
#include<stdio.h>
int main()
{
int a, b;
a = sizeof(3 + 5.0);
b = sizeof 3 + 5.0;
printf("%d,%d,%d\n", a, b, sizeof("china"));
return 0;
}
The running result is:
8,9,6
In VS2022:
#include<stdio.h>
int main()
{
printf("%d\n", sizeof(char));//1
printf("%d\n", sizeof(float));//4
printf("%d\n", sizeof(double));//8
printf("%d\n", sizeof(short int));//2
printf("%d\n", sizeof(int));//4
printf("%d\n", sizeof(long int));//4
printf("%d\n", sizeof(unsigned short));//2
printf("%d\n", sizeof(unsigned int));//4
printf("%d\n", sizeof(unsigned long));//4
return 0;
}
5. Relational operators and relational expressions
(1) Relational operators
operator | meaning |
---|---|
> | more than the |
>= | greater or equal to |
< | less than |
<= | less than or equal to |
== | equal |
!= | not equal to |
(2) Relational expression
expression 1 relational operator expression 2
- "=" is an assignment operator, which represents assignment; "==" is a relational operator, which represents judgment.
- Non-0 is true, 0 is false, false is 0, and true may be 1. For example: the value of the relational expression "(a=3)>(b=8)" is 0
- Judging the equality of real numbers may not yield correct results. For example: the result of "1.0/3*3.0==1.0" is 0
6. Logical operators and logical expressions
(1) Logical operators
operator | ! | && | || | |
---|---|---|---|---|
name | logical negation | logical AND | logical or |
(2) Logical expression
expression1 logical operator expression2
or logical operator expression1
- In a logical expression composed of several sub-expressions, calculation is performed from left to right. When the value of a sub-expression is calculated to determine the value of the entire logical expression, the remaining sub-expressions on the right will not be calculated thereafter. value of the formula, this situation is called a "short circuit"
- If one of the expressions around && is false, the result is false
- || If one of the left and right expressions is true, the result is true
7. Conditional operators and conditional expressions
(1) Conditional operator
It consists of two symbols "?" and ":"
(2) Conditional expression
Expression 1? Expression 2: Expression 3
- Execution order : Calculate the value of expression 1 first. If the value is non-0 and the expression condition is true, then the value of expression 2 is used as the value of the entire conditional expression. Otherwise, the value of expression 3 is used as the entire conditional expression. value. For example, the value of 4>3?5:7 is 5
- The type of expression 1 in the conditional expression can be different from expression 2 and expression 3, and the type of expression 2 and expression 3 can also be different. In this case, the system will automatically convert, and the result will be expression 2 and expression 3. The medium-level type is higher and used as the type of the conditional expression. For example, the result of "'a'?1:2.0" is double type 1.0
- Sequential nesting: "a>3?b:c>2?1:0", according to the right associativity of the conditional expression , it is equivalent to "a>3?b:(c>2?1:0)"