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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

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

3、量值存儲單元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數據類型基本類型構造類型指針類型空類型整型字符型實型(浮點型)單精度型雙精度型枚舉類型數組類型結構體類型共同體類型

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

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

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

8、變量的定義 對變量的定義,一般是放在一個函數的開頭部分的聲明部分。 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整型數據的溢出 如果一個整型變量存放的值,超出它所允許的范圍,將會產生溢出。 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、一個整型常量,如果其值在-32768+32767范圍內,認為它是int型,它可以賦給int型和long int型變量。2、一個整型常量,如果其值超過了上述范圍,而在 -2147483648+2147483647范圍內,則認為它是long int 型。 3、一個整常數后面加一個字母u或U,認為是unsigned int 型。4、一個整常數后面加一個字母l或L,認為是long int型常數。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、字符型數據和整形數據是通用的,但字符數據只能存放0255范圍內的整數。 2、字符數據與整數可以直接進行算術運算。 3、字符數據與整型數據可以相互賦值Character2.2 Data Types and siz

13、es十進制數形式:由數碼0 9和小數點組成(必須有小數點)。0.0、25.0、5.789、0.13、5.0、300.、-267.8230指數形式:由十進制數,加階碼標志“e”或“E”以及階碼(只能為整數,可以帶符號)組成 實型常量表示形式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實型數據存放形式實型數據一般占4個字節(jié)(32位)內存空間,按指數形式存儲。 +.3141591 數符 小數部分 指數Float2.2 Data Types

14、and sizes實型變量的分類:單精度(float型)雙精度(double型)長雙精度(long double型)類型說明符比特數(字節(jié)數)有效數字數的范圍float32(4)6710-371038 double64(8)151610-30710308 long double 128(16)181910-4931104932 Float2.2 Data Types and sizes例 實型數據的舍入誤差。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 ( 八 進 制 ) hexadecimal : leading 0 x or 0X (十六進制) 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; 在聲明時進行初始化 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運算符簡介 C語言的運算符有以下幾類 1.算術運算符(+ - * / % + -)2.關系運算符( = = = !=)3.邏輯運算符(! & |)4.位運算符 ( | &)5.賦值運算符(=及其擴展賦值運算符)6.條件運算符(? :)7.逗號運算符(,)8.指針運算符(*和&)9.求字節(jié)數運算符(sizeof)10.強制類型轉換運算符(類型)11.分量運算符(.和-)12.下標運算符( )13.其它 (函數調用運算符( )2.5 Arithmetic Operators各類數值型數據間的混合運算數據類型轉換整型、實型、字符型數據間可以混合運算。在

26、運算時,不同類型的數據要先轉換成同一類型,然后進行運算。轉換的規(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)先級) +, - have the same precedence, lower than * , / and % Associativity(結合性) 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 等價于 (ab)&(cd) !b=c|da 等價于 (!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: 強制類型轉換得到一個所需類型的中間變量,原來便另的類

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.如果變元是通過函數原型說明的,那么在通常情況下,當該函數被調用時,系統(tǒng)對變元進行強制轉換 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: 無論是強制轉換或是自動轉換,都只是為了本次運算的需要而對變量的數據長度進行的臨時性轉換,而不改變數據說明時對該變量定義的類型 .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的值,再運算先運算,再使用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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論