0% found this document useful (0 votes)
14 views

Chapter 5 Preparing and Running a Complete C Program

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Chapter 5 Preparing and Running a Complete C Program

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Preparing and Running a Complete C Program

Objectives
 To plan, write and execute of a complete C
program.
 To detect and correct the different types of
errors that can occur in improperly written
programs.
PLANNING A C PROGRAM
 It is essential that the overall program strategy be
completely mapped out before any of the detailed
programming actually begins.
 Concentrate on the general program logic, without
being concerned with the syntactic details of the
actual instructions.
 Once the overall program strategy has been clearly
established, the details associated with the
individual program statements can be considered.
 This approach is generally referred to as “top-down”
programming.
 The resulting outline is usually referred to as
pseudocode.
Detailed version of the outline
Bottom-UP
 Another method that is sometimes used when planning a C
program is the "bottom-up" approach.
 This method may be useful for programs that make use of
self-contained program modules (e.g., user-defined
functions).
 The bottom-up approach involves the detailed development
of these program modules early in the planning process.
 The overall program development is then based upon the
known characteristics of these available program modules.
 In practice we often use both approaches: top-down for the
overall program planning, bottom-up in developing
individual modules before the main part of the program, and
top-down with respect to the development of each individual
module.
WRITING A C PROGRAM
 Once an overall program strategy has been
formulated and a program outline has been
written, attention canbe given to the detailed
development of a working C program.
 At this point the emphasis becomes one of
translating each step of the program outline
(or each portion of the pseudocode) into one
or more equivalent C instructions.
 This should be a straightforward activity
provided the overall program strategy has
been thought through carefully and in enough
detail.
Contd…
 Attention should also be given to including
certain additional features that will improve
the readability of the program and its
resulting output.
 These features include the logical sequencing
of the statements, the use of indentation and
whitespace, the inclusion of comments and
the generation of clearly labeled output.
A well written program in C

 Compound Interest: Let us now consider an


interactive C program c orresponding to the outline
presented in previous example.
ERROR DIAGNOSTICS
 programming errors often remain undetected
until an attempt is made to compile or
execute the program.
 The presence of syntactic (or grammatical)
errors will become readily apparent once the
Run command has been issued, since these
errors will prevent the program from being
compiled or executed successfully.
 Some particularly common errors of this type
are improperly declared variables, a reference
to an undeclared variable, incorrect
punctuation, etc.
Contd…
 Most C compilers will generate diagnostic
messages when syntactic errors have been
detected during the compilation process.
 These diagnostic messages are not always
straightforward in their meaning and they
may not correctly identify where the error
occurred (though they may attempt to do so).
 Nevertheless, they are helpful in identifying
the nature and the approximate location of
the errors.
Syntactic Errors
Error Messages
Execution error
 Execution errors occur during program execution,
after a successful compilation.
 For example, some common execution errors are a
numerical overflow or underflow (exceeding the
largest or smallest permissible number that can be
stored in the computer), division by zero, attempting
to compute the logarithm or the square root of a
negative number, etc.
 Diagnostic messages will often be generated in
situations of this type, making it easy to identify and
correct the errors.
 These diagnostics are sometimes called execution
messages or run-time messages, to distinguish them
from the compilation messages described earlier.
Example
 Some notable example of execution error
messages would be:
 For square root of a negative number- sqrt :
DOMAIN error or NAN
 Similarly, suppose the program is run with the
input values a=IE-30 b=l El0 c=lE36
 The system now generates the error message

Floating Point : Overflow


DEBUGGING TECHNIQUES
 Syntactic errors are relatively easy to find and
correct, even if the resulting error messages
are unclear.
 Execution errors, on the other hand, can be
much more troublesome.
 When an execution error occurs, we must first
determine its location (where it occurs) within
the program.
 Once the location of the execution error has
been identified, the source of the error (why it
occurs) must be determined.
 Knowing where the error occurred often
assists, however, in recognizing and
Contd…
 Closely related to execution errors are logical errors.
 Logical errors can be very difficult to detect, since the
