当前位置:网站首页>Operators in C language
Operators in C language
2022-04-23 17:55:00 【Chshyz】
C Language operators
The precedence table of operators and ASCII surface
One 、 Arithmetic operator
- Add (+) reduce (—) ride (*) except (/)
- model ( more than ) Operator (%): Floating point is not allowed , The remainder depends on the divisor
#include <stdio.h>
int main() {
int i, b, a, c;
i= 4, b=3;
a= i+b;
c= i*a;
float p, k;
p= i/b;
k= i%a;
printf("a=%d,c=%d,p=%f,k=%f\n",a,c,p,k);
return 0;
}
[[email protected] c]# ./o
a=7,c=28,p=1.000000,k=4.000000
- Self increasing (++i,–i;i++,i–)
#include <stdio.h>
int main() {
int i;
i= 4;
printf("%d\n",++i);
return 0;
}
[[email protected]shuyi c]# ./o
5
#include <stdio.h>
int main() {
int i;
i= 4;
printf("%d\n",i--);
//printf("%d\n",i); The output is
return 0;
}
[[email protected] c]# ./o
4
Two 、 Relational operator
- Greater than (>) Less than (<) be equal to (==)
#include <stdio.h>
int main() {
int a, b, max;
printf ("please enter a and b\n");
scanf ("%d %d",&a, &b);
if (a>b) max=a;
if (a<b) max=b;
if (a==b) max=a;
printf ("max=%d\n",max);
return 0;
}
[[email protected] c]# ./o
please enter a and b
55 66
max=66
[[email protected] c]# ./o
please enter a and b
44 44
max=44
- Less than or equal to (<=) Greater than or equal to (>=) It's not equal to (!=) assignment (-=、+=、*=);
#include<stdio.h>
int main(){
int a = 10;
a +=a *=a -=20;
printf("%d\n",a);
}
[[email protected] c]# ./ass
200
#include <stdio.h>
int main() {
int a, b, max;
printf ("please enter a and b\n");
scanf ("%d %d",&a, &b);
if (a>=b) max=a;
if (a<=b) max=b;
printf ("max=%d\n",max);
return 0;
}
[[email protected] c]# ./o
please enter a and b
55 66
max=66
[[email protected] c]# ./o
please enter a and b
77 77
max=77
#include <stdio.h>
int main() {
int a, b, max;
printf ("please enter a and b\n");
scanf ("%d %d",&a, &b);
if (a!=b);
if (a>b) max=a;
if (a<b) max=b;
printf ("max=%d\n",max);
return 0;
}
[[email protected] c]# ./o
please enter a and b
44 44
max=0
[[email protected] c]# ./o
please enter a and b
44 55
max=55
3、 ... and 、 Logical operators ( also 、 perhaps 、 Unless )
PS: Priority from top to bottom
- Logic is not (! NOT)
#include <stdio.h>
int main() {
int num = 0;
printf ("Please enter num value: ");
scanf("%d", &num);
if (num != 69) {
printf ("num %d is not equal to 69.\n", num);
}
return 0;
}
~
[[email protected] c]# ./o
Please enter num value: 69
[[email protected] c]# ./o
Please enter num value: 1
num 1 is not equal to 69.
- Logic and (&& AND)
#include <stdio.h>
int main() {
int a, b, x, y;
printf ("please enter a and b,x and y\n");
scanf ("a=%d,b=%d,x=%d,y=%d",&a, &b, &x, &y);
if (a==b && x==y){
printf ("a=b,x=y\n");
}
else
printf ("sorry,I donot konw.\n");
return 0;
}
[[email protected] c]# ./o
please enter a and b,x and y
a=1,b=2,x=1,y=1
sorry,I donot konw.
[[email protected] c]# ./o
please enter a and b,x and y
a=1,b=1,x=2,y=2
a=b,x=y
- Logic or (|| OR)
Four 、 An operator
- Move right (>>) Move left (<<)
- Bitwise AND (&)
- Press bit or (|)
- Bitwise XOR (^)
- Take the opposite (~)
5、 ... and 、 Assignment operator
- Equal sign (=)
- Extended assignment operators
+= Add the assignment (a += 3 Equivalent to a = a + 3)
-= Assignment reduction
*= Take the assignment
/= In addition to the assignment
%= Complementary valuation
&= Bitwise and assignment
| = By bit or by assignment
^= Assign values by bitwise exclusive or
<<= Left shift assignment (>>= Right shift assignment )
<> When the right operand is another assignment expression , Form multiple assignment expressions
6、 ... and 、 Conditional operator
PS: Conditional operators take precedence over assignment 、 The comma operator , Lower than other operators .
- Relationship expression ? expression 1 : expression 2
Ternary operator : Conditions ? result 1 : result 2
Example :
Judge a Less than or greater than b, Output Max max
#include <stdio.h>
int main(){
int a, b, max;
scanf("a=%d,b=%d",&a,&b);
max=a>b?a:b;
printf("max=%d\n",max);
return 0;
}
[[email protected] c]# gcc -o swap swap.c
[[email protected] c]# ./swap
a=2,b=3
max=3
7、 ... and 、 The comma operator (,)
#include<stdio.h>
int main(){
int i = 1;
int a = (i+100,i++,i);
printf("%d\n",a);
return 0;
}
[[email protected] c]# ./comma
2
#include<stdio.h>
int main(){
int i = 1;
int a = (i=i+100,i++,i);
printf("%d\n",a);
return 0;
}
[[email protected] c]# ./comma
102
8、 ... and 、 Pointer operator
- Pointer to the variable (*)
Definition : Base type * Pointer variable name ;
Example 1: Access integer variables through pointer variables
#include <stdio.h>
int main() {
int a, b; # Define two int Variable
int * pointer_1, * pointer_2; # Define two pointer variables , Point to int Variable
a = 100; b = 10;
pointer_1 = &a; # Put variables a To the pointer pointer_1
pointer_2 = &b;
printf ("a = %d, b = %d \n", a, b);
printf (" * pointer_1 = %d, * pointer_2 = %d \n", * pointer_1, * pointer_2); # The value of the integer variable pointed to by the output pointer variable
return 0;
}
~
[[email protected] c]# gcc -o pointer1 pointer1.c
[[email protected] c]# ./pointer1
a = 100, b = 10
* pointer_1 = 100, * pointer_2 = 10
[[email protected] c]# vi pointer1.c
Example 2: Than the size
#include <stdio.h>
int main () {
int * p1, * p2, * p, a, b;
scanf ("%d, %d", &a, &b);
p1 = &a; p2 = &b;
if (a < b) {
p = p1; p1 = p2; p2 = p;
}
printf ("a = %d, b = %d\n", a, b);
printf ("max = %d, min = %d\n", * p1, * p2);
return 0;
}
r
[[email protected] c]# ./pointer2
4,6
a = 4, b = 6
max = 6, min = 4
Example 3: The arithmetic
#include <stdio.h>
int main () {
int maxsum = 123;
double temp = 888.8889922111111;
temp += (float)maxsum;
printf ("%7.3f, %d\n", (float)temp, (int)temp);
return 0;
}
[[email protected] c]# gcc -o compulsion compulsion.c
[[email protected] c]# ./compulsion
1011.889, 1011
Nine 、 Find byte operator (sizeof)
- When sizeof( And data types ( Such as int,float,char … etc. ) When used together , Returns the amount of memory allocated to the data type .
#include<stdio.h>
int main()
{
printf("%d\n",sizeof(char));
printf("%d\n",sizeof(int));
printf("%d\n",sizeof(float));
printf("%d\n", sizeof(double));
return 0;
}
- When sizeof When used with expressions , Returns the size of the expression .
#include<stdio.h>
int main() {
int a = 1, b = 3;
printf ("%d\n", sizeof ( a + b ));
return 0;
}
[[email protected] c]# gcc -o sizeof1 sizeof1.c
[[email protected] c]# ./sizeof1
4
Ten 、 Cast operators
example 1: Decimal to integer
#include <stdio.h>
int main () {
float f = 8.88;
int i;
i = (int) f;
printf ("f = %f, i = %d \n", f, i);
return 0;
}
[[email protected] c]# gcc -o compulsion compulsion.c
[[email protected] c]# ./compulsion
f = 8.880000, i = 8
example 2: Integer to decimal
#include <stdio.h>
int main () {
int maxsum = 123;
printf ("%lf\n", (double) maxsum);
return 0;
}
[[email protected] c]# gcc -o compulsion compulsion.c
[[email protected] c]# ./compulsion
123.000000
11、 ... and 、 member operator
- member operator (.)
- Indirect member operator (–>)
Twelve 、 Subscript operator ([ ])
PS: Precedence and associativity of operators
版权声明
本文为[Chshyz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230547413924.html
边栏推荐
- Halo open source project learning (II): entity classes and data tables
- C#字节数组(byte[])和字符串相互转换
- Detailed deployment of flask project
- Error in created hook: "referenceerror:" promise "undefined“
- 48. 旋转图像
- .105Location
- .104History
- 2022制冷与空调设备运行操作判断题及答案
- Leak detection and vacancy filling (6)
- Chrome浏览器的跨域设置----包含新老版本两种设置
猜你喜欢
Summary of floating point double precision, single precision and half precision knowledge
Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
JS high frequency interview questions
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
Kubernetes 服务发现 监控Endpoints
958. 二叉树的完全性检验
Comparison between xtask and kotlin coroutine
[appium] write scripts by designing Keyword Driven files
Cloud native Virtualization: building edge computing instances based on kubevirt
Operation of 2022 mobile crane driver national question bank simulation examination platform
随机推荐
ES6 face test questions (reference documents)
YOLOv4剪枝【附代码】
干货 | 快速抽取缩略图是怎么练成的?
Fashion classification case based on keras
92. 反转链表 II-字节跳动高频题
C1 notes [task training chapter I]
Eigen learning summary
Examination question bank and online simulation examination of the third batch (main person in charge) of special operation certificate of safety officer a certificate in Guangdong Province in 2022
圆环回原点问题-字节跳动高频题
Utilisation de la liste - Ajouter, supprimer et modifier la requête
Future usage details
[appium] write scripts by designing Keyword Driven files
The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time
Where is the configuration file of tidb server?
Oil monkey website address
C1小笔记【任务训练篇一】
Cloud native Virtualization: building edge computing instances based on kubevirt
编译原理 求first集 follow集 select集预测分析表 判断符号串是否符合文法定义(有源码!!!)
Arithmetic expression
Compilation principle first set follow set select set prediction analysis table to judge whether the symbol string conforms to the grammar definition (with source code!!!)