C程序設(shè)計(jì)英文課件:CHAPTE 2 TypesOperators and Expressions_第1頁(yè)
C程序設(shè)計(jì)英文課件:CHAPTE 2 TypesOperators and Expressions_第2頁(yè)
C程序設(shè)計(jì)英文課件:CHAPTE 2 TypesOperators and Expressions_第3頁(yè)
C程序設(shè)計(jì)英文課件:CHAPTE 2 TypesOperators and Expressions_第4頁(yè)
C程序設(shè)計(jì)英文課件:CHAPTE 2 TypesOperators and Expressions_第5頁(yè)
已閱讀5頁(yè),還剩82頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、CHAPTE 2Contents1 Variable Names2 Data Types and Sizes3 Constants (常量)4 Declarations & Expressions5 Arithmetic Operators (算數(shù)運(yùn)算符)6 Relational and Logical Operators (關(guān)系和邏輯運(yùn)算符)Contents7 Types Conversions(類型轉(zhuǎn)換)8 Increment and Decrement Operators9 Bitwise Operators (位運(yùn)算)10 Assignment Operators and Expres

2、sions (賦值運(yùn)算符與表達(dá)式)11 Conditional Expressions (條件表達(dá)式)12 Precedence and Order of Evaluation (優(yōu)先級(jí)和結(jié)合性)2.1 Variable Names常量和符號(hào)常量 常量:程序運(yùn)行中,其值不能被改變的量 符號(hào)常量:用標(biāo)識(shí)符表示的常量 定義符號(hào)常量 : #define 符號(hào)常量名 常量值 例:#define PAI 3.14159 /*定義符號(hào)常量代表圓周率 */注意使用符號(hào)常量的好處及編譯對(duì)符號(hào)常量的處理方法。變量變量:程序運(yùn)行中,其值可以改變的量稱2.1 Variable Names變量名和變量值:3a變量名變

3、量值存儲(chǔ)單元2.1 Variable Names2.1 Variable NamesNames are made up of letters, digits and underscore.The first character must be a letter or underscore.Upper case and lower case letters are distinct.The number limit of characters is not consist, depend on system. (always 8 characters)Keywords like if, else

4、, int, etc., cant be used.Its a good habit to choose the name related to the purpose of variable.Sum, day, M.D.richie, $45, int X,x,Y,y;student_name, student_number,k1,k2,k3 = m_eng, m_math,m_elec2.2 DataTypes and Sizes2.2 Data Types and sizes數(shù)據(jù)類型基本類型構(gòu)造類型指針類型空類型整型字符型實(shí)型(浮點(diǎn)型)單精度型雙精度型枚舉類型數(shù)組類型結(jié)構(gòu)體類型共同體類型

5、整型常量的表示方法 整型常量即整型常數(shù) : 十進(jìn)制整數(shù) 、 八進(jìn)制整數(shù)(以0開頭的數(shù))、十六進(jìn)制整數(shù)(以0 x開頭的數(shù))2.2 Data Types and sizesFor example:Decimal OctalHexadecimal 31 037 0 x1F 0X1FInt2.2 Data Types and sizes整型在內(nèi)存中的存放方式求負(fù)數(shù)的補(bǔ)碼方式是:將該數(shù)的絕對(duì)值的二進(jìn)制形式,按位取反再加1.00000000000010101111111111110101再加1,得-10的補(bǔ)碼1111111111110110例如求10的補(bǔ)碼.10的原碼取反正數(shù)負(fù)數(shù)0000000000001

6、010Int整型變量的分類2.2 Data Types and sizes基本型:類型說(shuō)明符為int,在內(nèi)存中占2個(gè)字節(jié)。短整型:類型說(shuō)明符為short int或short。所占字節(jié)和取值范圍均與基本型相同。長(zhǎng)整型:類型說(shuō)明符為long int或long,在內(nèi)存中占4個(gè)字節(jié)。無(wú)符號(hào)型:類型說(shuō)明符為unsigned。有符號(hào)整型變量:最大表示327670111111111111111 無(wú)符號(hào)整型變量:最大表示655351111111111111111Int整型變量的分類(續(xù))2.2 Data Types and sizes無(wú)符號(hào)型又可與上述三種類型匹配而構(gòu)成:無(wú)符號(hào)基本型:類型說(shuō)明符為unsign

