当前位置:网站首页>Explain C language 10 in detail (C language series)
Explain C language 10 in detail (C language series)
2022-06-26 13:03:00 【Cream dimple* ٩ ( ˊωˋ*)و*】
Catalog
Subscript reference operator :
Structure member access operator :
Preface :
In the beginning C The language editor shared some small knowledge about operators with you and learned about operators , Next, I'll take you to learn more about operators and appreciate different operators .
The operator :
Arithmetic operators :
Arithmetic operators have :/、-、+、*、%
1.% : among % Operands at both ends must be integer .
2. / : When calculating, if both ends are integers, only the integer part will be retained in the calculation result , If one of the two ends is a floating-point type, the result of the calculation is a floating-point type .( As shown in the following code )
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
float a = 6 / 5;
float b = 6.0 / 5;
float c = 6 / 5.0;
printf("%f\n", a);
printf("%f\n", b);
printf("%f\n", c);
return 0;
}Code screenshot :

Shift operator :
Shift operator :<< 、>>
<<: Move left
characteristic : The one on the left is discarded , The right to repair 0.
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 2;
int b = a << 1;
printf("%d\n", a);// remain unchanged - 2
printf("%d\n", b);// take a Move the binary bit of one bit to the left - 4
return 0;
}Code screenshot :

Analysis diagram :
>>: Move right
There is a difference between moving right and moving left , There are two ways to shift right :
1. Count right : On the right side of the discarded , Left complement sign bit .
2. Logical shift right : On the right side of the discarded , Left complement 0.

Bit operators :
Bit operators have :&、|、^
Be careful : The operand of a bitwise operator must be an integer
&: Bitwise AND ( And by bits )
example :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 3;
int b = 5;
int c = a & b;
printf("%d\n", c);// 1
return 0;
}The resolution is as follows :
|: Press bit or ( To operate on or by bits )
example :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 3;
int b = 5;
int c = a | b;
printf("%d\n", c);// 7
return 0;
}The resolution is as follows :

^: Bitwise XOR
The rules : Same as 0, Different for 1.
example :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 3;
int b = 5;
int c = a ^ b;
printf("%d\n", c);// 6
return 0;
}The resolution is as follows :

Assignment operator :
Assignment operators have :=、+=、*=、/=、>>=、<<=、%=
Be careful := Is to assign values ,== It's judgment .
Monocular operators :
Monocular operators have :!、-、+、&、sizeof、~、--、++、*、( type )
!: Logical inverse operator
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int flage = 0;
if (flage)//flage Print when true haha
printf("haha\n");
if (!flage)//flage Print when false xixi
printf("xixi\n");
return 0;
} The code runs as follows :
sizeof: Calculates the type length of the operand ( In bytes )
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
short a = 0;
int b = 0;
int arr1[10] = { 0 };
char arr2[10] = { 0 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof a);// Parentheses can be omitted when calculating as variable names
printf("%d\n", sizeof(int));// Do not omit parentheses when calculating as keywords
printf("%d\n", sizeof(b));
printf("%d\n", sizeof(arr1));// You can calculate the size of the array
printf("%d\n", sizeof(arr2));
return 0;
} The code screenshot is as follows :
~: The binary bit of a number is reversed bit by bit
&: Take the address operator
*: Dereference operator
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 4;
int* pa = &a;//*--- explain pa It's a pointer variable
*pa = 10;//*-- Yes pa Quoting
printf("%d\n", a);
return 0;
}The code screenshot is as follows :

( type ): Cast
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = (int)3.14;
printf("%d\n", a);
return 0;
}
The code screenshot is as follows :

Relational operator :
Relational operators have :>、>=、<、<=、!=、==
Logical operators :
Logical operators have :&&、||
&&: Logic and
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 2;
int b = 0;
if (a && b)// False, false
{
printf("haha\n");
}
else
{
printf("xixi\n");
}
return 0;
}The code screenshot is as follows :

||: Logic or
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 2;
int b = 0;
if (a || b)// There are really true
{
printf("haha\n");
}
else
{
printf("xixi\n");
}
return 0;
} The code screenshot is as follows :
Be careful :
1.&&: If one is 0 Then the following expressions do not need to be calculated .
2.||: If one is 1 Then the following expressions do not need to be calculated .
example :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int i = 0, a = 0, b = 2, c = 3, d = 4;
//1.&&
i = a++ && ++b && d++;
printf("a = %d\nb = %d\nc = %d\nd = %d\n", a, b, c, d);
//2.||
//i = a++ || ++b || d++;
//printf("a = %d\nb = %d\nc = %d\nd = %d\n", a, b, c, d);
return 0;
} among 1.&& The running results of are as follows :
2.|| The running results of are as follows :
Conditional operators :
The conditional operator is also called the trinocular operator ,(exp1?exp2:exp3)
if exp1 If yes, the calculation result is exp2 Otherwise, the value of exp3 Value .
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
// Use the conditional operator to calculate the maximum value
int main()
{
int a = 4;
int b = 7;
int max = (a > b ? a : b);
printf("max = %d\n", max);
return 0;
}The code screenshot is as follows :

