当前位置: 首页 > news >正文

C语言运算符与流程控制详解

运算符概述

运算符是告诉编译器执行特定数学或逻辑功能的符号。C语言包含丰富的内置运算符,主要分为以下几类:

算术运算符

假设变量A的值为10,变量B的值为20:

操作符描述例子
+加两个操作数A + B = 30
-从第一个减去第二个操作数A − B = -10
*将两个操作数相乘A * B = 200
/将分子除以除分母B / A = 2
%模运算符和整数除法后的余数B % A = 0
++增量运算符将整数值增加一A++ = 11
--减量运算符将整数值减一A-- = 9

c

#include <stdio.h>int main() {int a = 21;int b = 10;int c;c = a + b;printf("Line 1 - Value of c is %d\n", c);c = a - b;printf("Line 2 - Value of c is %d\n", c);c = a * b;printf("Line 3 - Value of c is %d\n", c);c = a / b;printf("Line 4 - Value of c is %d\n", c);c = a % b;printf("Line 5 - Value of c is %d\n", c);c = a++; printf("Line 6 - Value of c is %d\n", c);c = a--; printf("Line 7 - Value of c is %d\n", c);return 0;
}

关系运算符

假设变量A的值为10,变量B的值为20:

操作符描述例子
==检查两个操作数的值是否相等(A == B) 为 false
!=检查两个操作数的值是否相等(A != B) 为 true
>检查左操作数的值是否大于右操作数(A > B) 为 false
<检查左操作数的值是否小于右操作数(A < B) 为 true
>=检查左操作数的值是否大于或等于右操作数(A >= B) 为 false
<=检查左操作数的值是否小于或等于右操作数(A <= B) 为 true

c

#include <stdio.h>int main() {int a = 21;int b = 10;int c;if(a == b) {printf("Line 1 - a is equal to b\n");} else {printf("Line 1 - a is not equal to b\n");}if(a < b) {printf("Line 2 - a is less than b\n");} else {printf("Line 2 - a is not less than b\n");}if(a > b) {printf("Line 3 - a is greater than b\n");} else {printf("Line 3 - a is not greater than b\n");}/* 改变a和b的值 */a = 5;b = 20;if(a <= b) {printf("Line 4 - a is either less than or equal to b\n");}if(b >= a) {printf("Line 5 - b is either greater than or equal to a\n");}return 0;
}

逻辑运算符

假设变量A持有1,变量B持有0:

操作符描述例子
&&逻辑与运算符。如果两个操作数都不为零,则条件为true(A && B) 为 false
||逻辑或运算符。如果两个操作数中的任何一个不为零,则条件为true(A || B) 为 true
!逻辑非运算符。用于反转其操作数的逻辑状态!(A && B) 为 true

c

#include <stdio.h>int main() {int a = 5;int b = 20;int c;if(a && b) {printf("Line 1 - Condition is true\n");}if(a || b) {printf("Line 2 - Condition is true\n");}/* 改变a和b的值 */a = 0;b = 10;if(a && b) {printf("Line 3 - Condition is true\n");} else {printf("Line 3 - Condition is not true\n");}if(!(a && b)) {printf("Line 4 - Condition is true\n");}return 0;
}

按位运算符

按位运算符对位进行运算并执行逐位操作。假设A = 60,B = 13:

text

A = 0011 1100
B = 0000 1101
-----------------
A & B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
~A = 1100 0011
操作符描述例子
&二进制AND运算符(A & B) = 12,即0000 1100
|二进制OR运算符(A | B) = 61,即0011 1101
^二进制XOR运算符(A ^ B) = 49,即0011 0001
~二进制补码运算符(~A) = -61,即1100 0011
<<二进制左移运算符A << 2 = 240,即1111 0000
>>二进制右移运算符A >> 2 = 15,即0000 1111

c

#include <stdio.h>int main() {unsigned int a = 60; /* 60 = 0011 1100 */  unsigned int b = 13; /* 13 = 0000 1101 */int c = 0;           c = a & b;       /* 12 = 0000 1100 */ printf("Line 1 - Value of c is %d\n", c);c = a | b;       /* 61 = 0011 1101 */printf("Line 2 - Value of c is %d\n", c);c = a ^ b;       /* 49 = 0011 0001 */printf("Line 3 - Value of c is %d\n", c);c = ~a;          /* -61 = 1100 0011 */printf("Line 4 - Value of c is %d\n", c);c = a << 2;     /* 240 = 1111 0000 */printf("Line 5 - Value of c is %d\n", c);c = a >> 2;     /* 15 = 0000 1111 */printf("Line 6 - Value of c is %d\n", c);return 0;
}