7、ed int或unsigned。無(wú)符號(hào)短整型:類型說(shuō)明符為unsigned short。無(wú)符號(hào)長(zhǎng)整型:類型說(shuō)明符為unsigned long。Int整型變量的分類(續(xù))整數(shù)類型 位數(shù) 定義類型字 表示數(shù)的范圍 有符號(hào) 基本型 16int-32768+32767 短整型 16short int -32768+32767長(zhǎng)整型 32long int -231(231-1 )無(wú)符號(hào)基本型 16unsigned int065535 短整型 16unsigned short 065535 長(zhǎng)整型 32unsigned long 0(232-1) 2.2 Data Types and sizesInt整型

8、變量的定義 對(duì)變量的定義,一般是放在一個(gè)函數(shù)的開頭部分的聲明部分。 main( ) int a, b, c, d; /* 定義整型變量 */ unsigned u; a = 12; b = -24; u = 10; c = a + u; d = b + u; printf( “a + u = %d t b + u = %d n”, c, d );2.2 Data Types and sizes#include Int整型數(shù)據(jù)的溢出 如果一個(gè)整型變量存放的值,超出它所允許的范圍,將會(huì)產(chǎn)生溢出。 2.2 Data Types and sizes011111111111111110000000000

9、0000032767-32768main() int a,b; a=32767; b=a+1; printf(%d,%dn,a,b); #include Int整型常量的類型注意:1、一個(gè)整型常量,如果其值在-32768+32767范圍內(nèi),認(rèn)為它是int型,它可以賦給int型和long int型變量。2、一個(gè)整型常量,如果其值超過(guò)了上述范圍,而在 -2147483648+2147483647范圍內(nèi),則認(rèn)為它是long int 型。 3、一個(gè)整常數(shù)后面加一個(gè)字母u或U,認(rèn)為是unsigned int 型。4、一個(gè)整常數(shù)后面加一個(gè)字母l或L,認(rèn)為是long int型常數(shù)。2.2 Data Type

10、s and sizesFor example:123456789l or 123456789L1234u123456789ulInt2.2 Data Types and sizesCharacterchar c1,c2;c1=a;c2=b;2.2 Data Types and sizesCharacter Specification: A character within single quotesx, X, a, A Note: character constant is an integer, that is the numeric value of the character in th

11、e machines character set.For example: In the ASCII character set, the character constants 0 has the value 48, calculate expression as follow:x=0+10; So x has value 58. Character Constants2.2 Data Types and sizesCharacter2.2 Data Types and sizesmain() char a,b; a=120; /*x*/ b=121; /*x*/ printf(%c,%cn

12、,a,b);printf(%d,%dn,a,b); #include Character2.2 Data Types and sizesmain() char a,b; a=a; b=b; a=a-32; b=b-32; printf(%c,%cn%d,%dn,a,b,a,b); #include Character2.2 Data Types and sizesStatement:1、字符型數(shù)據(jù)和整形數(shù)據(jù)是通用的,但字符數(shù)據(jù)只能存放0255范圍內(nèi)的整數(shù)。 2、字符數(shù)據(jù)與整數(shù)可以直接進(jìn)行算術(shù)運(yùn)算。 3、字符數(shù)據(jù)與整型數(shù)據(jù)可以相互賦值Character2.2 Data Types and siz

13、es十進(jìn)制數(shù)形式:由數(shù)碼0 9和小數(shù)點(diǎn)組成(必須有小數(shù)點(diǎn))。0.0、25.0、5.789、0.13、5.0、300.、-267.8230指數(shù)形式:由十進(jìn)制數(shù),加階碼標(biāo)志“e”或“E”以及階碼(只能為整數(shù),可以帶符號(hào))組成 實(shí)型常量表示形式2.1E5 (等于2.1*105)3.7E-2 (等于3.7*10-2)0.5E7 (等于0.5*107)-2.8E-2 (等于-2.8*10-2)Float2.2 Data Types and sizes實(shí)型數(shù)據(jù)存放形式實(shí)型數(shù)據(jù)一般占4個(gè)字節(jié)(32位)內(nèi)存空間,按指數(shù)形式存儲(chǔ)。 +.3141591 數(shù)符 小數(shù)部分 指數(shù)Float2.2 Data Types

