首页 C语言Chapter3东北大学李丹程

C语言Chapter3东北大学李丹程

举报
开通vip

C语言Chapter3东北大学李丹程nullIntroduction to Computer (C Programming)Introduction to Computer (C Programming)Software College , Northeastern University 2010,9 Chapter3 Operators and Expressions 3.1 introduction 3.1 introduction C operators can be classified into a number of categorie...

C语言Chapter3东北大学李丹程
nullIntroduction to Computer (C Programming)Introduction to Computer (C Programming)Software College , Northeastern University 2010,9 Chapter3 Operators and Expressions 3.1 introduction 3.1 introduction C operators can be classified into a number of categories. They include: Arithmetic operators. Relational operators. Logical operators. Assignment operators. Increment and decrement operators. Conditional operators. Bitwise operators. Special operators.3.2 Arithmetic operators 3.2 Arithmetic operators Operator Meaning + Addition or unary plus - Subtraction or unary minus * Multiplication / Division % Modulo division Examples: a-b, a+b, a*b, a/b, a%bExample 3.1Example 3.1The Program shows the use of integer arithmetic to convert a given number of days into months and days.main() { int months, days; printf(“Enter days\n”); scanf(“%d”, &days); months = days/30; days = days % 30; printf(“Months=%d days=%d”, months, days); } Output Enter days 265 Months=8 Days=25 Enter days 364 Months=12 Days=4 Enter days 45 Months=1 Days=153.3 Relational Operators3.3 Relational OperatorsC supports six relational operators in all: Operators Meaning < is less than <= is less than or equal to > is greater than >= is greater than or equal to == is equal to != is not equal toExample 4.5<=10 4.5<-10 -35>=0 10<7+5 a+b==c+d ?Relational Operator ComplementsRelational Operator Complements Among the six relational operators, each one is a complement of another operator. > is complement of <= < is complement of >= == is complement of != We can simplify an expression involving the not and the less than operators using the complements as shown below: Actual one Simplified one !(x=y !(x>y) x<=y !(x!=y) x==y !(x<=y) x>y !(x>=y) xb && x==10 Truth TableTruth Table Op_1 Op_2 Value of the Expression op_1&&op_2 op_1||op_2 Non_zero Non_zero 1 1 Non_zero 0 0 1 0 Non_zero 0 1 0 0 0 0Examples: 1. if (age > 55 && salary < 1000) 2. if (number < 0 || number > 1000) 3.5 Assignment Operators3.5 Assignment OperatorsC has a set of ‘shorthand’ operators of the form: v op =exp; where v is a variable, exp is an expression op is a C binary arithmetic operator op= is known as the shorthand assignment operator v op =exp; is equivalent to v =v op (exp);Examples: x+=y+1; is equivalent to x=x+(y+1); x+=3; is equivalent to x=x+3;Shorthand Assignment OperatorsShorthand Assignment Operators Statement with simple Statement with assignment operator shorthand operator a = a + 1 a += 1 a = a – 1 a -= 1 a = a * (n+1) a *= n+1 a = a / (n+1) a /= n+1 a = a % b a %= bThe use of shorthand assignment operators has three advantages:The use of shorthand assignment operators has three advantages:What appears on the left-hand side need not be repeated and therefore it becomes easier to write. The statement is more concise and easier to read The statement is more efficientExample: value = value+ delta; With the help of the += operator, this can be written as follows: value += delta;Example 3.2Example 3.2The Program prints a sequence of squares of numbers. Note the use of the shorthand operator *=.#define N 100 #define A 2 main() { int a; a = A; while(a b) ? a : b; if (a > b) x = a; else x = b;3.8 Bitwise Operator3.8 Bitwise OperatorC has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level. These operators are used for testing the bits, or shifting them right or left. Bitwise operators may not applied to float or double. Operator Meaning & bitwise AND | bitwise OR ^ bitwise exclusive OR << shift left >> shift right3.9 Special Operators3.9 Special OperatorsPointer Operators: & and * Member Selection Operators: . and ->nullThe Comma Operator: can be used to link the related expression together. A comma-linked list of expressions are evaluated left to right and the value of right-most expression is the value of the combined expression. Example: value = (x = 10, y = 5, x + y); value = 15; Some applications of comma operator are: In for loops: for ( n = 1, m = 10; n <= m; n++, m++ ) In while loops: while( c = getchar( ), c != ‘\n’ ) Exchanging values: t = x, x = y, y = t; nullThe sizeof Operator: is a compile time operator and, when used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier. Example: m = sizeof (sum); n = sizeof (long int); k = sizeof(235L);Note: The sizeof operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variables during of a program Example 3.3Example 3.3The program employs different kinds of operators. The results of their evaluation are also shown for comparison.main() { int a, b, c, d; a = 15; b = 10; c = ++a – b; printf(“a=%d b=%d c=%d\n”,a,b,c); d = b++ +a; printf(“a=%d b=%d d=%d\n”,a,b,d); printf(“a/b = %d\n”, a/b); printf(“a%%b = %d\n”, a%b); printf(“a*=b = %d\n”, a*=b); printf(“%d\n”, (c>d)?1:0); printf(“%d\n”, (c main ( ) { int a,b,k; k=(a=2,b=5,a>b?a++:b++,a+b); printf("k = %d\n", k); }k = 8运行结果:课堂练习课堂练习给出下面程序的输出结果:[短路现象]#include main ( ) { int a=1,b=2,c=3,d=4,m=1,n=1; (m=a>b)&&(n=c>d); printf("m = %d\n", m); printf("n = %d\n", n); }m = 0 n = 1运行结果:
本文档为【C语言Chapter3东北大学李丹程】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_740234
暂无简介~
格式:ppt
大小:409KB
软件:PowerPoint
页数:0
分类:互联网
上传时间:2010-12-07
浏览量:8