Вы находитесь на странице: 1из 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

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

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-2

Figure 2.1 C Language Elements in


Miles-to-Kilometers Conversion Program

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-3

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
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-4

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

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-5

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

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-6

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
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-7

C Language Elements
-- Function main

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-8

C Language Elements
-- Function main

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-9

C Language Elements
-- Reserved Words

Reserved word
a word that has special meaning in C

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-10

C Language Elements
-- Standard Identifiers

Standard identifier
a word having special meaning but one that a
programmer may redefine (but redefinition is not
recommended!)

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-11

TABLE 2.1 Reserved Words in Fig. 2.1

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-12

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.
Copyright
2004 Pearson
Addison-Wesley. All rights
reserved.
2-13

TABLE 2.2 Invalid Identifiers

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-14

TABLE 2.3 Reserved Words and Identifiers in Fig.


2.1

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-15

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.
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-16

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

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-17

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
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-18

Variable Declarations and Data Types


-- Variable Declarations (Cont)

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-19

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' '*' ':' '"' ' '
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-20

Variable Declarations and Data Types


-- Data Types (Cont)

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-21

Figure 2.2 Memory(a) Before and (b) After


Execution of a Program

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-22

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.
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-23

Figure 2.3 Effect of kms = KMS_PER_MILE *


miles;

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-24

Executable Statements
-- Assignment Statements

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-25

Figure 2.4 Effect of sum = sum + item;

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-26

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
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-27

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
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.
2-28

Executable Statements
-- The printf Function (Cont)

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-29

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
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-30

Executable Statements
-- The printf Function (Cont)

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-31

Executable Statements
-- The printf Function (Cont)

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-32

Executable Statements
-- The printf Function (Cont)

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-33

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 2004
this is the
second.
Copyright
Pearson
Addison-Wesley. All rights
reserved.

2-34

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);

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-35

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);
Copyright
2004 Pearson
Addison-Wesley. All rights
reserved.

2-36

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

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-37

Figure 2.6 Scanning Data Line Bob

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-38

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.
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.
2-39

Executable Statements
-- The scanf Function

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-40

Executable Statements
-- The return Statement

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-41

General Form of a C Program

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-42

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.
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-43

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.
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-44

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 */
Copyright
2004
Pearson
Addison-Wesley. All rights
reserved.

2-45

General Form of a C Program


-- Comments in Programs

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-46

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 programmers 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
*/

Copyright 2004 Pearson


Addison-Wesley. All rights
reserved.

2-47

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;
Copyright 2004 Pearson
Addison-Wesley. All rights
reserved.

2-48

Вам также может понравиться