14、and sizes實(shí)型變量的分類:?jiǎn)尉龋╢loat型)雙精度(double型)長(zhǎng)雙精度(long double型)類型說(shuō)明符比特?cái)?shù)(字節(jié)數(shù))有效數(shù)字?jǐn)?shù)的范圍float32(4)6710-371038 double64(8)151610-30710308 long double 128(16)181910-4931104932 Float2.2 Data Types and sizes例 實(shí)型數(shù)據(jù)的舍入誤差。main()float a,b; a=123456.789e5; b=a+20printf(%fn,a);printf(%fn,b);#include Float2.3 Constants

15、2.3 ConstantsConstant Integer Constants Float Constants Character Constants Enumeration Constants Symbolic Constants #define NAME replacement text2.3 ConstantsInteger Constants Decimal, Octal and Hexadecimal specification octal : leading 0 ( 八 進(jìn) 制 ) hexadecimal : leading 0 x or 0X (十六進(jìn)制) For example

16、: Decimal OctalHexadecimal 31 037 0 x1f 0X1F2.3 ConstantsInteger Constants Suffix(后綴) No suffix int constants (1234) . L or l long constants(123456789l or 123456789L) U or u unsigned constants(1234u) UL or ul unsigned long constats(123456789ul)2.3 ConstantsFloat Constants Specification: decimal poin

17、t : 123.4 exponent :1.234e2=123456.789 2.3 ConstantsCharacter ConstantsSpecification: A character within single quotesx, X, a, A Note: character constant is an integer, that is the numeric value of the character in the machines character set.For example: In the ASCII character set, the character con

18、stants 0 has the value 48, calculate expression as follow:x=0+10; So x has value 58. 2.3 ConstantsString ConstantsDefinition: A sequence of zero or more characters surrounded by double quotes.For example: “this is a string” “” /*the empty string*/ “hello,” “world” /* equivalent to “hello, world” */2

19、.3 ConstantsString ConstantsNote: 1)A string constant is an array of characters, and has a 0(null character) at the end. 2)Be careful to distinguish between a character constant and a string constant that contains a single character. 注意字符常量和字符串常量的區(qū)別a and “a” are differentaa02.3 ConstantsString Const

20、ants/*strlen: return length of string s*/int strlen(char s )int i;i=0;while(si!=0) +i;return i;2.3 ConstantsEnumeration ConstantsDefinition: An enumeration is a list of constant integer values. 1)the first name in an enum has value 0, the next 1, and so on. enum boolean No, Yes ; /* No=0, Yes=1*/ 2)

21、Every name can have be specified. enum escapes BELL=a, BACKSPACE=b, TAB=t ; 3)If not all values are specified, unspecified values continue the prorssion from the last specified value.enum months JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP,OCT, NOV, DEC ; /*FEB=2, MAR=3, APR=4, etc.*/Example:enum b

22、oolean No, Yes ; /* No=0, Yes=1*/enum boolean yorn;yorn=Yes;if(yorn=No) Example:enum months JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP,OCT, NOV, DEC ; /*FEB=2, MAR=3, APR=4, etc.*/enum months month; int day;if(month=FEB) day=28;month=15; /*invalid value*/2.3 Constants2.3 Constants#include main()

23、enum s224m2241,m2242,m2243; enum s224 a; enum s224 b; enum s224 c; a=m2241; b=m2242; c=m2243; printf(a,m2241,%dnb,m2242,%dnc,m2243,%dn,a,b,c); 2.3 ConstantsConstants ExpressionDefinition: An expression that involves only constantsFor examples:#define MAXLINE 1000char lineMAXLINE;#define LEAP 1int x=

24、32+LEAP;2.4 Declarations2.4 Declarations All variables must be declared before useint lower, upper, step; A variable may also be initialized in its declarationfloat eps=1.0e-5; 在聲明時(shí)進(jìn)行初始化 const be applied to the variable to specify that its value will not be changed.const double e=2.71828182845905;2.

