5.4位逻辑或运算
#include <stdio.h>
int main()
{
unsigned char x = 076,y = 0x89,z;
z = x | y;
printf("z = %#x",z);
return 0;
}
位逻辑异或运算
相同为假,不同为真
#include <stdio.h>
int main()
{
unsigned char x = 75,y = 0173,z;
z = x ^ y;
printf("z = %#x",z);
return 0;
}
位逻辑左移运算
<< //左移三位,十六进制右边加3个0
#include <stdio.h>
int main()
{
unsigned char x = 0xe4,z;
z = x<<3;
printf("z = %#x",z);
return 0;
}
5.5赋值运算符
#include <stdio.h>
int main()
{
int count,sum;
count = 0, sum = 0;
while(count++< 100) //sum += count == sum = sum + count
{
sum += count;
}
printf("sum = %d\n",sum);
return 0;
}
5.6特殊运算符
5.6.1 条件运算符
? :是三目运算符
一般形式<表达式1>?<表达式2>:<表达式3>
//表达式对执行表达式2,否则执行表达式3
#include <stdio.h>
int main()
{
int x,y;
x = 70;
y = x++ > 70 ?100:0;//x先跟70比较再自增
printf("x = %d,y = %d\n",x,y);
return 0;
}
5.6.2逗号运算符
由最后一个表达式组成
#include <stdio.h>
int main()
{
float x = 10.5,y = 1.8,z = 0;
z = (x+= 5 ,y = x + 0.2,z = x+y);
printf("x = %f,y = %f,z = %f\n",x,y,z);
return 0;
}
5.6.3 sizeof运算符
他只针对数据类型,而不针对变量
例如:sizeof(double) sizeof(long)
#include <stdio.h>
int main()
{
int a;
long long b;
printf("a = %d,b = %d\n",sizeof(a),sizeof(b));
return 0;
}
5.6.4 c运算符的优先级
括号优先级最高
#include <stdio.h>
int main()
{
int x = 1,y = 0,z = 0;
if(x += y == z,y = x+2, z = x + y + z > 0)
printf("x = %d, y = %d",x,y);
return 0;
}
六.输入输出
6.1数据输出
字符输出函数
格式: putchar©
功能:把字符c输出到显示器中
返回值:正常,为显示的代码值
#include <stdio.h>
int main()
{
int a;
char c;
a = 65;
c = 'B';
putchar(a);
putchar('\n');
putchar(c);
return 0;
}