Title: A1258150470DjBcm
1Chapter 2
Introduction to The C Programming Language
2Overview of Computers and Software
- Software Development Method
- 1. specify the program requirements
- 2. analyze the problem
- 3. design the algorithm
- 4. implement the algorithm
- 5. test the program
- 6. maintain and update the program
3Program Structure - General Form
preprocessing directives int main(void) declara
tions statements
4First Program - Hello World!
/
/ / Program Hello World
/ /
/ / This program
displays Hello World! on the console.
/ /
/ include ltstdio.hgt int
main(void) printf(Hello World!\n) /
Exit program. / return 0 /
/
See next slide for explaination!
5What it all means?
Preprocessor statement - tells the preprocessor
to read in the contents of the stdio.h file into
the program (no semicolon required since this is
not a C statement)
Comments are good
/
/ / Program Hello World
/ /
/ / This program displays
Hello World! on the console./ /
/ include
ltstdio.hgt int main(void) printf(Hello
World!\n) / Exit program. / return
(0) /
/
Library command - note the which is required
at the end of all C statements
6C Programs Style
C program is made up of Global Declarations
Section and one or more functions. The Global
Declarations section is normally found up front
of the program. Global means that all
declarations are available to all functions that
may follow. Must be careful! More
later!! Global declarations can be helpful and
can also lead to very serious problems. Again,
will discuss later.
7C Programs Style (cont)
Can only have (MUST have) a function named,
main. All functions have a declarations section
(variables are local) to the function (that is
variables are visible only to this function,
and statements, that is, the executables. This
program consists only of a single function
namely main. Program also has preprocessor
directives (precompiler directives) Almost
always use a include ltstdio.hgt and others
8Program Structure
- Comments begin with the characters / and end
with the characters / - Preprocessor directives give instructions to the
C preprocessor, whose job it is to modify the
text of a C program before it is compiled. - Every C program contains one function named main
- The body of the main function is enclosed by
braces,
9Program Structure (continued)
- The main function contains two types of commands
declarations and statements - Declarations and statements are required to end
with a semicolon () - Preprocessor directives do not end with a
semicolon - To exit the program, use a return (0) statement
10Figure 2-3
Will almost always have this include
statement. This one says include a file called
standard i/o which happens to be a header or
library file. Net result is that some additional
code is brought into the program and will be
compiled with everything else below, as
appropriate.
Simple Hello World! program.
11Figure 2-3
This program a single preprocessor directive,
the include (as usual). No global declarations
no local declarations. The stdio.h file is needed
to support reading and printing in a C
program Note the sign Note the format of the
function header int ? function will return
an integer (could also be void) functions name
is main function takes no parameters
12Figure 2-3
Note that there are no declarations. Note that a
function starts with a left (opening) brace and
ends with a right (closing) brace Two
executable statements in the function. To print,
we call a print function, printf, and feed it a
list of parameters enclosed in parens. In this
case, there is a single parameter passed to the
print function, printf. The parameter is text,
Hello World! and it includes a special
character in C, the \n (backslash n). The
second statement, return 0, means that when this
statement is executed, a zero is returned to the
operating system, which will interpret this
program as having terminated normally.
13 Comments are used to internally document your
program. Essential!!!! Comments are ignored by
the compiler. Can appear anywhere Will show
you the Documentation Standard in a moment.
14(No Transcript)
15C doesnt care much about spaces
include ltstdio.hgt/ My first C program which
prints Hello World / int main(void)printf("Hello
World!\n")return 0
include ltstdio.hgt / My first C program which
prints Hello World / int main
(void ) printf ( "Hello World!\n" ) return
0
Both of these programs are exactly the same as
the original Hello World as far as the C
compiler is concerned. Note that words have to
be kept together and so do things in quotes. The
documentation standard provided on the class web
site details how your programs should be
formatted.
16/
//
/ / Program Name   Lab 3                   Â
                          / /                  Â
                                                 Â
  / / Student Name   John Q.
Programmer                                  / /Â
Semester       Fall, 2005                    Â
                  / / Class Section CoSc
10503 - 15Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
/ / Instructor     Dr. Hallie
Pena                                    / /Â
Due Date       October 15, 2005                Â
                  / /                         Â
                                             / /
 Program Overview                              Â
                     / /     This program
reads in a one-dimensional array of
floating-point / /Â Â Â Â Â values and determines
the maximum value, the minimum value,
and  / /     the mean value. The user must
enter initially the numberof      / /
values to read.
/ /Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
                                         //Â
Input                                           Â
                   / /     An initial integer
value representing the number of floating-
/ / point values to read. A list of the
values which will be used to / / determine
the maximum, minimum, and mean.
/ /Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
                               / /Â
Output                                          Â
                   / /     A value
representing the number of floating-point values
read, Â / /Â Â Â Â Â followed by the maximum,
minimum, and mean.
/ /Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
                            / / Program
Limitations                                     Â
           / /     (1) only floating-point
values may be input.
  / /     (2) The user must first enter a
value representing the number of / /        Â
floating-point values that will follow.
/ /Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
                                     / /Â
Significant Program ariablesÂ
/ /Â Â Â Â Â
values               - an array holding the
floating-point / /Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
    values being read in.            Â
/ /     n              - the number of
floating-point datavalues / /                Â
           to read.                      Â
  / /     max                - the
computed maximum value. Â Â Â / /Â Â Â Â Â
min            - the computed minimum
value.     / /     mean       Â
       - the computed mean value.
   / /                       Â
                               / /Â
References                                      Â
                   / /     (1) this program
originally appeared in "Programming in C with
/ /Â Â Â Â Â Â Â Â Â Numerical Methods for Engineers",
Kamal B. Rohiani, Prentice / /         Hall,
1996, pp. 392-393.                               Â
     / /
/ include
ltstdio.hgt define SIZE 100 /
/ /Â Function prototypes
     / /
/ double maxArray(double x, int n) double
minArray(double x, int n) double
meanArray(double x, int n) /
/ /Â Main driver program
     / /
/ main() / start of main program /
/ Variable declarations /
double dataSIZE double max, min,
mean int i, n /
/
/Â Read data loop
   /
/
/ printf("Number of
points ") scanf("d", n) for
(i 0 i lt n i)
printf("Enter value of valued ", i)
scanf("lf", datai)
/
/ /Â Compute the
statistics
   / /
/ max
maxArray(data, n) min minArray(data, n)
mean meanArray(data, n)
/
/ /Â Display the
results
   / /
/
printf("\n Number of items in the list d",
n) printf("\n Maximum value in the list
lf", max) printf("\n Minumum value in the
list lf", min) printf("\n Mean of the
list lf", mean) return(0) /
end of main program / /
/ /Â Function maxArray
/ /Â Â Â Â
       / / Determines the maximum value in an
array passed in as a parameter. / /
/ / Input parameters
/ /
x - the array containing list of
floating-point values. / / n -
number of elements in the array
/ /
/ /
Returns
/ / value of type
double representing the largest value in the
array. / /
/ double
maxArray(double x, int n) double max x0
/ maximum value / int i for (i 1 i lt
n i) if (max lt xi) max
xi return(max)
17/
/ /Â Function minArray
/ /Â Â Â Â
       / / Determines the
minimum value in an array passed in as a
parameter. / /
/ /
Input parameters
/ / x - the array
containing list of floating-point values.
/ / n - number of elements in the array
/ /
/ / Returns
/ / value
of type double representing the smallest value in
the array./ /
/ double
minArray(double x, int n) double min x0
/ minimum value / int i for (i 1 i lt
n i) if (min gt xi) min
xi return(min) /
/ /Â Function meanArray
/ /Â Â Â Â
       / / Determines the mean value in an
array passed in as a parameter. / /
/ / Input parameters
/ /
x - the array containing list of
floating-point values. / / n -
number of elements in the array
/ /
/ /
Returns
/ / value of type
double representing the mean value in the array.
/ /
/ double
meanArray(double x, int n) double mean
0.0 / mean value / int i for (i 0 i
lt n i) mean mean xi mean mean /
n return(mean)
18Reserved Words of C
- Reserved words have special meaning in C and
cannot be used for other purposes - they always
appear in lowercase. - Flow control (6) if, else, return, switch,
case, default - Loops (5) for, do, while, break, continue
- Common types (5) int, float, double, char, void
- structures (3) struct, typedef, union
- Counting and sizing things (2) enum, sizeof
- Rare but still useful types (7) extern, signed,
unsigned, long, short, static, const - Evil keywords which we avoid (1) goto
- Wierdies (3) auto, register, volatile
19Examples of Identifiers
- A student_name Student_Name
studentName TRUE C3PO R2D2
student_number FALSE - Invalid ones anything starting with a number or
any non-alphabetic character (except the
underscore). -
- Cannot have embedded spaces no dashes
- no special names / reserved words.
- ab4 Special word abc abc
20Some Common Programming Errors
- Using a capital letter instead of a lowercase
letter (i.e., typing Main instead of main).
Remember - C is case-sensitive. - Typing the name of the output function printf as
print in a program. - Forgetting to terminate a comment with /
- 4. Forgetting one or both of the double quotes
surrounding the format control string in a printf
or scanf. (well discuss scanf soon)
21Constants and Identifiers
- A constant (literal) is a specific value - may
be numeric (e.g., 17, 3.1415926, 4.0) or
character (e.g., a, A, 8) - Identifiers may be either standard identifiers or
user-defined identifiers. - The rules for selecting a valid identifier are
- must begin with an alphabetic character or
underscore (dont use underscores used for the
system.) - may contain only letters, digits and underscore
(no special characters) - case sensitive
- can not use keywords as identifiers
- first 31 characters of an identifier are
significant
22Identifiers
- Standard identifiers - have special meaning in C
(ex., printf and scanf are names of operations
defined in the standard I/O library). - Unlike reserved words, standard identifiers can
be redefined and used by the programmer for other
purposes - NOT recommended! If redefined, C
will no longer be able to use the identifier for
its original purpose. - User-defined identifiers - These are typically
variables in C and represent storage locations
where values are stored.