25、5 Arithmetic Operators C運(yùn)算符簡(jiǎn)介 C語(yǔ)言的運(yùn)算符有以下幾類 1.算術(shù)運(yùn)算符(+ - * / % + -)2.關(guān)系運(yùn)算符( = = = !=)3.邏輯運(yùn)算符(! & |)4.位運(yùn)算符 ( | &)5.賦值運(yùn)算符(=及其擴(kuò)展賦值運(yùn)算符)6.條件運(yùn)算符(? :)7.逗號(hào)運(yùn)算符(,)8.指針運(yùn)算符(*和&)9.求字節(jié)數(shù)運(yùn)算符(sizeof)10.強(qiáng)制類型轉(zhuǎn)換運(yùn)算符(類型)11.分量運(yùn)算符(.和-)12.下標(biāo)運(yùn)算符( )13.其它 (函數(shù)調(diào)用運(yùn)算符( )2.5 Arithmetic Operators各類數(shù)值型數(shù)據(jù)間的混合運(yùn)算數(shù)據(jù)類型轉(zhuǎn)換整型、實(shí)型、字符型數(shù)據(jù)間可以混合運(yùn)算。在

26、運(yùn)算時(shí),不同類型的數(shù)據(jù)要先轉(zhuǎn)換成同一類型,然后進(jìn)行運(yùn)算。轉(zhuǎn)換的規(guī)則如下所示: 高 double float long unsigned 低 int char,short2.5 Arithmetic OperatorsFor Example:3+6*8.7-12; 123+23.4*b-2+a2.5 Arithmetic OperatorsArithmetic OperatorsBinary arithmetic operators: + - * / Modulus operator: % (求余) x%y; /*produce the remainder when x is devided b

27、y y.*/ Precedence (優(yōu)先級(jí)) +, - have the same precedence, lower than * , / and % Associativity(結(jié)合性) Arithmetic operators associate left to right. 2.5 Arithmetic OperatorsArithmetic Operators x=5/3 /* xs value is 1 */ x=-5/3 /* xs value is -1 -2 */ x=5%3 /* xs value is 2 */ x=a*b/c%d-1.5+a 2.6 Relationa

28、l and Logical Operators 2.6 Relational and Logical OperatorsRelational Operators: = b x3/2The value of expression is True (1) or False(0).For example:a=3,b=2,c=1;d=ab; /* d have value true (1).*/f=abc;Relational Expressions: 2.6 Relational and Logical OperatorsNote: Logical operators associate left

29、to right.precedencehigherlowerLogical Operators: ! not & and | or2.6 Relational and Logical Operators ab & cd 等價(jià)于 (ab)&(cd) !b=c|da 等價(jià)于 (!b)=c)|(dc&x+yc)&(x+y)b)2.6 Relational and Logical OperatorsLogical ExpressionsDefinition: Some relational expressions and logical objects connected by logical ope

30、rator. The value of expression is True (1) or False(0)./* a=4, b=5*/!a /* value 0, because a not false, so !a false.*/a|b/* true */4&0|2/* true*/Note: The logical expression are evaluated left to right, and evaluation stop as soon as the truth or falsehood of the result is known.a & b & c Question:

