Professional Documents
Culture Documents
3
CSC 2II (COMPUTER PROGRAMMING)
Aim
After working through this module you should be able to create, compile and run a
simple C program.
Learning objectives
After working through this module you should be able to:
1. Identify the structural elements and layout of C source code.
2. Recall the rules for constructing C identifiers and using data types.
3. Use assignment statements and operators to store and manipulate data in a C
program.
4. Identify and apply the C control structures.
5. Describe the techniques for creating program modules in C.
6. Create C functions.
7. Make use of local and global parameters in functions.
8. Create recursive functions in C.
9. Use define and macro statements to simplify code development.
Content
1.
2.
3.
4.
5.
6.
Learning Strategy
Read the printed module and the assigned readings and complete the exercises as
requested.
A C program
The best way to get started with C is to look at a program. Consider the
sample program:
void main()
{
}
The word main is very important, and must appear once (and only once) in
every C program. This is the point where execution is begun when the
program is run. It does not have to be the first statement in the program but
it must exist as the entry point. The use of the term main is actually a
function definition. Functions must return a value and the term void is used
to denote that the main program does not return a value. Following the
main program name is a pair of parentheses, which are an indication to the
compiler that this is a function. As you will see, all C programs consist
essentially of a collection of functions. The two braces are used to define the
limits of the program itself: in this case, the main function. The actual
program statements go between the two braces, and in this case there are
no statements because the program does absolutely nothing. You can
compile and run this program, but since it has no executable statements, it
does nothing. Keep in mind, however, that it is a valid C program.
Exercise 1
Write a program to display your name on the monitor.
Modify the program to display your address and phone number on separate
lines by adding two additional "printf" statements.
Objective 2: After working through this module you should be able to recall
the
rules for constructing C identifiers and using data types.
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. Two rules must be kept in mind when
naming identifiers:
1. The case of alphabetic characters is significant. Using INDEX for a variable
is not the same as using index and neither of them is the same as using
InDeX for a variable. All three refer to different variables.
2. Up to 31 characters can be used and will be considered significant. If more
than 31 are used, they will be ignored by the compiler. You can reduce the
maximum number used to something less than 31 if you desire via a
compiler option. You should not do this for the duration of your study of this
tutorial as you could get some odd compiler diagnostics.
Using underlines
Even though the underline can be used as part of a variable name, it seems
not to be used very much by experienced C programmers. It adds greatly to
the readability of a program to use descriptive names for variables and it
would be to your advantage to do so. Pascal programmers tend to use long
descriptive names, but most C programmers tend to use short cryptic
#include <stdio.h>
main()
{
int index;
index = 13;
printf("The value of the index is %d\n",index);
index = 27;
printf("The value of the index is %d\n",index);
index = 10;
printf("The value of the index is %d\n",index);
return(0);
}
The first new thing we encounter is the line containing int index, which is
used to define an integer variable named index. The int is a reserved word in
C, and can therefore not be used for anything else. It defines a variable that
Variable types
The next program [LOTTYPES.C] contains every standard simple data type
available in the programming language C. There are other types, notably the
compound types (i.e. arrays and structures) that we will cover in due time.
#include <stdio.h">
void main()
{
int a; /* simple integer type */
long int b; /* long integer type */
short int c; /* short integer type */
unsigned int d; /* unsigned integer type */
char e; /* character type */
float f; /* floating point type */
double g; /* double precision floating point */
a = 1023;
b = 2222;
c = 123;
d = 1234;
e = 'X';
f = 3.14159;
g = 3.1415926535898;
printf("a = %d\n",a); /* decimal output */
printf("a = %o\n",a); /* octal output */
printf("a = %x\n",a); /* hexadecimal output */
Exercises 2
Objective 3: After working through this module you should be able to use
assignment statements and operators to store and manipulate data in a
C program.
Assignment statements
Lets move on to examine assignment statements. In the first program three
variables are defined for use, and the rest of the program is merely a series
of illustrations of various assignments.
/* This program illustrates the assignment statement */
#include <stdko.h>
void main()
{
int a,b,c; /* Integer variables for examples */
/* data assignment statements */
a = 12;
b = 3;
/* data manipulation statements */
c = a + b; /* simple addition */
c = a - b; /* simple subtraction */
c = a * b; /* simple multiplication */
c = a / b; /* simple division */
c = a % b; /* simple modulo (remainder) */
c = 12*a + b/2 - a*b*2/(a*c + b*2);
c = c/4+13*(a + b)/3 - a*b + 2*a*a;
a = a + 1; /* incrementing a variable */
b = b * 5;
a = b = c = 20; /* multiple assignment */
a = b = c = 12*13/4;
return(0);
}
The data assignment statements assign numerical values to a and b, and the
next four lines illustrate the five basic arithmetic functions and how to use
them. The fifth is the modulo operator and gives the remainder if the two
variables were divided. It can only be applied to int or char type variables,
Arithmetic Operators
The basic arithmetic operators have been introduced in the previous section.
Addition (+), subtraction (-), multiplication (*) and division (/) behave in
much the same way as other languages. Modular division is achieved using
the % operator. The result of an arithmetic operation is determined by the
data type of the receiving variable, rather than the data types of the
operands. For example, in the following statement:
x = 12.678 * 13;
the result of the operation will be 164.814 if x is defined as a float, and 164 if
x is defined as an int. Note that the type casting occurs after the calculation
has been performed. There are three constructs used in C that make no
sense at all when first encountered because they are not intuitive, but they