已閱讀5頁(yè),還剩78頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1,C的基本數(shù)據(jù)類型及運(yùn)算,信息對(duì)抗,電子工程學(xué)院,2,標(biāo)識(shí)符,標(biāo)識(shí)符就是給程序中的變量、常量、函數(shù)、數(shù)組、結(jié)構(gòu)體以及文件所起的名字。 1命名規(guī)則: ()以字母或下劃線開頭,由字母、數(shù)字和下劃線組成; ()不能和系統(tǒng)關(guān)鍵字同名; ()盡量不要用下劃線開頭; ()一般對(duì)長(zhǎng)度無規(guī)定; ()大小寫是不同的字母(大小寫敏感)。,例:判斷下列標(biāo)識(shí)符號(hào)合法性 sum Sum M.D.John day Date 3days student_name #33 lotus_1_2_3 char ab _above $123,M.D.John,3days,#33,char,$123,ab,3,標(biāo)識(shí)符, 好的命名習(xí)慣: ()盡量做到見名知意;(sum,area) ()變量名、函數(shù)名用小寫,符號(hào)常量用大寫; ()在易混淆地方,盡量避免用易認(rèn)錯(cuò)的字母; e.g. 0(數(shù)字) (大寫字母) o(小寫字母) 1 (數(shù)字) I(I的大寫字母) l(L的小寫字母) (數(shù)字)(大寫字母) z(小寫字母),4,Keywords and Reserved Identifiers,ISO/ANSI C Keywords auto enum restrict unsigned break extern return void case float short volatile char for signed while const goto sizeof _Bool continue if static _Complex default inline struct _Imaginary do int switch double long typedef else register union,5,注意:1C語(yǔ)言區(qū)分大小寫,2標(biāo)識(shí)符的長(zhǎng)度一般不超過32個(gè)字符,如:Price 和 price,系統(tǒng)會(huì)認(rèn)為是兩個(gè)不同的標(biāo)識(shí)符。,具體情況視不同系統(tǒng)而定。,6,數(shù)據(jù)類型 Data Types,程序應(yīng)包括兩個(gè)方面的內(nèi)容: 數(shù)據(jù)的描述。 操作步驟,即動(dòng)作的描述。 數(shù)據(jù)不僅是操作的對(duì)象,而且操作結(jié)果會(huì)改變數(shù)據(jù)的狀況。 計(jì)算機(jī)科學(xué)家沃思(Niklklaus Wirth)提出一個(gè)公式: 數(shù)據(jù)結(jié)構(gòu)十算法=程序 程序算法十?dāng)?shù)據(jù)結(jié)構(gòu)十程序設(shè)計(jì)方法十 語(yǔ)言工具和環(huán)境 C語(yǔ)言中,任何一個(gè)數(shù)據(jù)都必須屬于一種類型,7, C語(yǔ)言對(duì)程序中要用到的每一個(gè)變量 都要事先指定它的數(shù)據(jù)類型!,為什么要指定數(shù)據(jù)類型?,1不同類型的數(shù)據(jù)在內(nèi)存中占據(jù)不同長(zhǎng)度的存儲(chǔ)區(qū)。 2不同類型的數(shù)據(jù)取值范圍不同。 3不同類型的數(shù)據(jù)有不同的操作。,8,C語(yǔ)言數(shù)據(jù)類型,數(shù)據(jù)類型,基本類型,構(gòu)造類型,指針類型,字符型 char 整型 int 實(shí)型 float, double 空類型 void,枚舉型 enum 數(shù)組 (Array) 結(jié)構(gòu)體 struct 共用體 union,9,基本數(shù)據(jù)類型,Void: 1, 函數(shù)不返回任何值 2, 同一類型指針,10,類型修飾符,除void類型以外,基本數(shù)據(jù)類型可以帶各種修飾符作為前綴. 右邊四個(gè)修飾符可以用于字符型和整型的基本類型. Long還可以用于雙精度實(shí)型. 整型數(shù)缺省狀態(tài)為signed和short.,11,基本類型和修飾符組合,12,數(shù)據(jù)類型的缺省等價(jià)形式,13,浮點(diǎn)數(shù),在計(jì)算機(jī)中,定點(diǎn)數(shù)通常只用于表示整數(shù)或純小數(shù)。而對(duì)于既有整數(shù)部分又有小數(shù)部分的數(shù),由于其小數(shù)點(diǎn)的位置不固定,一般用浮點(diǎn)數(shù)表示,由兩部分組成:尾數(shù)和2的冪,地址,0,內(nèi)容,SEEEEEEE,EMMMMMMM,MMMMMMMM,MMMMMMMM,1,2,3,說明: S:符號(hào)位,1代表負(fù),0代表正 E:偏移127的冪,為階碼(即多少次方的意思) M:24位尾數(shù)保存在23位中,最高位為1省略,則:浮點(diǎn)數(shù)的值(符號(hào))2(EEEEEEEE)-127尾數(shù),14,例:浮點(diǎn)數(shù)0xC1 0x48 0x00 0x00的值 二進(jìn)制表示:11000001 01001000 00000000 00000000 符號(hào)位S1,故為負(fù)數(shù) 階碼(10000010)1271301273 尾數(shù)1.1001000 00000000 00000000 1.5625 所以:其代表的值為-1231.5625-12.5,15,常量 constants,Some data are preset before a program is used and keep their values unchanged throughout the life of the program. These are constants.(常量:具有固定的值,且不會(huì)被程序改變) 數(shù)值常量整型常量,實(shí)型常量,雙精度常量 字符常量 字符串常量 符號(hào)常量,16,整型常量,整型常量即整常數(shù)。整常數(shù)可用以下三種形式表示: 十進(jìn)制整數(shù)。如123、-456、0。 八進(jìn)制整數(shù)。以0開頭的數(shù)是八進(jìn)制數(shù)。如0123表示八進(jìn)制數(shù)123,即(123)8等于十進(jìn)制數(shù)83。-011表示八進(jìn)制數(shù) -11,即十進(jìn)制數(shù)-9。 十六進(jìn)制整數(shù)。以0x開頭的數(shù)是進(jìn)制數(shù)。 如0x123,代表16進(jìn)制數(shù)123,即(123)161162163160=256323=291。-0x12等于十進(jìn)制數(shù)一18。,整型常量的類型 根據(jù)其值所在范圍確定其數(shù)據(jù)類型 在整常量后加字母l或L,認(rèn)為它是long int 型常量,例 12 與 12L,例 30000 為int型 65536 為long int 型,17,實(shí)型常量,實(shí)數(shù)在語(yǔ)言中又稱浮點(diǎn)數(shù),實(shí)數(shù)有兩種表示形式: 1.小數(shù)形式。它由數(shù)字和小數(shù)點(diǎn)組成(注意必須有小數(shù)點(diǎn))。0.123、.123、123.0、123.、0.0都是十進(jìn)制數(shù)形式。 2.指數(shù)形式。如123e3或123E3都代表123103但注意字母e(或E)之前必須有數(shù)字。且e后面指數(shù)必須為整數(shù),如e3、2.1e3.5、e等都不是合法的指數(shù)形式。 規(guī)范化(標(biāo)準(zhǔn)化)指數(shù)形式:在字母e(或E)之前的小數(shù)部分中,小數(shù)點(diǎn)左邊應(yīng)有一位(且只能有一位)非零的數(shù)字。,實(shí)型常量的類型 默認(rèn)double型 在實(shí)型常量后加字母f或F,認(rèn)為它是float 型,18,定義:用單引號(hào)括起來的單個(gè)普通字符或轉(zhuǎn)義字符.,字符常量的值:該字符的ASCII碼值,如 101 -A 012 -n 376 - x61 -a 60 -0 483 -(),例: A-101-x41-65,如 A65, a97, 048 , n10,如 a A ? n 101,轉(zhuǎn)義字符:反斜線后面跟一個(gè)字符或一個(gè)代碼值表示,例 轉(zhuǎn)義字符舉例 main() printf(“101 x42 Cn“); printf(“I say:“How are you?“n“); printf(“C Programn“); printf(“Turbo C“); ,運(yùn)行結(jié)果:(屏幕顯示) A B C Isay:”How are you?” C Program Turbo C,例 main() printf(“Yb=n”); ,運(yùn)行結(jié)果: 屏幕顯示:= 打印機(jī)輸出:,字符常量,19,字符常量與字符串常量不同,定義:用雙引號(hào)(“”)括起來的字符序列 存儲(chǔ):每個(gè)字符串尾自動(dòng)加一個(gè) 0 作為字符串結(jié)束標(biāo)志,例: char ch; ch=“A”;,字符串常量,20,舉例,#include main() printf(“ ab ct denftgn“); printf(“ ab ct derftgn“); printf(“htibbj kn“); printf(“htij kn“); printf(“h12345678ij kn“); ,21,Constants and the C Preprocessor,Sometimes you need to use a constant in a program. For example, you could give the circumference of a circle as follows: circumference = 3.14159 * diameter; Here, the constant 3.14159 represents the world-famous constant pi (). To use a constant, just type in the actual value, as in the example. However, there are good reasons to use a symbolic constant instead. That is, you could use a statement such as the following and have the computer substitute in the actual value later: circumference = pi * diameter;,22,Why to use a symbolic constant?,First, a name tells you more than a number does. Compare the following two statements: owed = 0.015 * housevalue; owed = taxrate * housevalue; If you read through a long program, the meaning of the second version is plainer. Also, suppose you have used a constant in several places, and it becomes necessary to change its value. After all, tax rates do change. Then you only need to alter the definition of the symbolic constant, rather than find and change every occurrence of the constant in the program.,23,how set up a symbolic constant?,One way is to declare a variable and set it equal to the desired constant. You could write this: float taxrate; taxrate = 0.015; This provides a symbolic name, but taxrate is a variable, so your program might change its value accidentally. Fortunately, C has a couple better ideas.,24,#define NAME value,Just add a line like the following at the top of the file containing your program: #define TAXRATE 0.015 When your program is compiled, the value 0.015 will be substituted everywhere you have used TAXRATE. This is called a compile time substitution.,25,符號(hào)常量 Symbolic Constants,C語(yǔ)言允許將程序中的常量定義為一個(gè)標(biāo)識(shí)符,稱為符號(hào)常量。符號(hào)常量一般使用大寫英文字母表示,以區(qū)別于一般用小寫字母表示的變量。符號(hào)常量在使用前必須先定義,定義的形式是: #define 是宏定義預(yù)處理命令,不是C語(yǔ)句 例如: #define PI 3.1415926 #define TRUE 1 #definr FALSE 0 #define STAR * 這里定義PI、TRUE、FLASE、STAR為符號(hào)常量,其值分別為3.1415926,1,0,*。,26,compile time substitution,27,examples,The #define statement can be used for character and string constants, too. Just use single quotes for the former and double quotes for the latter. The following examples are valid: #define BEEP a #define TEE T #define ESC 033 #define OOPS “Now you have done it!“ Remember that everything following the symbolic name is substituted for it. Dont make this common error: /* the following is wrong */ #define TOES = 20 If you do this, TOES is replaced by = 20, not just 20. In that case, a statement such as digits = fingers + TOES; is converted to the following misrepresentation: digits = fingers + = 20;,28,The #undef Directive,The #undef directive cancels an earlier #define definition. That is, suppose you have this definition: #define LIMIT 400 Then the directive #undef LIMIT removes that definition. Now, if you like, you can redefine LIMIT so that it has a new value. Even if LIMIT is not defined in the first place, it is still valid to undefine it.,29,The #undef Directive,#define PI 3.14159 Main() #undef PI /* 從此往后,PI不在代表3.14159 */ F1() ,30,帶參數(shù)的宏定義,除了字符串的替換,還要進(jìn)行參數(shù)替換。 定義形式: #define S(a,b) a*b . . area = S(3,2); 預(yù)處理后: area=3*2; 注意:1,宏名和參數(shù)之間不能有空格 2,注意括號(hào)的使用,保證運(yùn)算次序。,31,The const Modifier,C90 added a second way to create symbolic constantsusing the const keyword to convert a declaration for a variable into a declaration for a constant: const int MONTHS = 12; / MONTHS a symbolic constant for 12 This makes MONTHS into a read-only value. That is, you can display MONTHS and use it in calculations, but you cannot alter the value of MONTHS.,32,概念:其值可以改變的量 變量名與變量值 變量定義的一般格式: 數(shù)據(jù)類型 變量1,變量2,變量n;,變量初始化:定義時(shí)賦初值,例: int a,b,c; float data;,決定分配字節(jié)數(shù) 和數(shù)的表示范圍,合法標(biāo)識(shí)符,例: int a=2,b,c=4; float data=3.67; char ch=A; int x=1,y=1,z=1; int x=y=z=1; X,變量的使用:先定義,后使用,例1 int student; stadent=19; /Undefined symbol stadent in function main,例2 float a,b,c; c=a%b; /Illegal use of floating point in function main,變量定義位置:一般放在函數(shù)開頭,變量 Variables,33,占字節(jié)數(shù)隨機(jī)器不同而不同,一般占一個(gè)機(jī)器字 shortintlong 可用sizeof(類型標(biāo)識(shí)符)測(cè)量,實(shí)型變量 float:占4字節(jié),提供7位有效數(shù)字 double:占8字節(jié),提供1516位有效數(shù)字,字符型變量 字符變量存放字符ASCII碼 char與int數(shù)據(jù)間可進(jìn)行算術(shù)運(yùn)算,例1: float a; a=111111.111; /* a=111111.1*/ 例2: double b; b=111111.111; /* b=111111.111*/,例 a=D; /* a=68; */ x=A+5; /* x=65+5; */ s=!+G /* s=33+71; */,沒有字符串變量,用字符數(shù)組存放,整型變量,34,#define PRICE 12.5 main() int num=3; float total; char ch1,ch2=D; total=num*PRICE; ch1=ch2-A+a; printf(“total=%f,ch1=%cn”,total,ch1); ,運(yùn)行結(jié)果: total=37.500000, ch1=d,例子,35,Enumerated Types,declare symbolic names to represent integer constants. By using the enum keyword, you can create a new “type“ and specify the values it may have. (Actually, enum constants are type int, therefore they can be used wherever you would use an int.) The purpose of enumerated types is to enhance the readability of a program. For example, you can make these declarations: enum spectrum red, orange, yellow, green, blue, violet ; enum spectrum color; The identifiers within the braces enumerate the possible values that a spectrum variable can have.,establishes spectrum as a tag name,makes color a variable of that type,36,Usage,Therefore, the possible values for color are red, orange, yellow, and so on. Then, you can use statements like the following: int c; color = blue; if (color = yellow) .; for(color =red;color = violet;color+) .;,37,Enum Constants,Just what are blue and red? Technically, they are type int constants. For example, given the preceding enumeration declaration, you can try this: printf(“red = %d, orange = %dn“, red, orange); Here is the output: red = 0, orange = 1 What has happened is that red has become a named constant representing the integer 0. Similarly, the other identifiers are named constants representing the integers 1 through 5.,38,Default Values,By default, the constants in the enumeration list are assigned the integer values 0, 1, 2, and so on. Therefore, the declaration enum kids nippy, slats, skippy, nina, liz ; results in nina having the value 3. Assigned Values You can choose the integer values that you want the constants to have. Just include the desired values in the declaration: enum levels low = 100, medium = 500, high = 2000 ;,39,Assigned Values,If you assign a value to one constant but not to the following constants, the following constants will be numbered sequentially. For example, suppose you have this declaration: enum feline cat, lynx = 10, puma, tiger ; Then cat is 0, by default, and lynx, puma, and tiger are 10, 11, and 12, respectively.,40,變量賦初值,1.初始化:在定義變量的同時(shí)為變量賦初值 2.形式: 類型標(biāo)識(shí)符 變量名=常量或常量表達(dá)式 例如: int x=10 ; char ch=a ; 變量賦初值允許使用符號(hào)常量 例如: #define PI 3.1415926 .float x=PI ; . 可對(duì)被定義的變量的一部分賦初值 例如: int a , b, c=1, d=2; float r=2 ,l, s; 可對(duì)幾個(gè)變量賦以同一個(gè)初值 例如: int a=6, b=6, c=6; 不要寫為: int a=b=c=6;,41,初始化,初始化不是在編譯階段完成的(只有靜態(tài)存儲(chǔ)變量和外部變量的初始化是在編譯階段完成的),而是在程序運(yùn)行時(shí)執(zhí)行本函數(shù)時(shí)賦以初值的。相當(dāng)于有一個(gè)賦值語(yǔ)句,例如: int a=3; 相當(dāng)于: int a; *指定a為整型變量*/ a=3; *賦值語(yǔ)句,將3賦予a* 又如 int a,b, c=5; 相當(dāng)于: int a,b, c; *指定a、b、c為整型變量* c5; *將5賦給c*/,42,C 運(yùn) 算 符,算術(shù)運(yùn)算符:(+ - * / % + -) 關(guān)系運(yùn)算符:( = !=) 邏輯運(yùn)算符:(! & |) 位運(yùn)算符 :( | &) 賦值運(yùn)算符:(= 及其擴(kuò)展) 條件運(yùn)算符:(?:) 逗號(hào)運(yùn)算符:(,) 指針運(yùn)算符:(* &) 求字節(jié)數(shù) :(sizeof) 強(qiáng)制類型轉(zhuǎn)換:(類型) 分量運(yùn)算符:(. -) 下標(biāo)運(yùn)算符:() 其它 :(( ) -),Operators, Expressions 運(yùn)算符和表達(dá)式,43,Operators, Expressions 運(yùn)算符和表達(dá)式,運(yùn)算符功能 與運(yùn)算量關(guān)系 要求運(yùn)算量個(gè)數(shù) 要求運(yùn)算量類型 運(yùn)算符優(yōu)先級(jí)別 結(jié)合方向 結(jié)果的類型,學(xué)習(xí)運(yùn)算符應(yīng)注意,44,運(yùn)算結(jié)合方向,兩種: 1. 自左向右,為左結(jié)合性。 2. 自右向左,為右結(jié)合性。 若在運(yùn)算量的兩側(cè)的運(yùn)算符有相同的優(yōu)先級(jí),則按它們的結(jié)合方向順序處理。,例: a b / c 1.5 + ad e;,a=b=c=d;,相當(dāng)于: a=(b=(c=d);,45,Arithmetic Operators,+ 加法,正值。 如: 3+6, +3, 減法,負(fù)值。 如: 6 4, 5, 乘法。 如: 38,/ 除法。 如: 8 / 5,% 求余。 如: 7 % 4 的值為3,46,基本算術(shù)運(yùn)算符: + - * / % 結(jié)合方向:從左向右 優(yōu)先級(jí): - -* / % - + - (2) (3) (4) 說明: “-”可為單目運(yùn)算符時(shí),右結(jié)合性 兩整數(shù)相除,結(jié)果為整數(shù) %要求兩側(cè)均為整型數(shù)據(jù),例 5/2 = -5/2.0 =,例 5%2 = -5%2 = 1%10 = 5%1 = 5.5%2,算術(shù)運(yùn)算符和表達(dá)式,2,-2.5,1,-1,1,0,(),47,Division Operator : integer division,兩個(gè)整數(shù)相除結(jié)果為整數(shù),如5/3的結(jié)果值為1,舍去小數(shù)部分。 但是如果除數(shù)或被除數(shù)中有一個(gè)為負(fù)值,則舍入的方向是不固定的。 例如,53在有的機(jī)器上得到結(jié)果1,有的機(jī)器則給出結(jié)果2。 多數(shù)機(jī)器采取“向零取整”方法,即5/31, 53= 1,取整后向零靠攏。 如果參加運(yùn)算的兩個(gè)數(shù)中有一個(gè)數(shù)為實(shí)數(shù),則結(jié)果是double型,因?yàn)樗袑?shí)數(shù)都按double型進(jìn)行運(yùn)算。,48,The divide.c Program,/* divide.c - divisions we have known */ #include int main(void) printf(“integer division: 5/4 is %d n“, 5/4); printf(“integer division: 6/3 is %d n“, 6/3); printf(“integer division: 7/4 is %d n“, 7/4); printf(“floating division: 7./4. is %1.2f n“, 7./4.); printf(“mixed division: 7./4 is %1.2f n“, 7./4); return 0; ,integer division: 5/4 is 1 integer division: 6/3 is 2 integer division: 7/4 is 1 floating division: 7./4. is 1.75 mixed division: 7./4 is 1.75,Notice how integer division does not round to the nearest integer, but always truncates, that is, discards the entire fractional part.,49,Modulus Operator: %,used in integer arithmetic. gives the remainder that results when the integer to its left is divided by the integer to its right. For example, 13 % 5 (read as “13 modulo 5“) has the value 3, because 5 goes into 13 twice, with a remainder of 3. Dont bother trying to use this operator with floating-point numbers. It just wont work. One common use is to help you control the flow of a program.,50,What about negative numbers?,Before C99 settled on the “truncate toward zero“ rule for integer division, there were a couple of possibilities. But with the rule in place, you get a negative modulus value if the first operand is negative and a positive modulus otherwise: 11 / 5 is 2 and 11 % 5 is 1 11 / -5 is -2 and 11 % -2 is 1 -11 / -5 is 2 and -11 % -5 is -1 -11 / 5 is -2 and -11 % 5 is -1 If your system shows different behavior, it hasnt caught up to the C99 standard. In any case, the standard says, in effect, that if a and b are integer values, you can calculate a%b by subtracting (a/b)*b from a. For example, you can evaluate “-11%5” this way: -11 - (-11/5) * 5 = -11 -(-2)*5 = -11 -(-10) = -1,51,Increment and Decrement Operators: + and -,The increment operator performs a simple task; it increments (increases) the value of its operand by 1. This operator comes in two varieties. The first variety has the + come before the affected variable; this is the prefix mode. The second variety has the + after the affected variable; this is the postfix mode. The two modes differ with regard to the precise time that the incrementing takes place.,52,自增、自減運(yùn)算符(+ and -),作用是使變量的值增1或減1,如: + i, - i (在使用i之前,先使i的值加(減)1) i + ,i - (在使用i之后,使i的值加(減)l) +i和i+的作用相當(dāng)于ii+1。但+i和i+不同之處在于+i是先執(zhí)行i=i+1后,再使用i的值;而i+是先使用i的值后,再執(zhí)行i=i+1。如果i的原值等于3,則: j+i; j的值為4 j=i+; j的值為3,然后i變?yōu)? 又如: i=3; printf (”d”,+ i); 輸出“4”。若改為 printf(”d”,i+ ); 則輸出“3”。,例 j=3; k=+j; j=3; k=j+; j=3; printf(“%d”,+j); j=3; printf(“%d”,j+); a=3;b=5;c=(+a)*b; a=3;b=5;c=(a+)*b;,/k=4,j=4,/k=3,j=4,/4,/3,/c=20,a=4,/c=15,a=4,53,說明,+/-只能用于變量,不能用于常量或表達(dá)式,如5+或(a+b)+都是不合法的。因?yàn)?是常量,常量的值不能改變。(a+b)+也不可能實(shí)現(xiàn),假如a+b的值為5,那么自增后得到的6放在什么地方呢?無變量可供存放 +,-的結(jié)合方向是“自右至左”。運(yùn)算符的結(jié)合方向?yàn)椤白宰蠖摇?,是大家所熟知的。如果?i+,i的左面是負(fù)號(hào)運(yùn)算符,右面是自加運(yùn)算符。如果i的原值等于3,若按左結(jié)合性,相當(dāng)于(-i)+,而(-i)+是不合法的。對(duì)表達(dá)式不能進(jìn)行自加自減運(yùn)算。 自增(減)運(yùn)算符常用于循環(huán)語(yǔ)句中使循環(huán)變量自動(dòng)加1。也用于指針變量,使指針指向下一個(gè)地址。,54,說明,+ -結(jié)合方向: 自右向左 優(yōu)先級(jí):- + - -* / % -+ - (2) (3) (4),例 -i+ -(i+) i=3; printf(“%d”,-i+); /-3,例 -i+ i=3; printf(“%d”,-i+);,55,Assignment Operator: =,In C, the equal sign does not mean “equals.“ Rather, it is a value-assigning operator. The statement bmw = 2002; assigns the value 2002 to the variable named bmw. That is, the item to the left of the = sign is the name of a variable, and the item on the right is the value assigned to the variable. The = symbol is called the assignment operator. Again, dont think of the line as saying, “bmw equals 2002.“ Instead, read it as “assign the value 2002 to the variable bmw.“ The action goes from right to left for this operator.,56,The statement i = i + 1;.,i = i + 1; As mathematics, this statement makes no sense. If you add 1 to a finite number, the result isnt “equal to“ the number you started with, but as a computer assignment statement, it is perfectly reasonable. It means “Find the value of the variable named i; to that value, add 1, and then assign this new value to the variable named i“,57,Modifiable lvalue,A statement such as 2002 = bmw; makes no sense in C (indeed, is invalid) because 2002 is just a constant. You cant assign a value to a constant; When you sit down at the keyboard,remember that item to the left of the = sign must be the name of a variable. Actually, the left side must refer to a storage location. The simplest way is to use the name of a variable, but, as you will see later, a “pointer“ can be used to point to a location. More generally, C uses the term modifiable lvalue to label those entities to which you can assign values.,58,Data Objects, Lvalues, Rvalues, and Operands,A data object is a general term for a region of data storage that can be used to hold values. C uses the term lvalue to mean a name or expression that identifies a particular data object. The name of a variable is an lvalue, so object refers to the actual data storage, but lvalue is a label used to identify, or locate, that storage. Not all objects can have their values changed, so C uses the term modifiable lvalue to identify objects whose value can be changed. left side of an assignment operator should be a modifiable lvalue. L Left,59,rvalue,refers to quantities that can be assigned to modifiable lvalues. For instance, consider the following statement: bmw = 2002; Here, bmw is a modifiable lvalue, and 2002 is an rvalue. As you probably guessed, the r in rvalue comes from right. Rvalues can be constants, variables, or any other expression that yields a value. As long as you are learning the names of things, the proper term for what we have called an “item“ (as in “the item to the left of the =“) is operand. Operands are what operators operate on.,60,triple assignment,/* golf.c - golf tournament scorecard */ #include int main(void) int jane, tarzan, cheeta; cheeta = tarzan = jane = 68; printf(“ cheeta tarzan janen“); printf(“First round score %4d %8d %8dn“,cheeta,tarzan,jane); return 0; ,The assignments are made right to left: First jane gets the value 68, and then tarzan does, and finally cheeta does. Therefore, the output is as follows: cheeta tarzan jane First round score 68 68 68,61,簡(jiǎn)單賦值運(yùn)算符,符號(hào): = 格式: 變量標(biāo)識(shí)符=表達(dá)式 作用:將一個(gè)數(shù)據(jù)(常量或表達(dá)式)賦給一個(gè)變量,例 a=3; d=func(); c=d+2;,62,復(fù)合賦值運(yùn)算符,種類:+= -= *= /= %= = &= = |= 含義: exp1 op= exp2 exp1 = exp1 op exp2,63,復(fù)合賦值運(yùn)算符,結(jié)合方向:自右向左
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026江蘇海晟控股集團(tuán)有限公司下屬子公司招聘任務(wù)型合同制員工20人筆試備考題庫(kù)及答案解析
- 2026甘肅人力資源服務(wù)股份有限公司社會(huì)招聘筆試備考題庫(kù)及答案解析
- 2026上海戲曲藝術(shù)中心所屬上海長(zhǎng)江劇場(chǎng)(上海市宛平藝苑)副總經(jīng)理招聘1人筆試備考題庫(kù)及答案解析
- 2026內(nèi)蒙古烏蘭察布卓資縣招聘公益性崗位人員46人筆試備考試題及答案解析
- 2026年長(zhǎng)沙商貿(mào)旅游職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測(cè)試備考試題及答案詳細(xì)解析
- 朱朝敏《七月桃店》閱讀答案
- 5.1.1 生物與環(huán)境的相互作用教學(xué)設(shè)計(jì)(2025-2026學(xué)年人教版生物八年級(jí)上冊(cè))
- 大通縣緊密型縣域醫(yī)共體總院所屬分院編制外工作人員招聘筆試備考題庫(kù)及答案解析
- 2026浙江大學(xué)相關(guān)科創(chuàng)中心海外優(yōu)青招聘筆試備考試題及答案解析
- 2026江西事業(yè)單位聯(lián)考宜春市招聘343人筆試備考試題及答案解析
- 2025年江蘇省無錫市中考物理真題卷含答案解析
- 中國(guó)血脂管理指南2025年版ascvd的
- 科技寫作與文獻(xiàn)檢索課程論文試題(卷)及答案
- (2021-2025)5年高考1年模擬物理真題分類匯編專題07 靜電場(chǎng)(北京專用)(解析版)
- 新疆2025年通信安全員c證題庫(kù)教材及答案解析
- 管家安全知識(shí)培訓(xùn)內(nèi)容課件
- 數(shù)據(jù)處理基礎(chǔ)講解
- 常見疾病在輸液室護(hù)理要點(diǎn)
- 糖尿病性外展神經(jīng)麻痹的護(hù)理課件
- GJB2489A2023航空機(jī)載設(shè)備履歷本及產(chǎn)品合格證編制要求
- 熱點(diǎn)話題18 航天新征程:神舟二十號(hào)引領(lǐng)科技創(chuàng)新與傳統(tǒng)突破-2025年高考語(yǔ)文作文主題預(yù)測(cè)+素材+模擬范文
評(píng)論
0/150
提交評(píng)論