31、Try to evaluate the expression: a | b | c2.6 Relational and Logical OperatorsLogical Expressions For example:for(i=0; ilim-1 & (c=getchar( ) != n & c !=EOF;+i)si = c;ilim-1 & c !=EOF & (c=getchar( ) != n 2.7 Type Conversions 2.7 Type ConversionsRule1: “narrow” to “wide”.For example:float f;char c;f+

32、c;/* value is double type*/2.7 Type ConversionsRule2: Conversions take place across assignments; the value of the right side is converted to the type of the left.For exampleint i; float x;i=x; /*result is a integer type*/x=i; /*result is a float type*/#include main()int i=3; float x=3.5; printf(%d,%

33、fn,i,x); i=x; printf(%f,%fn,i,x); x=i; printf(%f,%fn,i,x); 2.7 Type ConversionsExplicit type conversions can be forced(coerced) in any expression (type-name) expressionExample:char c;d=(double)c;Rule1: (type-name) expression (int)(x+y) ; (int)x+y;2.7 Type ConversionsRule2: 強(qiáng)制類型轉(zhuǎn)換得到一個(gè)所需類型的中間變量,原來(lái)便另的類

34、型未發(fā)生變化。Example:#includemain()float x;int i;x=3.6;i=(int)x;Printf(“x=%f,i=%d”,x,i);2.7 Type ConversionsRule3:if arguments are declared by function protype,the type conversions are forced when the fuction is called.如果變?cè)峭ㄟ^(guò)函數(shù)原型說(shuō)明的,那么在通常情況下,當(dāng)該函數(shù)被調(diào)用時(shí),系統(tǒng)對(duì)變?cè)M(jìn)行強(qiáng)制轉(zhuǎn)換 Example:root2=sqrt(2); /* root2 is double

35、type, and 2 is convert to double type while calling sqrt function*/Prototype of function sqrt:double sqrt(double);Rule4: 無(wú)論是強(qiáng)制轉(zhuǎn)換或是自動(dòng)轉(zhuǎn)換,都只是為了本次運(yùn)算的需要而對(duì)變量的數(shù)據(jù)長(zhǎng)度進(jìn)行的臨時(shí)性轉(zhuǎn)換,而不改變數(shù)據(jù)說(shuō)明時(shí)對(duì)該變量定義的類型 .2.7 Type ConversionsString to numberint atoi (char s ) /* convert string to integer*/int i,n;n=0;for(i=0;si=0 & si=

36、9;i+)n=10*n+(si-0);return n;1234=10*(10*(10*1)+2)+3)+42.7 Type Conversions /* convert upper letter to lower*/#include main()int a=67;int lower(int c);a=lower(a);printf(%c,a);int lower(int c) if (c=A& c=0 & si=9;i+)n=10*n+(si-0);return n;1234=10*(10*(10*1)+2)+3)+42.8 Increment and Decrement Operators

37、 2.8 Increment and Decrement Operators+ Increment operator_ _ Decrement operator+nincrement before n usedn+increment after n used2.8 Increment and Decrement OperatorsNormally use for variables(i+j)+ illegal expression 6+illegal expression2.8 Increment and Decrement OperatorsAssociativity:from right

38、to left-i+ -(i+)i+j (i+)+j#include main()int i=4;printf(%d,%dn,i,-i-);printf(%d,%dn,i,-(-i);printf(%d,%dn,i,i+); printf(%d,%dn,i,+i); 2.8 Increment and Decrement Operators k1=+nc; k2=nc+; k3=(nc+)+(nc+)+(nc+); k4=(+nc)+(+nc)+(+nc);Note: for each expression, nc=3 k1=4 nc=4 k2=3 nc=4 k3=9 nc=6 k4=18 n

39、c=6先取出nc的值,再運(yùn)算先運(yùn)算,再使用nc的值,2.8 Increment and Decrement Operatorsmain()int k1,k2,k3,k4,nc=3;k1=+nc; k2=nc+; k3=(nc+)+(nc+)+(nc+);k4=(+nc)+(+nc)+(+nc);printf(“k1,nc=%d,%d”,k1,nc); printf(“k2,nc=%d,%d”,k2,nc); printf(“k3,nc=%d,%d”,k3,nc);printf(“k4,nc=%d,%d”,k4,nc); k1,nc=4,11k2,nc=4,11k3,nc=15,11k4,nc=

40、33,112.8 Increment and Decrement Operators main()int k1,k2,k3,k4,bak,nc=3;bak=nc;/* ncs value store in bak*/k1=+nc;nc=bak; /* restore ncs value*/ k2=nc+;nc=bak; /* restore ncs value*/ k3=(nc+)+(nc+)+(nc+);nc=bak; /* restore ncs value*/k4=(+nc)+(+nc)+(+nc); k1=4 nc=4 k2=3 nc=4 k3=9 nc=6 k4=18 nc=62.8 Increment and Decrement OperatorsVoid strcat(char s ,char t )int i,j;i=j=0;while(si!=0)i+; /* find end of

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論