国外C语言程序设计英文课件-lectu.ppt

上传人:小飞机 文档编号:5100962 上传时间:2023-06-04 格式:PPT 页数:37 大小:264.99KB
返回 下载 相关 举报
国外C语言程序设计英文课件-lectu.ppt_第1页
第1页 / 共37页
国外C语言程序设计英文课件-lectu.ppt_第2页
第2页 / 共37页
国外C语言程序设计英文课件-lectu.ppt_第3页
第3页 / 共37页
国外C语言程序设计英文课件-lectu.ppt_第4页
第4页 / 共37页
国外C语言程序设计英文课件-lectu.ppt_第5页
第5页 / 共37页
点击查看更多>>
资源描述

《国外C语言程序设计英文课件-lectu.ppt》由会员分享,可在线阅读,更多相关《国外C语言程序设计英文课件-lectu.ppt(37页珍藏版)》请在三一办公上搜索。

1、Computer Programming,Lecture 2,Lecture 2:Outline,Variables,Data Types,and Arithmetic Expressions K-ch.4Working with Variables Understanding Data Types and Constants The Basic Integer Type int The Floating Number Type floatThe Extended Precision Type doubleThe Single Character Type char The Boolean D

2、ata Type _Bool Storage sizes and rangesType Specifiers:long,long long,short,unsigned,and signed Working with Arithmetic Expressions Integer Arithmetic and the Unary Minus Operator The Modulus Operator Integer and Floating-Point Conversions Combining Operations with Assignment:The Assignment Operator

3、s Types _Complex and _Imaginary,Variables,Programs can use symbolic names for storing computation data Variable:a symbolic name for a memory locationprogrammer doesnt have to worry about specifying(or even knowing)the value of the locations addressIn C,variables have to be declared before they are u

4、sedVariable declaration:symbolic name(identifier),typeDeclarations that reserve storage are called definitionsThe definition reserves memory space for the variable,but doesnt put any value thereValues get into the memory location of the variable by initialization or assignement,Variables-Examples,in

5、t a;/declaring a variable of type intint sum,a1,a2;/declaring 3 variablesint x=7;/declaring and initializing a variable a=5;/assigning to variable a the value 5a1=a;/assigning to variable a1 the value of a,L-value,R-value,a1=a1+1;/assigning to variable a1 the value of a1+1/(increasing value of a1 wi

6、th 1),Variable declarations,Data type Variable name,Which data types are possible in C?,Which variable names are allowed in C?,Variable names,Rules for valid variable names(identifiers)in C:Name must begin with a letter or underscore(_)and can be followed by any combination of letters,underscores,or

7、 digits.Any name that has special significance to the C compiler(reserved words)cannot be used as a variable name.Examples of valid variable names:Sum,pieceFlag,I,J5x7,Number_of_moves,_sysflagExamples of invalid variable names:sum$value,3Spencer,int.C is case-sensitive:sum,Sum,and SUM each refer to

8、a different variable!Variable names can be as long as you want,although only the first 63(or 31)characters might be significant.(Anyway,its not practical to use variable names that are too long)Choice of meaningful variable names can increase the readability of a program,Data types,Basic data types

9、in C:int,float,double,char,and _Bool.Data type int:can be used to store integer numbers(values with no decimal places)Data type type float:can be used for storing floating-point numbers(values containing decimal places).Data type double:the same as type float,only with roughly twice the precision.Da

10、ta type char:can be used to store a single character,such as the letter a,the digit character 6,or a semicolon.Data type _Bool:can be used to store just the values 0 or 1(used for indicating a true/false situation).This type has been added by the C99 standard(was not in ANSI C),Example:Using data ty

11、pes,#include int main(void)int integerVar=100;float floatingVar=331.79;double doubleVar=8.44e+11;char charVar=W;_Bool boolVar=0;printf(integerVar=%in,integerVar);printf(floatingVar=%fn,floatingVar);printf(doubleVar=%en,doubleVar);printf(doubleVar=%gn,doubleVar);printf(charVar=%cn,charVar);printf(boo

12、lVar=%in,boolVar);return 0;,The basic data type int,Examples of integer constants:158,10,and 0 No embedded spaces are permitted between the digits,and values larger than 999 cannot be expressed using commas.(The value 12,000 is not a valid integer constant and must be written as 12000.)Integer value

13、s can be displayed by using the format characters%i in the format string of a printf call.Also the%d format characters can be used to display an integer(Kernighan&Ritchie C)Integers can also be expressed in a base other than decimal(base 10):octal(base 8)or hexa(base 16).,Octal notation for integers

14、,Octal notation(base 8):If the first digit of the integer value is 0,the integer is taken as expressed in octal notation.In that case,the remaining digits of the value must be valid base-8 digits and,therefore,must be 07.Example:Octal value 0177 represents the decimal value 127(1 82+7 8+7).An intege