赋值运算符

操作符描述例子
=简单的赋值运算符C = A + B
+=加和赋值运算符C += A 等价于 C = C + A
-=减和赋值运算符C -= A 等价于 C = C - A
*=乘和赋值运算符C *= A 等价于 C = C * A
/=除和赋值运算符C /= A 等价于 C = C / A
%=模和赋值运算符C %= A 等价于 C = C % A
<<=左移和赋值运算符C <<= 2 等价于 C = C << 2
>>=右移和赋值运算符C >>= 2 等价于 C = C >> 2
&=按位与和赋值运算符C &= 2 等价于 C = C & 2
^=按位异或和赋值运算符C ^= 2 等价于 C = C ^ 2
|=按位或和赋值运算符C |= 2 等价于 C = C | 2

c

#include <stdio.h>int main() {int a = 21;int c;c = a;printf("Line 1 - = Operator Example, Value of c = %d\n", c);c += a;printf("Line 2 - += Operator Example, Value of c = %d\n", c);c -= a;printf("Line 3 - -= Operator Example, Value of c = %d\n", c);c *= a;printf("Line 4 - *= Operator Example, Value of c = %d\n", c);c /= a;printf("Line 5 - /= Operator Example, Value of c = %d\n", c);c = 200;c %= a;printf("Line 6 - %%= Operator Example, Value of c = %d\n", c);c <<= 2;printf("Line 7 - <<= Operator Example, Value of c = %d\n", c);c >>= 2;printf("Line 8 - >>= Operator Example, Value of c = %d\n", c);c &= 2;printf("Line 9 - &= Operator Example, Value of c = %d\n", c);c ^= 2;printf("Line 10 - ^= Operator Example, Value of c = %d\n", c);c |= 2;printf("Line 11 - |= Operator Example, Value of c = %d\n", c);return 0;
}

杂项运算符

操作符描述例子
sizeof()返回变量的大小sizeof(a)
&返回变量的地址&a
*指向变量的指针*a
?:条件表达式条件 ? 值1 : 值2

c

#include <stdio.h>int main() {int a = 4;short b;double c;int* ptr;/* sizeof运算符示例 */printf("Line 1 - Size of variable a = %lu\n", sizeof(a));printf("Line 2 - Size of variable b = %lu\n", sizeof(b));printf("Line 3 - Size of variable c = %lu\n", sizeof(c));/* & 和 * 运算符示例 */ptr = &a;        /* 'ptr' 现在包含 'a' 的地址 */printf("value of a is %d\n", a);printf("*ptr is %d.\n", *ptr);/* 三元运算符示例 */a = 10;b = (a == 1) ? 20 : 30;printf("Value of b is %d\n", b);b = (a == 10) ? 20 : 30;printf("Value of b is %d\n", b);return 0;
}

运算符优先级

运算符优先级确定表达式中术语的分组,并确定如何评估表达式。

类别操作符关联性
后缀() [] -> . ++ --左到右
一元+ - ! ~ ++ -- (type) * & sizeof右到左
乘性* / %左到右
加减+ -左到右
位移<< >>左到右
关系型< <= > >=左到右
相等== !=左到右
按位与&左到右
按位异或^左到右
按位或|左到右
逻辑与&&左到右
逻辑或||左到右
条件?:右到左
赋值= += -= *= /= %= >>= <<= &= ^= |=右到左
逗号,左到右

c

#include <stdio.h>int main() {int a = 20;int b = 10;int c = 15;int d = 5;int e;e = (a + b) * c / d;      // (30 * 15) / 5printf("Value of (a + b) * c / d is : %d\n", e);e = ((a + b) * c) / d;    // (30 * 15) / 5printf("Value of ((a + b) * c) / d is : %d\n", e);e = (a + b) * (c / d);   // 30 * (15/5)printf("Value of (a + b) * (c / d) is : %d\n", e);e = a + (b * c) / d;     // 20 + (150/5)printf("Value of a + (b * c) / d is : %d\n", e);return 0;
}

