资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,Computer Programming,Lecture 2,Lecture 2:Outline,Variables,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,assignement,Variables-Examples,int a;/declaring a variable of type int,int sum,a1,a2;/declaring 3 variables,int x=7;/declaring and initializing a variable,a=5;/assigning to variable a the value 5,a1=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 with 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 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 program,Data 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(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,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 8,2,+7 8+7).,An integer 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 taken 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 16,2,+3 16+15
展开阅读全文