15、r value can be displayed in octal notation by using the format characters%o or%#o in the format string of a printf statement.,Hexadecimal notation for integers,Hexadecimal notation(base 16):If an integer constant is preceded by a zero and the letter x(either lowercase or uppercase),the value is take

16、n as being expressed in hexadecimal.Immediately following the letter x are the digits of the hexadecimal value,which can be composed of the digits 09 and the letters af(or AF).The letters represent the values 1015,respectively.Example:hexadecimal value 0 xA3F represents the decimal value 2623(10 162

17、+3 16+15).The format characters%x,%X,%#x,or%#X display a value in hexadecimal format,Data display vs data storage,The option to use decimal,octal or hexadecimal notation doesnt affect how the number is actually stored internally!When/where to use octal and hexa:to express computer-related values in

18、a more convenient way,int x=16;printf(%i%#X%#on,x,x,x);,0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0,storage(binary),program,display,“%i”,“%#X”,16,0X10,“%#o”,020,The floating number type float,A variable declared to be of type float can be used for storing values containing decimal places.Examples of floating-po

19、int constants:3.,125.8,.0001To display a floating-point value at the terminal,the printf conversion characters%f are used.Floating-point constants can also be expressed in scientific notation.The value 1.7e4 represents the value 1.7 104.The value before the letter e is known as the mantissa,whereas

20、the value that follows is called the exponent.This exponent,which can be preceded by an optional plus or minus sign,represents the power of 10 by which the mantissa is to be multiplied.To display a value in scientific notation,the format characters%e should be specified in the printf format string.T

21、he printf format characters%g can be used to let printf decide whether to display the floating-point value in normal floating-point notation or in scientific notation.This decision is based on the value of the exponent:If its less than 4 or greater than 5,%e(scientific notation)format is used;otherw

22、ise,%f format is used.,The extended precision type double,Type double is very similar to type float,but it is used whenever the range provided by a float variable is not sufficient.Variables declared to be of type double can store roughly twice as many significant digits as can a variable of type fl

23、oat.Most computers represent double values using 64 bits.Unless told otherwise,all floating-point constants are taken as double values by the C compiler!To explicitly express a float constant,append either an f or F to the end of the number:12.5fTo display a double value,the format characters%f,%e,o

24、r%g,which are the same format characters used to display a float value,can be used.,The character type char,A char variable can be used to store a single character.A character constant is formed by enclosing the character within a pair of single quotation marks.Valid examples:a,;,and 0.Character zer

25、o(0)is not the same as the number(integer constant)0.Do not confuse a character constant with a character string:character 0 and string“0”.The character constant nthe newline characteris a valid character constant:the backslash character is a special character in the C system and does not actually c

26、ount as a character.There are other special characters(escape sequences)that are initiated with the backslash character:,”,tThe format characters%c can be used in a printf call to display the value of a char variableTo handle characters internally,the computer uses a numerical code in which certain

27、integers represent certain characters.The most commonly used code is the ASCII code,Assigning values to char,char letter;/*declare variable letter of type char*/letter=A;/*OK*/letter=A;/*NO!Compiler thinks A is a variable*/letter=“A;/*NO!Compiler thinks“A is a string*/letter=65;/*ok because characte

28、rs are really stored as numeric values(ASCII code),but poor style*/,Data display vs data storage,/*displays ASCII code for a character*/#include int main(void)char ch;ch=A;printf(The code for%c is%i.n,ch,ch);return 0;,0 1 0 0 0 0 0 1,storage(ASCII code),program,display,“%c”,“%d”,A,65,The Boolean Dat

29、a Type _Bool,A _Bool variable is defined in the language to be large enough to store just the values 0 and 1.The precise amount of memory that is used is unspecified._Bool variables are used in programs that need to indicate a Boolean condition.For example,a variable of this type might be used to in

30、dicate whether all data has been read from a file.By convention,0 is used to indicate a false value,and 1 indicates a true value.When assigning a value to a _Bool variable,a value of 0 is stored as 0 inside the variable,whereas any nonzero value is stored as 1.To make it easier to work with _Bool va

31、riables in your program,the standard header file defines the values bool,true,and false:bool endOfData=false;The _Bool type has beed added by C99.Some compilers(Borland C,Turbo C,Visual C)dont support it,Storage sizes and ranges,Every type has a range of values associated with it.This range is deter

32、mined by the amount of storage that is allocated to store a value belonging to that type of data.In general,that amount of storage is not defined in the language.It typically depends on the computer youre running,and is,therefore,called implementation-or machine-dependent.For example,an integer migh

33、t take up 32 bits on your computer,or it might be stored in 64.You should never write programs that make any assumptions about the size of your data types!The language standards only guarantees that a minimum amount of storage will be set aside for each basic data type.For example,its guaranteed tha

34、t an integer value will be stored in a minimum of 32 bits of storage,which is the size of a“word”on many computers.,Integer overflow,What happens if an integer tries to get a value too big for its type(out of range)?,#include int main(void)int i=2147483647;printf(%i%i%in,i,i+1,i+2);return 0;,Program

35、 output:2147483647-2147483648-2147483647,Explanation:On this computer,int is stored on 32 bits:the first bit represents the sign,the rest of 31 bits represent the value.Biggest positive int value here:231-1=2147483647,Floating point round-off error,#include int main(void)float a,b;b=2.0e20+1.0;a=b-2

36、.0e20;printf(%f n,a);return 0;,Program output:4008175468544.000000,Explanation:the computer doesnt keep track of enough decimal places!The number 2.0e20 is 2 followed by 20 zeros and by adding 1you are trying to change the 21st digit.To do this correctly,the program would need to be able to store a

37、21-digit number.A float number is typically just six or seven digits scaled to bigger or smaller numbers with an exponent.,Type Specifiers:long,long long,short,unsigned,signed,Type specifiers:extend or limit the range of certain basic types on certain computer systemsIf the specifier long is placed

38、directly before the int declaration,the declared integer variable is of extended range on some computer systems.Example of a long int declaration:long int factorial;On many systems,an int and a long int both have the same range and either can be used to store integer values up to 32-bits wide(2311,o

39、r 2,147,483,647).A constant value of type long int is formed by optionally appending the letter L(upper-or lowercase)at the end of an integer constant.Example:long int numberOfPoints=131071100L;To display the value of a long int using printf,the letter l is used as a modifier before the integer form

40、at characters i,o,and x,Basic Data Types-Summary,Basic Data Types-Summary(contd.),Knowing actual ranges for types,Defined in the system include files and contains system-dependent values that specify the sizes of various character and integer data types:the maximum size of an int is defined by the n

41、ame INT_MAXthe maximum size of an unsigned long int is defined by ULONG_MAX gives information about floating-point data types.FLT_MAX specifies the maximum floating-point number,FLT_DIG specifies the number of decimal digits of precision for a float type.,Working with arithmetic expressions,Basic ar

42、ithmetic operators:+,-,*,/Precedence:one operator can have a higher priority,or precedence,over another operator.Example:*has a higher precedence than+a+b*cif necessary,you can always use parentheses in an expression to force the terms to be evaluated in any desired order.Associativity:Expressions c

43、ontaining operators of the same precedence are evaluated either from left to right or from right to left,depending on the operator.This is known as the associative property of an operatorExample:+has a left to right associativityIn C there are many more operators-later in this course!(Table A5 in An

44、nex A of Kochan:full list,with precedence and associativity for all C operators),Working with arithmetic expressions,#include int main(void)int a=100;int b=2;int c=25;int d=4;int result;result=a-b;/subtractionprintf(a-b=%in,result);result=b*c;/multiplicationprintf(b*c=%in,result);result=a/c;/divisio

45、nprintf(a/c=%in,result);result=a+b*c;/precedenceprintf(a+b*c=%in,result);printf(a*b+c*d=%in,a*b+c*d);return 0;,Integer arithmetic and the unary minus operator,/More arithmetic expressions#include int main(void)int a=25;int b=2;float c=25.0;float d=2.0;printf(6+a/5*b=%in,6+a/5*b);printf(a/b*b=%in,a/b

46、*b);printf(c/d*d=%fn,c/d*d);printf(-a=%in,-a);return 0;,The modulus operator,/The modulus operator#include int main(void)int a=25,b=5,c=10,d=7;printf(a%b=%in,a%b);printf(a%c=%in,a%c);printf(a%d=%in,a%d);printf(a/d*d+a%d=%in,a/d*d+a%d);return 0;,Modulus operator:%Binary operatorGets the remainder res

47、ulting from integer division%has equal precedence to*and/,Integer and Floating-Point Conversions,Assign an integer value to a floating variable:does not cause any change in the value of the number;the value is simply converted by the system and stored in the floatingAssign a floating-point value to

48、an integer variable:the decimal portion of the number gets truncated.Integer arithmetic(division):int divided to int=result is integer divisionint divided to float or float divided to int=result is real division(floating-point),Integer and Floating-Point Conversions,/Basic conversions in C#include i

49、nt main(void)float f1=123.125,f2;int i1,i2=-150;char c=a;i1=f1;/floating to integer conversionprintf(%f assigned to an int produces%in,f1,i1);f1=i2;/integer to floating conversionprintf(%i assigned to a float produces%fn,i2,f1);f1=i2/100;/integer divided by integerprintf(%i divided by 100 produces%f

50、n,i2,f1);f2=i2/100.0;/integer divided by a floatprintf(%i divided by 100.0 produces%fn,i2,f2);f2=(float)i2/100;/type cast operatorprintf(float)%i divided by 100 produces%fn,i2,f2);return 0;,The Type Cast Operator,f2=(float)i2/100;/type cast operatorThe type cast operator has the effect of converting

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号