运算符 | 含义 |
---|---|
+ | 数据相加 |
- | 数据相减 |
* | 数据相乘 |
/ | 数据相除 |
% | 数据求余,要求两边的运算符必须为整数。 |
练习
int a; int x,y,z; 要求用户从键盘输入一个三位数的整数赋值给a,然后要求把该三位数的 百数,十位,个位求出来。赋值给x,y,z并输出。 例如: a = 456; x = 4; y = 5; z = 6;
代码:
#include <stdio.h> #include <termios.h> #include <unistd.h> int main() { int x,y,z,ret,a; again: printf("请输入三位数的整数:\n"); ret = scanf("%d",&a); if (ret != 1) { while (getchar() != '\n'); goto again; } if (a > 1000 || a < 99) { printf("输入不正确,请重新输入。\n"); goto again; } x = a / 100; y = a % 100 / 10; z = a % 10 ; printf("x = %d \nz = %d\ny = %d\n",x,y,z); return 0; }
#include <stdio.h> int main() { int a = 10,b = 0; b = a ++;//b = a;a = a + 1; printf("a = %d b = %d\n",a,b); //11,10 b = ++a; //a = a + 1 b = a; printf("a = %d b = %d\n",a,b);//12,12 return 0; }
练习
求出下列数据的答案。(上一个表达式x,y的值会影响下面x和y的值)
#include <stdio.h> int main() { int x = 10 ,y =20; int z = 0; z = (++x) + (y++); printf("z = %d\n",z); z = (--x) + (y++); printf("z = %d\n",z); z= (++x) + (--y); printf("z = %d\n",z); printf("x = %d y = %d\n",x,y); return 0; }
分析结果:
= 10 + 1 + 20 (+ 1) = 31 x = 11 y = 21 z = 31 = 11 - 1 + 21 (+ 1) = 31 x = 10 y = 22 z = 31 = 10 + 1 + 22 - 1 = 32 x = 11 y = 21 z = 32
运算符 | 含义 |
---|---|
> | 大于条件成立的时候,运算的结果为真 |
= | 大于或等于条件成立的时候,运算的结果为真 |
< | 小于条件成立的时候,运算的结果为真 |
<= | 小于或等于条件成立的时候,运算的结果为真 |
== | 等于条件成立的时候,运算的结果为真 |
!= | 不等于条件成立的时候,运算的结果为真 |
#include <stdio.h> int main() { int data = 0; printf("please input a data : "); scanf("%d",&data); printf("data > 30 = %d\n",data > 30); //1 printf("data == 30 = %d\n",data == 30); //0 printf("============================\n"); printf("data = 800 : %d\n",data = 800); //800 printf("data != 800 : %d\n",data != 800);//0 printf("============================\n"); //数学:10 < data < 20 ===> 10 < data 结果为0或1 // 0或1 < 20 结果 1 printf("10 < data < 20 : %d\n",10 < data < 20); return 0; }
练习
从键盘输入2个字符数据,要求数据>,<,!=三个的结果。
#include <stdio.h> int main() { int a,b; printf("请输入两个2个字符数据:\n"); scanf("%d",&a); scanf("%d",&b); printf("a => %d , b => %d\n",a,b); printf("\n"); printf("a > b => %d\n",a > b); printf("a < b => %d\n",a < b); printf("a != b => %d\n",a != b); return 0; }
#include <stdio.h> int main() { int a = 0,b = 0; int ret = 0; printf("please input two data : \n"); scanf("%d%d",&a,&b); // &&的规则:都真则真,一假则假 // ret = a > b && a != 30; // printf("ret = %d\n",ret); // ||的规则: 一真则真,都假则假 ret = a > b || a != 30; printf("ret = %d\n",ret); return 0; }
#include <stdio.h> int main() { int a = 7,b = 9,c = 1; int ret = 0; //&&符号的短路特性: 表达式1为假,那么整个表达式的值就已经为假了,表达式2不会执行 ret = a > b && c ++; printf("ret = %d c = %d\n\n",ret,c); //0,1 printf("+++++++++++++++++++++++++\n"); ret = a < b && c ++; printf("ret = %d c = %d\n\n",ret,c); //1,2 printf("========================================\n"); c = 1; //||符号的短路特性: 表达式1为真,那么整个表达式的值就已经为真了,表达式2不会执行 ret = a > b || c ++; printf("ret = %d c = %d\n\n",ret,c); //1,2 printf("+++++++++++++++++++++++++\n"); ret = a < b || c ++; printf("ret = %d c = %d\n\n",ret,c); //1,2 return 0; }
练习
要求大家用笔算出下列printf输出的结果
int value = 0; int a = 0; int b = 1; value = a && b; printf("%d\n",value); a=0 b=1 value=0 value = a || b; printf("%d\n",value); a=0 b=1 value=1 value = a++ && b ++; printf("%d\n",value); a=1 b=1 value=0 value = --a || b--; printf("%d\n",value); a=0 b=0 value=1 value = (a + b) || (a - b); printf("%d\n",value); a=0 b=0 value=0 value = (a * b) && (a + b); printf("%d\n",value); a=0 b=0 value=0 value = !a; printf("%d\n",value); value=1 printf("%d,%d,%d\n",a,b,value); a=0 b=0 value=1
通过运算符的知识学习,熟练掌握一些算法的应用。