操作符分类
算数操作符
移位操作符
位操作符
赋值操作符
单目操作符
关系操作符
逻辑操作符
条件操作符
都好表达式
下标引用,函数调用和结构成员
算术操作符
+ - * / %
1.除了%操作符之外,其他的几个操作符均可以作用于整数和浮点数。
2.对于/操作符而言,如果两个操作数均为整数,则执行整数除法。而只要有浮点数执行的就是浮点数除法。
3.%操作符的两个操作数必须为整数。返回的是整除之后的余数。
#include <stdio.h>
int main()
{
int ret = 10 / 3;
printf("%d\n", ret);
double ret2 = 10.0 / 3;
printf("%lf\n", ret2);
printf("%.1lf\n", ret2);
int ret3 = 10 % 3;
printf("%d\n", ret3);
return 0;
}
移(二进制)位操作符( <<左移操作符,>>右移操作符)
整数的二进制表示形式有三种:原码,反码, 补码。
原码:把一个整数按照其正负直接翻译成二进制就是原码。
反码:原码的符号位不变,其他位按位取反就是反码。
补码:反码+1。
注意事项:
- 移位操作符的操作数只能是整数。
- 正整数的原码,反码,补码是相同的。负整数的原码,反码,补码需要计算。
- 整数在内存中是以补码的形式存在的(二进制)。
- 左移操作符:
-(不管是正整数还是负整数,向左移动位数则左侧数据丢失相应位数,右侧尽数补0。) - 右移操作符:
- 1.(算术右移:右侧丢弃,左侧直接用操作数原来的符号位填充。)
- 2.(逻辑右移:右侧丢弃,左侧直接用0填充。)
- 注意:在右移的时候究竟采用的是逻辑右移还是算术右移,是取决于编译器的。
//正数的原码,反码,补码都是相同的
int main()
{
int a = 10;
//00000000000000000000000000001010 原码
//00000000000000000000000000001010 反码
//00000000000000000000000000001010 补码
int b = -10;
//10000000000000000000000000001010 原码
//11111111111111111111111111110101 反码
//11111111111111111111111111110110 补码
return 0;
}
#include <stdio.h>
int main()
{
int a = 3;
//00000000000000000000000000000011,正整数的原码,反码,补发一致
int b = a << 1;
printf("%d\n", b);//6
//00000000000000000000000000000110,二进制补码向左移动一位,右侧补0
printf("%d\n", a);//3
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
printf("%d\n", a);
int b = a << 1;
printf("%d\n", b);
return 0;
}
#include <stdio.h>
int main()
{
int a = -3;//内存中以补码形式存储,移动的是补码的二进制位。
//原码:10000000000000000000000000000011
//反码:11111111111111111111111111111100
//补码:11111111111111111111111111111101
int b = a << 1;
//b的补码:11111111111111111111111111111010
//b的反码:11111111111111111111111111111001
//b的原码:10000000000000000000000000000110
printf("%d\n", b);//-6
printf("%d\n", a);//-3
return 0;
}
//如何将补码变成原码:1.补码-1再取反。2.补发取反再+1。
#include <stdio.h>
int main()
{
int a = -1;
printf("%d\n", a);
int b = a >> 1;
printf("%d\n", b);
return 0;
}
#include <stdio.h>
int main()
{
int a = -5;
//原码:10000000000000000000000000000101
//反码:11111111111111111111111111111010
//补码:11111111111111111111111111111011
int b = a >> 1;
//补码:11111111111111111111111111111101
//反码:11111111111111111111111111111100
//原码:00000000000000000000000000000011
printf("b = %d\n",b);//-3
printf("a = %d\n",a);//-5
return 0;
}
位操作符
& 按位与:全为1才为1
| 按位或:遇1就为1
^ 按位亦或
注意:他们的操作数只能是整数
#include <stdio.h>
int main()
{
int a = 3;
//原反补码:00000000000000000000000000000011
int b = -5;
//原码:10000000000000000000000000000101
//反码:11111111111111111111111111111010
//补码:11111111111111111111111111111011
int c = a & b;//遇0为0
//补码:00000000000000000000000000000011
//补码:11111111111111111111111111111011
//C的原码反码补码:00000000000000000000000000000011
printf("%d\n", c);//3
int d = a | b;
//补码:00000000000000000000000000000011
//补码:11111111111111111111111111111011
//d的补码:11111111111111111111111111111011
//d的反码:11111111111111111111111111111010
//d的原码:10000000000000000000000000000101
printf("%d\n", d);//-5
return 0;
}
#include <stdio.h>
int main()
{
int a = 3;
//原反补码:00000000000000000000000000000011
int b = -5;
//原码:10000000000000000000000000000101
//反码:11111111111111111111111111111010
//补码:11111111111111111111111111111011
int c = a ^ b;//相同为0,相亦为1
//c的补码:11111111111111111111111111111000
//c的原码(补码取反再+1):10000000000000000000000000001000
printf("%d\n", c);//-8
return 0;
}
//利用第三个变量来交换变量中原本存储的数据
#include <stdio.h>
int main()
{
int a = 3;
int b = 5;
printf("交换前:%d %d\n", a, b);
int tmp = a;
a = b;
b = tmp;
printf("交换后:%d %d\n", a, b);
return 0;
}
//缺陷:当数据太大时会遗失数据
#include <stdio.h>
int main()
{
int a = 3;
int b = 5;
printf("交换前:%d %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("交换后:%d %d\n", a, b);
return 0;
}
#include <stdio.h>
int main()
{
int a = 3;
int b = 5;
printf("交换前:%d %d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("交换后:%d %d\n", a, b);//5 3
return 0;
}
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0, d = 0;
printf("%d %d %d %d\n", a, b, c, d);
return 0;
}
#include <stdio.h>
int main()
{
int a = 0;
printf("%d\n", ~a);
return 0;
}
赋值操作符
+=
-=
*=
/=
%=
<<=
>>=
&=
|=
^=
#include <stdio.h>
int main()
{
int a = 0;
a += 1;
printf("%d\n",a);
return 0;
}
单目操作符
#include <stdio.h>
int main()
{
int flag = 5;
int tmp = 0;
if (flag)
{
printf("You're right.\n");
}
if (!tmp) //!这个单目操作符能够变假为真
{
printf("yes.");
}
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
int* p = &a;
*p = 20;//间接访问操作符
printf("%d\n",a);
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
int arr[10] = {0};
int sz = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", sizeof(a));
printf("%d\n", sizeof a);
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr[0]));
printf("%d\n", sz);
return 0;
}
#include <stdio.h>
int main()
{
int a = 0;
//00000000000000000000000000000000
printf("%d\n",~a);//-1
//11111111111111111111111111111111 补码
//11111111111111111111111111111110 反码
//10000000000000000000000000000001 原码
return 0;
}
#include <stdio.h>
int main()
{
int a = 3;
//00000000000000000000000000000011
a |= (1 << 3);
printf("%d\n",a);
//00000000000000000000000000001011
//11111111111111111111111111110111
a &= (~(1 << 3));
printf("%d\n",a);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
_Bool is_leap_year(int y)
{
for (y = 1000; y <= 2000; y++)
{
if (((y % 4 == 0 && y % 100 != 0)) || (y % 400 == 0))
return true;
else
return false;
}
}
int main()
{
int y = 0;
scanf("%d", &y);
int ret = is_leap_year(y);
printf("%d ", ret);
}
#include <stdio.h>
int main()
{
int a = 10;
int b = -10;
printf("%d %d %d %d\n", a,-a,b,-b);
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
int b = -10;
printf("%d %d %d %d\n", a, +a, b, +b);
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
printf("%p", &a);
int* pa = &a;
char ch = 'w';
char* pc = &ch;
char arr[10] = { 0 };
char* p2 = arr;
char* p3 = &arr[0];
char* p = "abcdef";
printf("%p\n", p);
printf("%c\n", *p);
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
int* pa = &a;
*pa = 20;
printf("%d\n", a);//20
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
printf("%d ",sizeof(a));
printf("%d ",sizeof a);
printf("%d ",sizeof(int));
int arr[10] = { 0 };
printf("%d ", sizeof arr);
printf("%d ", sizeof(arr));
printf("%d ", sizeof(int[10]));
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
printf("%d ",a++);//10
printf("%d ",a);//11
int b = a++;
printf("%d %d\n",a,b);//12 11
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
int b = ++a;
printf("%d %d\n", a, b);
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
int b = a++;
printf("%d %d\n", a, b);
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
printf("%d\n", ++a);
printf("%d\n", a);
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
printf("%d\n", a++);
printf("%d\n", a);
return 0;
}
#include <stdio.h>
void test(int b)
{
printf("%d\n", b);//10
}
int main()
{
int a = 10;
test(a++);
return 0;
}
#include <stdio.h>
void test(int b)
{
printf("%d\n", b);//11
}
int main()
{
int a = 10;
test(++a);
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
printf("%d\n", a--);//10
printf("%d\n", a);//9
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
printf("%d\n", --a);//9
printf("%d\n", a);//9
return 0;
}
#include <stdio.h>
void test1(int arr[])
{
printf("%zd\n", sizeof(arr));
}
void test2(char ch[])
{
printf("%zd\n", sizeof(ch));
}
int main()
{
int arr[10] = { 0 };
char ch[10] = { 0 };
printf("%zd\n", sizeof(arr));
printf("%zd\n", sizeof(ch));
test1(arr);
test2(ch);
return 0;
}
#include <stdio.h>
void test1(int arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void test2(int* arr, int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
test1(arr,sz);
test2(arr, sz);
return 0;
}
关系操作符
>
>=
<
<=
!=
==
逻辑操作符
#include <stdio.h>
int main()
{
int a = 10;
int b = 0;
int c = 10;
int d = 0;
if (a && b)
{
printf("haha\n");
}
if (a && c)
{
printf("xixi\n");
}
if (a || b)
{
printf("hehe\n");
}
if (a && d)
{
printf("yes\n");
}
return 0;
}
#include<stdio.h>
//判断是否为闰年
int main()
{
int y = 2048;
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
{
printf("yes.\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int i = 0, a = 0, b = 2, c = 3, d = 4;
i = a++ && ++b && d++;
printf("a=%d\nb=%d\nc=%d\nd=%d\n",a,b,c,d);
return 0;
}
#include <stdio.h>
int main()
{
int i = 0, a = 1, b = 2, c = 3, d = 4;
i = a++ && ++b && d++;
printf("a=%d\nb=%d\nc=%d\nd=%d\n", a, b, c, d);//2 2 3 4
return 0;
}
#include <stdio.h>
int main()
{
int i = 0, a = 0, b = 2, c = 3, d = 4;
i = a++ && ++b && d++;
printf("a=%d\nb=%d\nc=%d\nd=%d\n", a, b, c, d);//1 2 3 4
return 0;
}
条件操作符
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int m = 0;
if (a > b)
m = a;
else
m = b;
printf("%d\n", m);
return 0;
}
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
if (a > 5)
b = 3;
else
b = -3;
printf("%d\n", b);
return 0;
}
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
b = (a > 5 ? 3 : -3);
printf("%d\n", b);
return 0;
}
//条件表达式判断大小
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a,&b);
int m = a > b ? a : b;
return 0;
}
逗号表达式
#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
int c = (a > b, a = b + 10, a, b = a + 1);//a = 12,b =13
printf("%d\n", c);
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int m = 0;
m = (a > b?a:b);
printf("%d\n", m);
return 0;
}
#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
int c = (a > b, a = b + 10, a, b = a + 1);
printf("c = %d\n", c);//13
return 0;
}
下标引用,函数调用和结构体
#include <stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5 };
printf("%d\n", arr[4]);//[]下标引用操作符,操作数是arr,4
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
int len = strlen("abcdef");//()是函数调用操作符。操作数:strlen,abcdef
printf("%d\n", len);
return 0;
}
#include <stdio.h>
struct book
{
char name[20];
char author[10];
int price;
};
void Print(struct book* s1)
{
printf("%s %s %d\n", (*s1).name, (*s1).author, (*s1).price);
printf("%s %s %d\n", s1->name, s1->author, s1->price);
}
void print(struct book* s2)
{
printf("%s %s %d\n", (*s2).name, (*s2).author, (*s2).price);
printf("%s %s %d\n", s2->name, s2->author, s2->price);
}
int main()
{
struct book s1 = { "《明解C语言(上)》","柴田望洋",50 };
struct book s2 = { "《明解C语言(下)》","柴田望洋",50 };
printf("%s %s %d\n", s1.name, s1.author, s1.price);
printf("%s %s %d\n", s2.name, s2.author, s2.price);
Print(&s1);
print(&s2);
return 0;
}
表达式求值
表达式求值的顺序一部分是由操作符的优先级和结合性决定的。
同意,有些表达式的操作数在求职的过程中可能需要转换为其他类型。
隐式类型转换
C的整型算数运输,至少是以缺省整型类型的精度来进行的。
为了提升这个精度,表达式中的字符和短整型操作数在使用之前被转换为普通整型,这种转换成为整型提升。
#include <stdio.h>
int main()
{
char a = 3;
char b = 127;
char c = a + b;
printf("%d\n", c);//-126
return 0;
}
#include <stdio.h>
int main()
{
char c = 1;
printf("%u\n", sizeof(c));//1
printf("%u\n", sizeof(+c));//4
printf("%u\n", sizeof(-c));//4
return 0;
}
#include <stdio.h>
int main()
{
int a, b, c;
a = 5;
c = ++a;
b = ++c, c++, ++a, a++;
b += a++ + c;
printf("a=%d b=%d c=%d\n", a, b, c);//9 23 8
return 0;
}