有关c语言的英文ppt,国外C语言程序设计英文课件-lecture2.ppt

62e50291a81fc54b507f33cb80033297.gif 国外C语言程序设计英文课件-lecture2.ppt

(37页)

c4e733a8ad799c30bde1690c9ffd6ed9.gif

本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦!

19.9 积分

Computer ProgrammingLecture 202.10.2012Lecture 2: OutlineVariables, Data Types, and Arithmetic Expressions [K- ch.4]Working 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 Data 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 Operators Types _Complex and _Imaginary VariablesPrograms can use symbolic names for storing computation data Variable: a symbolic name for a memory locationprogrammer doesn’t have to worry about specifying (or even knowing) the value of the location’s addressIn C, variables have to be declared before they are usedVariable declaration: [symbolic name(identifier), type]Declarations that reserve storage are called definitionsThe definition reserves memory space for the variable, but doesn’t put any value thereValues get into the memory location of the variable by initialization or assignementVariables - Examplesint 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 aL-valueR-valuea1=a1+1; // assigning to variable a1 the value of a1+1 // (increasing value of a1 with 1)Variable declarationsData type Variable nameWhich data types are possible in C ? Which variable names are allowed in C ? Variable namesRules 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 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 a different variable !Variable names can be as long as you want, although only the first 63 (or 31) characters might be significant. (Anyway, it’s not practical to use variable names that are too long)Choice of meaningful variable names can increase the readability of a programData typesBasic data types 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.Data type char: can be used to store a single character, such as the letter a, the digit character 6, or a semicolon. Data type。省略部分。in 29 + 21 (float) 6 / (float) 4 results in 1.5 (float) 6 / 4 results in 1.5The assignment operatorsThe C language permits you to join the arithmetic operators with the assignment operator using the following general format: op=, where op is an arithmetic operator, including +, –, ×, /, and %.op can also be a logical or bit operator => later in this courseExample: count += 10;Equivalent with: count=count+10;Example: precedence of op=: a /= b + cEquivalent with: a = a / (b + c)addition is performed first because the addition operator has higher precedence than the assignment operator _Complex and _Imaginary typesSupported only by a few compilersComplex numbers have two components: a real part and an imaginary part. C99 represents a complex number internally with a two-element array, with the first component being the real part and the second component being the imaginary part.There are three complex types:float _Complex represents the real and imaginary parts with type float values.double _Complex represents the real and imaginary parts with type double values.long double _Complex represents the real and imaginary parts with type long double values. There are three imaginary types; An imaginary number has just an imaginary part: float _Imaginary represents the imaginary part with a type float value.double _Imaginary represents the imaginary part with a type double value.long double _Imaginary represents the imaginary part with a type long double value. Complex numbers can be initialized using real numbers and the value I, defined in and representing i, the square root of –1_Complex example #include // for I int main(void) {double _Complex z1 = 3*I;double _Complex z2=5+4*I;double _Complex z;z=z1*z2;printf("%f+%f*I\n",creal(z),cimag(z) );}Declaring variablesSome older languages (FORTRAN, BASIC) allow you to use variables without declaring them. Other languages (C, Pascal) impose to declare variables Advantages of languages with variable declarations:Putting all the variables in one place makes it easier for a reader to understand the program Thinking about which variables to declare encourages the programmer to do some planning before writing a program ( What information does the program need? What must the program to produce as output? What is the best way to represent the data?)The obligation to declare all variables helps prevent bugs of misspelled variable names.Compiler knows the amount of statically allocated memory needed Compiler can verify that operations done on a variable are allowed by its type (strongly typed languages)Declaration vs DefinitionVariable declaration: [Type, Identifier] Variable definition: a declaration which does also reserve storage space (memory) !Not all declarations are definitionsIn the examples seen so far, all declarations are as well definitions Declarations which are not definitions: later in this semester ! 关 键 词: 国外 c语言 英文 lecture2 lecture

524d6daf746efaa52c3c71bbfe7ba172.gif  天天文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。

关于本文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值