流程控制 - 决策结构

C编程语言将任何非零和非null值假定为true,如果为零或null,则假定为false值。

if 语句

c

#include <stdio.h>int main() {int a = 10;if(a < 20) {printf("a is less than 20\n");}printf("value of a is : %d\n", a);return 0;
}

if...else 语句

c

#include <stdio.h>int main() {int a = 100;if(a < 20) {printf("a is less than 20\n");} else {printf("a is not less than 20\n");}printf("value of a is : %d\n", a);return 0;
}

嵌套 if 语句

c

#include <stdio.h>int main() {int a = 100;int b = 200;if(a == 100) {if(b == 200) {printf("Value of a is 100 and b is 200\n");}}printf("Exact value of a is : %d\n", a);printf("Exact value of b is : %d\n", b);return 0;
}

switch 语句

c

#include <stdio.h>int main() {char grade = 'B';switch(grade) {case 'A' :printf("Excellent!\n");break;case 'B' :case 'C' :printf("Well done\n");break;case 'D' :printf("You passed\n");break;case 'F' :printf("Better try again\n");break;default :printf("Invalid grade\n");}printf("Your grade is %c\n", grade);return 0;
}

嵌套 switch 语句

c

#include <stdio.h>int main() {int a = 100;int b = 200;switch(a) {case 100: printf("This is part of outer switch\n");switch(b) {case 200:printf("This is part of inner switch\n");}}printf("Exact value of a is : %d\n", a);printf("Exact value of b is : %d\n", b);return 0;
}

条件运算符 ?:

条件运算符 ?: 可以替代 if...else 语句,具有以下形式:

c

Exp1 ? Exp2 : Exp3;

其中:

  • 如果 Exp1 为 true,则计算 Exp2 并成为整个表达式的值

  • 如果 Exp1 为 false,则计算 Exp3 并成为表达式的值

C语言提供了丰富的运算符和灵活的控制结构,使得程序员能够编写出高效且功能强大的程序。理解运算符的优先级和结合性对于编写正确的表达式至关重要,而掌握各种决策结构则能帮助构建复杂的程序逻辑。

http://www.dtcms.com/a/425201.html

相关文章:

  • 建筑行业一般在哪个网站招聘儿童网站模板免费下载
  • 品牌宝正式推出免费个人网站认证网站免费推广方案
  • 网站克隆 有后台登录it培训机构有哪些
  • 做网站服务器多大的好怎么做下载类网站
  • 《道德经》第七章
  • 网站上面怎么做链接容桂网站制作值得信赖
  • 阿里云服务器网站目录网站seo在线诊断分析
  • 网站版面做得好的做网站3年
  • 做食品生产的网站wordpress首页轮换图片在哪里设置
  • 专业制作网站用哪些软件广州网络营销系统
  • 枣阳网站建设公司安装wordpress xampp
  • 网站备案安全责任书是谁盖章网站策划的前景
  • 大连网站建设优化怎样套用wordpress模板
  • 免费做ppt的网站有哪些云南建设学院的网站
  • 网站列表怎么做wordpress前台登录框
  • 建站多少钱一个厦门模板建站平台
  • 天津南开做网站深圳网站建设公司有哪些
  • 怀柔成都网站建设盐城外贸网站建设
  • 怎么用腾讯云主机建设网站知名网站建设多少钱
  • Cmake使用CPack实现打包
  • 免费电视剧网站大全在线观看网站设置保存登录密码怎么取消
  • (性能测试)磁盘关注的性能指标 网络瓶颈 带宽和宽带 数据库的性能瓶颈分析 数据库连接池 数据库死锁 JVM内存瓶颈分析 总结
  • 购物网站建设案例delphi7网站开发
  • 做网站的前景如何福州网站建设发布
  • 中国纪检监察报官网wordpress 移动 seo
  • 乐清做网站建设商洛市住房和城乡建设局网站
  • 网站建设合同任wordpress+改邮箱
  • 做网站贴吧网站如何添加浮动窗口
  • 做转发赚钱的网站哪个网站可以做会计分录
  • 机构网站建设需要交费吗wordpress cat_name