Comma expression :
(exp1,exp2,exp3,……,expn)
The value of the comma expression is expn Value .
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 3;
int b = 4;
int c = 5;
int d = (a++, c = a + b, b = a + c);
printf("d = %d\n", d);// The result of the calculation is b Value
return 0;
} The code screenshot is as follows :
Subscript reference operator :
[ ]: Examples are as follows .
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr[5] = { 1,2,3,4,5 };
printf("%d\n", arr[3]);// among [] Reference operator for subscript
return 0;
} The screenshot of the code running is as follows :
Function call operator :
(): Examples are as follows .
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int Add(int x, int y)
{
return x + y;
}
int main()
{
int a = 2;
int b = 3;
int ret = Add(a, b);// among () Call the operator for the function
printf("ret = %d\n", ret);
return 0;
}
The screenshot of the code running is as follows :
Structure member access operator :
The structure member access operators are :.、->
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct Book
{
char name[10];
char id[10];
int price;
};
int main()
{
struct Book b = {"C Language ","0x0060",30};
struct Book* pb = &b;
// adopt . Operator to access
printf("%s\n", b.name);
printf("%s\n", b.id);
printf("%d\n", b.price);
printf("%s\n", (*pb).name);
printf("%s\n", (*pb).id);
printf("%d\n", (*pb).price);
// adopt -> To visit
printf("%s\n", pb->name);
printf("%s\n", pb->id);
printf("%d\n", pb->price);
return 0;
}The code screenshot is as follows :

Expression evaluation :
1. Implicit type conversion :
Improve the overall shape : Integer promotion is promoted according to the sign bit of variable type .
How should I fill the previous position when I am promoted ? The rules are as follows .
Signed bit :
negative : repair 1.
Positive numbers : repair 0.
Unsigned bit : High direct compensation 0.
example :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
char a = 3;
char b = 127;
char c = a + b;
printf("%d\n", c);
return 0;
} Screenshot of code running :
The analysis is shown in the figure below :

2. Arithmetic conversion :
To byte long , High precision conversion .
3. Operator properties :
influence :
①: The priority of the operator .
②: The associativity of operators .
③: Whether to control the order of evaluation .
summary : Although we have the precedence of many operators , But the expression we write cannot determine the unique calculation path through the properties of the operator , So this expression is the problem expression .
Conclusion :
This time I share with you C The operators we often see in languages , I hope that's helpful , Students who want to learn remember to pay attention to Xiaobian and study together ! If there are any mistakes in the article, you are also welcome to point out the maze for Xiaobian in time ( I'd like to thank you guys first !)
边栏推荐
- solo 博客系统的 rss 渲染失败
- RSS rendering of solo blog system failed
- Photoshop 2022 23.4.1增加了哪些功能?有知道的吗
- 【网络是怎么连接的】第二章(中):一个网络包的发出
- PostGIS geographic function
- LeetCode_栈_中等_150. 逆波兰表达式求值
- HDU1724[辛普森公式求积分]Ellipse
- Basic principle and application routine of Beifu PLC rotary cutting
- 详细讲解C语言10(C语言系列)
- Guacamole installation
猜你喜欢

Group counting practice experiment 9 -- using cmstudio to design microprogram instructions based on segment model machine (2)

This function has none of deterministic, no SQL solution

opencv高速下载

Solution of Splunk iowait alarm
![[BSidesCF 2019]Kookie 1](/img/22/585d081668e67b8389a1b90aaebe9d.png)
[BSidesCF 2019]Kookie 1

Openlayers drawing dynamic migration lines and curves

ES6:Map

Power Designer - Custom Comment button

Research and development practice of Kwai real-time data warehouse support system

Redis learning - 05 node JS client operation redis and pipeline pipeline
随机推荐
How does easygbs solve the abnormal use of intercom function?
OPLG: 新一代云原生可观测最佳实践
Group counting practice experiment 9 -- using cmstudio to design microprogram instructions based on segment model machine (2)
opencv高速下载
Word文档导出(使用固定模板)
第十章 设置结构化日志记录(二)
C语言:练习题二
EasyGBS如何解决对讲功能使用异常?
Sharing ideas for a quick switch to an underlying implementation
倍福PLC选型--如何看电机是多圈绝对值还是单圈绝对值编码器
POJ 3070 Fibonacci
C# 结构体:定义、示例
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
初识-软件测试
数字信号处理——线性相位型(Ⅰ、Ⅲ型)FIR滤波器设计(1)
【shell】生成指定日期之间的字符串
Goto statement to realize shutdown applet
KVM video card transparent transmission -- the road of building a dream
Go 结构体方法
Deep parsing MySQL binlog