当前位置:网站首页>C language -- 7 operators
C language -- 7 operators
2022-06-10 21:35:00 【Try!】
1、 arithmetic operator
Arithmetic operators include + - * / %( Add 、 reduce 、 ride 、 except 、 modulus / Remainder )
#include <stdio.h>
// The operator
int main()
{
int a = 9 / 2;
printf("%d\n",a);
return 0;
}
Put statement int a = 9 / 2;printf("%d\n",a); Change it to float a = 9 / 2; printf("%f\n",a);, Run results from 4 Turn into 4.000000, I can't figure it out 4.5, because C Division in language only calculates quotient , Do not calculate the remainder , I can't work out decimals . therefore 9/2=4, If you want to ask for accurate results , You can change any number at both ends of the division sign to a decimal , You can get accurate results .
2、 The displacement operator
Displacement operators include <<( Move left ) and >>( Move right )
int main()
{
int a = 2;
int b = a << 1;
printf("%d\n",b);
return 0;
}
The running result is 4. Because the shift left operator moves bits .a=2----》 Expressed as binary is 10, because a It is stored in the integer , The memory size occupied by integer is 4 Bytes ,4 Bytes are 32 A bit , therefore :
After moving one bit left , The leftmost 0 The loss of , Fill in zero on the far right , Turn into
00000000 00000000 00000000 00000100, Expressed in decimal system 4.( Shift left , The last one will always fill in zero )
3、 Bit operators
Bit operators include Bitwise AND &、 Press bit or |、 Bitwise XOR ^
4、 Assignment operator
Assignment operators include := += -= *= /= &= ^= |= >>= <<=
5、 Monocular operators
| ! | Logical anti operation |
|---|---|
| - | negative |
| + | Comes at a time |
| & | Address fetch |
| * | Indirect access operators ( Dereference operator ) |
| sizeof | The type and length of the operator ( In bytes ) |
| ~ | To reverse the binary of a number |
| – | In front of 、 After – |
| ++ | In front of 、 After ++ |
| type | Cast |
explain : about “a+b” Come on ,“+” Yes 2 Operands , Therefore, it is a binocular operator ; The unary operator has only one operand .
(1) Logical anti operation
stay C There are provisions in the language :0 Said the false , Not 0 It's true .
int main()
{
int a = 10;
printf("%d\n",!a);
return 0;
}
The running result is :0. because 10 It's true , so !10 For false , So the result is 0.
int main()
{
int a = 0;
printf("%d\n",!a);
return 0;
}
The running result is 1. When the false becomes true , There are many real numbers , The specified result is 1.
“!” The real use of :
int main()
{
if (a)
{
// When a It's true , What to do
}
if (!a)
{
// When a For false , What else to do
}
return 0;
}
(2)sizeof Is an operator , Used to calculate the size of a variable or type .
int main()
{
int a = 10;
printf("%d\n",sizeof(int));
printf("%d\n", sizeof(a));
return 0;
}
The result of running is 4, because a The type is int type .printf("%d\n", sizeof(a)); in sizeof The following parentheses can be omitted , signify sizeof It's the operator , It's not a function . But the parentheses after the function cannot be omitted .sizeof To find variables , Brackets can be omitted , But when it comes to types , Brackets cannot be omitted , Such as :sizeof(int). But usually , Use sizeof When , The brackets are not omitted .
reflection :sizeof Can I calculate the size of an array ?
int main()
{
int arr[10] = {
0 };
printf("%d\n",sizeof(arr));
printf("%d\n", sizeof(arr[0]));
int sz = sizeof(arr) / sizeof(arr[0]);// Count the number of elements
printf("%d\n",sz);
return 0;
}
The running result is :
40
4
10
Because the defined array contains 10 individual 0 Array of elements , And this 10 individual 0 Each of the zero elements is a int type , Every int Types are 4 Bytes , So for the entire array , Its length is 10x4 be equal to 40 individual . For the first element in the array , Its length is 4 Bytes .
(3) According to the not
“~” Reverse by bit , Invert by bit , Invert all binary bits ,1 Turn into 0,0 Turn into 1.
int main()
{
int a = 0;
printf("%d\n",~a);
return 0;
}
The running result is :-1
For positive integers , Its original code 、 Complement code 、 The inverse code is the same .
(4) In front of ++、 After ++
int main()
{
int a = 10;
int b = ++a;// In front of ++: First ++, After use
printf("%d\n",a);
printf("%d\n", b);
return 0;
}
The running result is :
11
11
In front of ++: First, let a perform ++ operation , And then put this time a The value is assigned to b, So the running results are 11
int main()
{
int a = 10;
int b = a++;// After ++: First use , after ++
printf("%d\n", a);
printf("%d\n", b);
return 0;
}
The running result is :
10
11
After ++: The first a The value is assigned to b, then a Re execution ++ operation .
(5) Type is put in brackets –》“( type )” Cast to type
int main()
{
int a = 3.14;
printf("%d\n",a);
return 0;
}
When running the program , Although not wrong , But it will warn :warning C4244: “ initialization ”: from “double” The switch to “int”, Possible loss of data
take int a = 3.14; Change it to int a = (int)3.14; It can run correctly .
6、 Relational operator
The relationship operator includes :> >= < <= !=( Used for testing “ It's not equal ”) ==( Used for testing “ equal ”)
7、 Logical operators
Logical operators include &&( Logic and )、||( Logic or )
int main()
{
int a = 3;
int b = 5;
int c = a && b;
printf("%d\n",c);
return 0;
}
The running result is :1, because 3 And 5 It's all true , So the two do “ Logic and ” The result of the operation is also true .
If the b=5 Change it to b=0, The result is 0
take int c = a && b; Change it to int c = a|| b;, Then the running result is still 1.
8、 Conditional operators ( ternary operators )
exp1?exp2:exp3, Meaning for exp1 establish , The calculation exp2, The result of the whole expression is exp2 Result ; if exp1 Don't set up , Then the result of the whole expression is exp3 Result .
int main()
{
int a = 0;
int b = 3;
int max = 0;
if (a > b)
max = a;
else
max = b;
printf("%d\n", max);
return 0;
}
In this code if else Structure can be written :
max = a > b ? a : b;
9、 Comma expression
A comma expression is an expression separated by commas exp1,exp2,...,expN, The characteristic of comma expression is that it will evaluate from left to right , The result of the entire comma expression is the result of the last expression .
int main()
{
int a = 0;
int b = 3;
int c = 5;
int d = (a = b + 2, c = a - 4,b=c+2);
printf("%d\n",d);
return 0;
}
The running result is a3. After running this code ,a=3+2=5;c=5-4=1;b=c+2=1+2=3
10、 Subscript reference 、 Function calls and structure members
[] () . ->
int main()
{
int arr[10] = {
1,2,3,4,5,6,7,8,9,10};
printf("%d\n",arr[5]);
return 0;
}
In this code arr[5] The square brackets of are the subscript reference operators .
Another is when calling a function , After the function name () Is the function call operator , such as printf("hello\n")
边栏推荐
- Practical | how to use burp suite for password blasting!
- Read the source code of micropyton - add the C extension class module (1)
- CCF class a conference or journal - regression related papers
- Read the source code of micropyton - add the C extension class module (2)
- "O & M youxiaodeng" self service account unlocking tool
- ROS virtual time
- 蛮力法/u到v是否存在简单路径
- Attack and defense drill | network security "whistleblower": security monitoring
- Leetcode advanced road - 167 Sum of two numbers II - input ordered array
- Rotate menu 2.0
猜你喜欢

Self attention and multi head attention

^29事件循环模型

Redis缓存击穿

Redis缓存穿透

实用 | 如何利用 Burp Suite 进行密码爆破!

数据库系统概论 ---- 第一章 -- 绪论(重要知识点)

Calculus review 1

Construction of RT thread smart win10 64 bit compilation environment

Quick start to elastic job, three minutes to experience distributed scheduled tasks

Notes to entry: do I need to know programming for O & M?
随机推荐
[computer use] how to set software startup without auto startup
Software definition boundary (SDP)
Shell implements SSH login and executes commands
LeetCode 进阶之路 - 169.多数元素
蛮力法/邻接表 深度优先 有向带权图 无向带权图
编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
Construction of RT thread smart win10 64 bit compilation environment
Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)
Rotate menu 3.0
蛮力法/1~n的全排列 v3 递归
Redis缓存击穿
Unity analyzes the rendering of built-in terrain and does some interesting things
Brute force method /k integers out of 1~n integers
LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
蛮力法/任务分配
Is it safe to buy funds on mobile phones? Will the principal be swallowed?
H.264中NALU、RBSP、SODB的关系
Signal and system review 1
app测试用例
01js basic null and undefined difference type conversion = = code block logical operator