final c elements

48
Chapter 2 Overview of C Part I J. H. Wang ( 王王王 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

Upload: swatantrakumar66

Post on 18-Jul-2016

226 views

Category:

Documents


3 download

DESCRIPTION

Element amalysis

TRANSCRIPT

Page 1: Final c Elements

Chapter 2Overview of C

Part I

J. H. Wang (王正豪 ), Ph. D.

Assistant Professor

Dept. Computer Science and Information Engineering

National Taipei University of Technology

Page 2: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-2

C Language Elements-- Preprocessor Directives

• Preprocessor directive– a C program line beginning with # that provides

an instruction to the preprocessor• Preprocessor

– a system program that modifies a C program prior to its compilation

• Library– a collection of useful functions and symbols that

may be accessed by a program

Page 3: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-3

Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program

Page 4: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-4

C Language Elements-- Preprocessor Directives (Cont’)

• Constant macro– a name that is replaced by a particular constant

value before the program is sent to the compiler• Comment

– text beginning with /* and ending with */ that provides supplementary information but is ignored by the preprocessor and compiler

Page 5: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-5

C Language Elements -- Syntax for Preprocessor Directives (Cont’)

Page 6: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-6

C Language Elements -- Syntax for Preprocessor Directives (Cont’)

Page 7: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-7

C Language Elements -- Function main

• Declarations– the part of a program that tells the compiler the

names of memory cells in a program • Statements

– program lines that are converted to machine language instructions and executed by the computer

Page 8: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-8

C Language Elements -- Function main

Page 9: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-9

C Language Elements -- Function main

Page 10: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-10

C Language Elements -- Reserved Words

• Reserved word – a word that has special meaning in C

Page 11: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-11

C Language Elements -- Standard Identifiers

• Standard identifier – a word having special meaning but one that a

programmer may redefine (but redefinition is not recommended!)

Page 12: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-12

TABLE 2.1 Reserved Words in Fig. 2.1

Page 13: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-13

C Language Elements -- User-Defined Identifiers

• User-defined identifiers– used to name memory cells that will hold data and

program results and to name operations that we define• Valid identifiers:

– An identifier must consist only of letters, digits, and underscores.

– An identifier cannot begin with a digit.– A C reserved word cannot be used as an identifier.– An identifier defined in a C standard library should not

be redefined.

Page 14: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-14

TABLE 2.2 Invalid Identifiers

Page 15: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-15

TABLE 2.3 Reserved Words and Identifiers in Fig. 2.1

Page 16: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-16

C Language Elements -- Uppercase and Lowercase Letters

• Uppercase and lowercase letters are viewed by the compiler as different identifiers.

• Adopting a consistent pattern in the way you use uppercase and lowercase letters is helpful to the readers of your programs.

• One style that has been widely adopted in industry uses all uppercase letters in the names of constant macros.

Page 17: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-17

C Language Elements-- Program Style

• Most programs will be examined or studied by someone other than the original programmers.– pick a meaningful name– placing the underscore character (_) between words– choose identifiers to convey your meaning– avoid excessively long names– do not choose names that are similar to each other

Page 18: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-18

Variable Declarations and Data Types-- Variable Declarations

• Variable – a name associated with a memory cell whose

value can change • Variable declarations

– statements that communicate to the compiler the names of variables in the program and the kind of information stored in each variable

Page 19: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-19

Variable Declarations and Data Types-- Variable Declarations (Cont’)

Page 20: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-20

Variable Declarations and Data Types-- Data Types• Data type

– a set of values and operations that can be performed on those values

• Data Type “int”– -10500, 435, +15, -25 32767

• Data Type “double”– 1.23 × 105 is equivalent to 123000.0– 1.23e5 or 1.23E5

• Read the letter e or E as “times 10 to the power”.• Data Type “char”

– 'A' 'z' '2' '9' '*' ':' '"' ' '

Page 21: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-21

Variable Declarations and Data Types-- Data Types (Cont’)

Page 22: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-22

Figure 2.2 Memory(a) Before and (b) After Execution of a Program

Page 23: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-23

Executable Statements-- Assignment Statements

• assignment statement– an instruction that stores a value or a

computational result in a variable – E.g. kms = KMS_PER_MILE * miles;

• Read = as “becomes,” “gets,” or “takes the value of ” rather than “equals” because it is not equivalent to the equal sign of mathematics.

Page 24: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-24

Figure 2.3 Effect of kms = KMS_PER_MILE * miles;

Page 25: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-25

Executable Statements-- Assignment Statements

Page 26: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-26

Figure 2.4 Effect of sum = sum + item;

Page 27: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-27

Executable Statements-- Input/Output Operations and Functions• input operation

– an instruction that copies data from an input device into memory

• output operation – an instruction that displays information stored in

memory • input/output function

– a C function that performs an input or output operation • function call

– Calling or activating a function

Page 28: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-28

Executable Statements-- The printf Function

• function argument – enclosed in parentheses following the function name;

provides information needed by the function • format string

– in a call to printf, a string of characters enclosed in quotes ("), which specifies the form of the output line

• print list – in a call to printf, the variables or expressions whose

values are displayed

Page 29: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-29

Executable Statements-- The printf Function (Cont’)

Page 30: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-30

Executable Statements-- The printf Function (Cont’)

• placeholder a symbol beginning with % in a format string that indicates where to display the ouput value

• newline escape sequence the character sequence \n, which is used in a format string to terminate an output line

Page 31: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-31

Executable Statements-- The printf Function (Cont’)

Page 32: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-32

Executable Statements-- The printf Function (Cont’)

Page 33: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-33

Executable Statements-- The printf Function (Cont’)

Page 34: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-34

Executable Statements-- The printf Function (Cont’)• cursor

– a moving place marker that indicates the next position on the screen where information will be displayed

– When executing a printf function call, the cursor is advanced to the start of the next line on the screen if the \n escape sequence is encountered in the format string.

– printf("Here is the first line\n");printf("\nand this is the second.\n");

– Here is the first line

and this is the second.

Page 35: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-35

Executable Statements-- The printf Function (Cont’)

• prompt (prompting message) – a message displayed to indicate what data to

enter and in what form – printf("Enter the distance in miles> ");

scanf("%lf", &miles);

Page 36: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-36

Executable Statements-- The scanf Function• Fig 2.5 Effect of scanf("%lf", &miles);• The name of each variable that is to be given a

value is preceded by the ampersand character (&), the C address-of operator.

• & tells the scanf function where to find each variable into which it is to store a new value.

• Once <return> or <enter> is pressed, the data are processed exactly as typed.

• Fig 2.6 scanf("%c%c%c", &letter_1, &letter_2, &letter_3);

Page 37: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-37

Figure 2.5 Effect of scanf("%lf", &miles);

Page 38: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-38

Figure 2.6 Scanning Data Line Bob

Page 39: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-39

Executable Statements-- The scanf Function (Cont’)

• format placeholder– reflect the type of the variable in which the data will be

stored– one input character is used for a %c– For %lf or %d, the program first skips any spaces and

then scans characters until it reaches a character that cannot be part of the number.

– If you type more data characters on a line than are needed by the current call to scanf, the extra characters will be processed by the next call to scanf.

Page 40: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-40

Executable Statements-- The scanf Function

Page 41: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-41

Executable Statements-- The return Statement

Page 42: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-42

General Form of a C Program

Page 43: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-43

General Form of a C Program (Cont’)

• C treats most line breaks like spaces so a C statement can extend over more than one line.

• You should not split a statement that extends over more than one line in the middle of an identifier, a reserved word, a constant, or a format string.

Page 44: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-44

General Form of a C Program -- Program Style Spaces in Programs

• The consistent and careful use of blank spaces can improve the readability and the style of a program.

• Indent the body of the main function and insert blank lines between sections of the program.

Page 45: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-45

General Form of a C Program-- Comments in Programs

• Use comments to describe the purpose of the program, the use of identifiers, and the purpose of each program step.

• program documentation – information (comments) that enhances the

readability of a program • E.g.

double miles, /* input - distance in miles */kms; /* output - distance in kilometers */

Page 46: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-46

General Form of a C Program-- Comments in Programs

Page 47: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-47

General Form of a C Program -- Program Style Using Comments• Each program should begin with a header section that

consists of a series of comments specifying– the programmer’s name– the date of the current version– a brief description of what the program does

• E.g./** Programmer: William Bell Date completed: May 9, 2003* Instructor: Janet Smith Class: CIS61** Calculates and displays the area and circumference of a* circle*/

Page 48: Final c Elements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-48

General Form of a C Program -- Program Style Using Comments

• Before you implement each step in the initial algorithm, you should write a comment that summarizes the purpose of the algorithm step.

• Comment should describe what the step does rather than simply restate the step in English.

• E.g./* Convert the distance to kilometers. */kms = KMS_PER_MILE * miles;