Previous: 2.6 Common Errors
Up: 2 Basic Concepts
Next: 2.8 Exercises
Previous Page: 2.6 Common Errors
Next Page: 2.8 Exercises
In this chapter we have begun looking at the process of designing programs.
We have stressed the importance of a correct understanding
of the problem statement, and careful development of the algorithm
to solve the problem.
This is probably the most important, and sometimes the most difficult
part of programming.
We have also begun introducing the syntax and semantics
of the C language.
We have seen how to define the special function, main() by specifying
the function header followed by the function body, a collection
of statements surrounded by brackets, { and }.
The function body begins with variable declarations to allocate
storage space and assign names to the locations, followed by
the executable statements.
Variable declarations take the form:
_specifier> <identifier>[, <identifier>
where <type_spec> may be either int or float
for integers or floating point variables, respectively.
(We will see other type specifiers in later chapters).
We gave rules for valid <identifier>s used as variable names.
We have discussed several forms for executable statements in the language.
The simplest statement is the assignment statement:
expression>;
where (for now) <Lvalue> is a variable name and
<expression> consists of constants, variable names and operators.
We have presented some of the operators available for arithmetic
computations and given rules for how expressions are evaluated.
The assignment statement evaluates the expression on the right hand side of
the operator = and stores the result in the object referenced by the
<Lvalue>.
We pointed out the importance of variable type in expressions and
showed the cast operator for specifying type conversions within them.
- <type-specifier>) <expression>
We also described how the library function printf() can be used to
generate output from the program, as well as how information may be
read by the program at run time using the scanf() function.
We next discussed two program control constructs of the language: the
if and while statements.
The syntax for if statements is:
(<expression>) <statement> [else <statement>]
where the <expression> is evaluated and if the result is True
(non-zero) then the first <statement> (the ``then'' clause)
is executed; otherwise, the <statement> after the keyword
else (the ``else'' clause) is executed.
For a while statement, the syntax is:
( <expression> ) <statement>
where the <expression> is evaluated, and as long as it evaluates
to True, the <statement> is repeatedly executed.
In addition we discussed one of the simple compiler directives:
- #define <symbol_name> <substitution_string>
which can be used to define symbolic names to character strings
within the source code; used here for defining constants in the program.
With these basic tools of the language you should
be able to begin developing your own programs to compile, debug and
execute.
Some suggestions are provided in the Problems Section below.
In the next chapter, we will once again concentrate on the proper
methods of designing programs, and in particular modular design with
user defined functions.
Previous: 2.6 Common Errors
Up: 2 Basic Concepts
Next: 2.8 Exercises
Previous Page: 2.6 Common Errors
Next Page: 2.8 Exercises