资源预览内容
第1页 / 共37页
第2页 / 共37页
第3页 / 共37页
第4页 / 共37页
第5页 / 共37页
第6页 / 共37页
第7页 / 共37页
第8页 / 共37页
第9页 / 共37页
第10页 / 共37页
亲,该文档总共37页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Computer ProgrammingLecture 2 02.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 float The Extended Precision Type double The Single Character Type char The Boolean Data Type _Bool Storage sizes and ranges Type 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 Variables Programs can use symbolic names for storing computation data Variable: a symbolic name for a memory location programmer doesnt have to worry about specifying (or even knowing) the value of the locations address In C, variables have to be declared before they are used Variable declaration: symbolic name(identifier), type Declarations that reserve storage are called definitions The definition reserves memory space for the variable, but doesnt put any value there Values 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, _sysflag Examples 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, its not practical to use variable names that are too long) Choice of meaningful variable names can increase the readability of a programData types Basic 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 _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 types#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 (“boolVar = %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 values 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 (Kernighanprintf(“%i %#X %#on“, x,x,x);0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0storage (binary)programdisplay“%i”“%#X”160X10“%#o”020The floating number type floatA variable declared to be of type float can be used for storing values containing decimal places. Examples of floating-point constants : 3., 125.8, .0001 To 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 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.
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号