Академический Документы
Профессиональный Документы
Культура Документы
Overview of C
Part I
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
2-2
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)
2-5
C Language Elements
-- Syntax for Preprocessor Directives (Cont)
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
2-8
C Language Elements
-- Function main
2-9
C Language Elements
-- Reserved Words
Reserved word
a word that has special meaning in C
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!)
2-11
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
2-14
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.
2-17
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
2-19
2-20
2-21
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;
2-23
2-24
Executable Statements
-- Assignment Statements
2-25
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)
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)
2-31
Executable Statements
-- The printf Function (Cont)
2-32
Executable Statements
-- The printf Function (Cont)
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);
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
2-37
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
2-40
Executable Statements
-- The return Statement
2-41
2-42
2-43
2-44
E.g.
double miles, /* input - distance in miles */
kms;
/* output
- distance in kilometers */
Copyright
2004
Pearson
Addison-Wesley. All rights
reserved.
2-45
2-46
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
*/
2-47
2-48