output resulting from a logically incorrect program may
appear to be error-free.
 Moreover, logical errors are often hard to locate even
when they are known to exist (as, for example, when the
computed results are obviously incorrect).
 Fortunately, methods are available for finding the
location of execution errors and logical errors within a
program.
 Such methods are generally referred to as debugging
techniques.
 Some of the more commonly used debugging
techniques are described in next slides.
Debugging Techniques
 Error Isolation
 Tracing
 Interactive debugging – watch values and
breakpoints, and allows stepping through a
program one instruction at a time.
Error Isolation
 Error isolation is useful for locating an error resulting in a diagnostic message.
 If the general location of the error is not known, it can frequently be found by
temporarily deleting a portion of the program and then rerunning the program
to see if the error disappears.
 The temporary deletion is accomplished by surrounding the instructions with
comment markers (/ * and * /), causing the enclosed instructions to become
comments.
 If the error message then disappears, the deleted portion of the program
contains the source of the error.
 A closely related technique is that of inserting several unique printf
statements, such as
printf ("Debugging - line1\ n " ) ;
printf ("Debugging - line2 \ n " ) ;
etc. at various places within the program.
 When the program is executed, the debug messages will indicate the
approximate location of the error.
 Thus, the source of the error will lie somewhere between the last printf
statement whose message did appear, and the first printf statement whose
message did not appear.
Tracing
 Tracing involves the use of printf statements to display the
values assigned to certain key variables, or to display the
values that are calculated internally at various locations
within the program.
 This information serves several purposes. For example, it
verifies that the values actually assigned to certain
variables really are (or are not) the values that should be
assigned to those values.
 It is not uncommon to find that the actual assigned values
are different than those expected.
 In addition, this information allows you to monitor the
progress of the computation as the program executes.
 In many situations, you will be able to identify a particular
place where things begin to go wrong because the values
generated will be obviously incorrect.
Contd…
 We saw that the program generates the
execution error in previous example.
 Floating Point: Overflow
 when it was executed with the input values a
= 1E- 30, b = 1E l0 and c = 1E36.
 Let us now apply error isolation and
tracing techniques to determine the source of
the error.
Contd…
 We now remove the comment markers, but precede each assignment
statement with a printf statement, as shown below.
Cond…
Tracing Example
Watch Values, Breakpoint and Stepping
 Most contemporary C compilers include an
interactive debugger, which provides the
ability to set watch values and breakpoints,
and allows stepping through a program one
instruction at a time.
 Watch values are usually used with breakpoints
or with stepping to provide detailed monitoring of
the program as it executes.
 The use of these features offers greater flexibility
and convenience than the simple error isolation and
tracing techniques described previously.
 Each of these features is described in more detail in
next slides.
Watch Values
 A watch value is the value of a variable
or an expression which is displayed
continuously as the program executes.
 Thus, you can see the changes in a watch
value as they occur, in response to the
program logic.
 By monitoring a few carefully selected watch
values, you can often determine where the
program begins to generate incorrect or
unexpected values.
Breakpoints
 A breakpoint is a temporary stopping
point within a program.
 Each breakpoint is associated with a
particular instruction within the program.
 When the program is executed, the program
execution will temporarily stop at the
breakpoint, before the instruction is
executed.
Stepping
 Stepping refers to the execution of one
instruction at a time, typically by pressing a
fbnction key to execute each instruction.
 By stepping through an entire program, you
can determine which instructions produce
erroneous results or generate error messages.
 Stepping is often used with watch values,
allowing you to trace the entire history of a
program as it executes.
 Thus, you can observe changes to watch
values as they happen. This allows you to
determine which instructions generate
erroneous results.
Debugging with an Interactive
Debugger
 Consider the solution of a quadratic equation-
 Set the watch values: -b+d, -b-d and 2*a.
 Where d=
 Set the breakpoint to a particular instruction.
 Now stepping into an instruction at a time.
 Observe the watch values.
Thank You!!!

You might also like