C language-learning path-02

Table of contents

Operators and Expressions

Classification of common operators
operator type effect
arithmetic operator Used to process four arithmetic operations.
assignment operator Used to assign the value of an expression to a variable.
comparison operator Used to compare expressions and return a true or false value.
Logical Operators Used to return true and false values ​​based on the value of an expression
bitwise operator Bit operations for manipulating data
sizeof operator Used to find the length in bytes.
arithmetic operator
operator the term example
+ plus sign +5
- negative -5
+ plus 3+2=5
- minus sign 3-2=1
* Multiplication sign 3*2=6
/ division sign 6/2=3
% modulo (remainder) 10%3=3
++ pre-increment a=2,b=++a;(a=3,b=3)
++ post increment a=2,b=a++;(a=3,b=2)
pre-decrement a=2.b=–a;(a=1,b=1)
post-decrement a=2.b=a–;(a=1,b=2)
assignment operator
operator the term example
= assignment a=2;b=3;
+= add equal to a=0;a+=2 (a=2)
-= minus equal to a=5;a-=3 (a=2)
*= multiply equal to a=2;a/=2 (a=1)
/= divide equal to a=4;a/=2 (a=2)
comparison operator
operator the term example
== equal to 4 == 3
!= not equal to 4 != 3
< (>) less than (greater than) 4 < 3(4>3)
<=(>=) less than or equal to (greater than or equal to) 4 <= 3(4>=3)
Logical Operators
operator the term example result
No !a If a is false, !a is true; if a is true, !a is false.
&& and a && b The result is true if both a and b are true, otherwise false.
|| or a || b If either a or b is true, the result is true, and if both are false, the result is false.
type conversion

There are different types of data. When performing mixed operations between different types of data, type conversion issues will inevitably be involved.
Conversion method:

  • Automatic conversion (implicit conversion): Follow certain rules and be automatically completed by the compilation system.
  • Mandatory type conversion: Mandatory conversion of the operation result of the expression into the required data type.
    Conversion principle:
    Types that occupy less memory bytes (small value range) are converted to types that occupy more memory bytes (large value range) to ensure that the accuracy does not decrease.
//自动转换
#include <stdint.h>
int main()
{
    
    
	int num = 5;
	printf("s1=%d\n",num / 2);
	printf("s2=%lf\n",num / 2.0);

	return 0;
}
//强制转换
#include <stdint.h>
int main()
{
    
    
	float x = 0;
	int i = 0;
	x = 3.6f;

	i = x;  //x为实型,i为整型,直接赋值会有警告
	i = (int)x;  //使用强制类型转换

	printf("x = %f,i=%d\n",x,i);

	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_50918736/article/details/130457635