文末附运算符的优先表和ASCII表
一、算术运算符
#include
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;
}
#include
int main()
{
int i;
i= 4;
printf("%d\n",++i);
return 0;
}
#include
int main()
{
int i;
i= 4;
printf("%d\n",i--);
//printf("%d\n",i);
return 0;
}
二、关系运算符
#include
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
#include
int main()
{
int a = 10;
a +=a *=a -=20;
printf("%d\n",a);
}
#include
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;
}
#include
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
三、逻辑运算符(并且、或者、除非)
优先级从上至下
#include
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;
}
#include
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;
}
#include
int main()
{
int a = 1;
int b = 0;
printf("%d\n",a || (b++));
printf("%d\n",b);
}
#include
int main()
{
int a = 0;
int b = 0;
printf("%d\n",a || (b++));
printf("%d\n",b);
}
#include
int main()
{
int a = 0;
int b = 0;
printf("%d\n",a || (++b));
printf("%d\n",b);
}
#include
int main(){
printf("%d\n",1 && 1);
printf("%d\n",0 && 1);
printf("%d\n",0 && 0);
printf("%d\n",1 || 1);
printf("%d\n",0 || 1);
printf("%d\n",0 || 0);
return 0;
}
优先级:算术运算符>关系运算符>赋值运算符
四、位运算符
右移(>>)左移(<<)
按位与(&)
按位或(|)
按位异或(^)
取反(~)
五、赋值运算符
等号(=)
扩展赋值运算符
+= 加赋值 (a += 3 等价于 a = a + 3)
-= 减赋值
*= 乘赋值
/= 除赋值
%= 求余赋值
&= 按位与赋值
| = 按位或赋值
^= 按位异或赋值
<<= 左移位赋值(>>= 右移位赋值)
<> 当右操作数又是一个赋值表达式时,形成多重赋值表达式
六、条件运算符
条件运算符优先级高于赋值、逗号运算符,低于其他运算符。
//例子:判断a小于或者大于b,输出最大值max
#include
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;
}
#include
int main()
{
int a = 300;
int b = 50;
int c = a>b? 350: a
int main()
{
int a = 50;
int b = 300;
int c = a>b? (b-a): a
七、逗号运算符
#include
int main()
{
int i = 1;
int a = (i+100,i++,i);
printf("%d\n",a);
return 0;
}
#include
int main()
{
int i = 1;
int a = (i=i+100,i++,i);
printf("%d\n",a);
return 0;
}
八、指针运算符
#include
int main()
{
int a, b; #定义两个int变量
int * pointer_1, * pointer_2; #定义两个指针变量,指向int变量
a = 100; b = 10;
pointer_1 = &a; #把变量a的地址赋给指针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); #输出的指针变量所指向的整型变量的值
return 0;
}
九、求字节数运算符(sizeof)
#include
int main()
{
printf("%d\n",sizeof(char));
printf("%d\n",sizeof(int));
printf("%d\n",sizeof(float));
printf("%d\n", sizeof(double));
return 0;
}
#include
int main()
{
int a = 1, b = 3;
printf ("%d\n", sizeof ( a + b ));
return 0;
}
十、强制类型转换运算符
例1:小数转整数
#include
int main ()
{
float f = 8.88;
int i;
i = (int) f;
printf ("f = %f, i = %d \n", f, i);
return 0;
}
例2:整数转小数
#include
int main ()
{
int maxsum = 123;
printf ("%lf\n", (double) maxsum);
return 0;
}
十一、成员运算符
十二、下标运算符([ ])
运算符的优先级和结合性