26-09-2013, 12:36 PM
Fundamental Programming Structures in C
Fundamental Programming .pdf (Size: 170.26 KB / Downloads: 72)
Introduction to Module 813
In this module you will be introduced to the fundamental structures
used to build a C program. In the previous module (Module 812:
Program Development in C) you discovered the general structure of a
C program and identified the various components needed to compile the
source code into an executable file. This module describes the essential
program elements that are common to all C programs. Future modules
will discuss specific concepts that are needed by some programs to deal
with more complex problems.
The concepts presented in this module provide the basic elements
needed to construct a working program. In completing this, and
subsequent modules, you should make use of the knowledge gained in
the modules of previous units. ASNS1101 (Introduction to Computing
and Numerical Processes) provided a general introduction to computer
systems and application software. ASCO1102 (Data Structures and
Language Design) introduced the class of computer languages called
third generation languages (3GL’s), the concepts of data and file
structures, and completed the study of the Pascal programming
language. ASCO2202 (Language Design and Computer Hardware)
developed an understanding of the architecture of machines and the
compilation process. Finally, ASCO3301 (Software Engineering)
developed some models of program design and construction,
particularly for large projects.
Identifiers
Before you can do anything in any language, you must at least know
how you name an identifier. An identifier is applied to any variable,
function, data definition, etc. In C an identifier is a combination of
alphanumeric characters, the first being a letter of the alphabet or an
underline character, and the remainder being made up of combination of
letters of the alphabet, numeric digits, or the underline.
Working with data
The basic data structure used in C programs is a number. C provides for
two types of numbers - integers and floating point. Computer
operations that involve text (characters, words or strings) still
manipulate numbers.
Each data item used by a program must have a data type. The basic
data types provided by C are shown in Table 1. Each data type
represents a different kind of number. You must choose an appropriate
type for the kind of number you need in the variable declaration section
of the program.
Data type mixing
It would be profitable at this time to discuss the way C handles the two
types char and int. Most functions in C that are designed to operate
with integer type variables will work equally well with character type
variables because they are a form of an integer variable. Those
functions, when called on to use a char type variable, will actually
promote the char data into integer data before using it.
Using data types
The first three lines of the program assign values to all nine of the
defined variables so we can manipulate some of the data between the
different types. Since, as mentioned above, a char data type is in reality
an integer data type, no special considerations need be taken to
promote a char to an int, and a char type data field can be assigned to
an “int” variable. When going the other way, there is no standard, so
you may simply get garbage if the value of the integer variable is
outside the range of the “char” type variable. It will translate correctly if
the value is within the range of -128 to 127.
The third line illustrates the simplicity of translating an integer into a
float: simply assign it the new value and the system will do the proper
conversion. When going the other way however, there is an added
complication. Since there may be a fractional part of the floating point
number, the system must decide what to do with it. By definition, it will
truncate it.