Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Topic  Programming

3 Language

LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. Differentiate the three different types of programming languages, by
levels;
2. Explain briefly on the history of C language development; and
3. Describe the six phases that are involved in developing a program in
C environment.

 INTRODUCTION
In Topics 1 and 2, we looked at the basics of program development. We will now
continue exploring programming language in more detail.

To command a computer to do a task, instructions must be written in the


computerÊs native language, which is machine language. If you have not heard
about machine language before, then this topic will be an eye-opener for you.

Writing programs in machine language is not an easy task for the programmer.
To overcome this problem, many different programming languages have been
created, to enable instructions to be given in forms other than machine language.
These instructions need to be translated into machine language, before a
computer can run them. This topic will give an explanation on the different types
of programming languages. Next, C language and its environment will be
introduced. By the end of this topic, one example of a simple C program will be
discussed.

Copyright © Open University Malaysia (OUM)


52  TOPIC 3 PROGRAMMING LANGUAGE

3.1 PROGRAMMING LANGUAGE TYPES


Programmers write programs using programming languages. There are many
categories of programming languages. Programming languages that are most
popular and normally used are high-level languages. In this topic, we will see
different types of languages, which are: machine language, assembly language
and high-level language.

3.1.1 Machine Language


Every computer has its own machine language, that is, its native language.
Programs written in machine language are normally stated in binary numbering
system (0 and 1) or in the hexadecimal numbering system. Every machine
language instruction will state the operation to be executed, along with the
memory cell involved in that operation.

3.1.2 Assembly Language


The second level of language is assembly language. Programming in machine
language was felt to be too difficult and slow. To make programming easier,
mnemonic language that resembles the English language was created and used
to represent operations in machine language. This is the basis of assembly
language. To run a program in assembly language, a program called the
assembler is used to translate the program to machine language.

ACTIVITY 3.1

Study the example of assembly language in the book Introduction to


Computers and Information System, 2nd Ed. page 228, Maxwell
Macmillan by Szymenski, R.A and Associates. Can you understand the
language? Can you list a few words in mnemonic language?

3.1.3 High-level Language


Do you know the other factors that contributed to writing programs with
machine language and assembly language to discontinue? Programs written in
machine language or assembly language are machine-dependent. Assuming we
write a program in assembly language for a particular computer and want to run

Copyright © Open University Malaysia (OUM)


TOPIC 3 PROGRAMMING LANGUAGE  53

this program on another type of computer; it has to be written again in assembly


language for that computer. A language that is machine-independent is needed
to make it easier for programs to be transferred from one computer to another.
Many high-level languages have been created for this reason. With high-level
language, a programmer is able to write programs that are simple and clear. For
example, the following statement is written in C language (which is one of the
high-level languages) to code an example on calculating a sales amount.

amount = itemPrice + salesTax;

Compared to assembly language, high-level language resembles the natural


language more, making it easier to read and understand.

Just like assembly language, a program in high-level language needs to be


compiled into machine language before it can be executed. The process of
compilation is accomplished by the compiler program. Some high-level
languages and their uses are listed in Table 3.1 below.

Table 3.1: High-level Languages

High-Level
Explanation
Languages
C A universal language that is usually used to write system software or
system applications.
C++ An extension from C language that provides support for object-
oriented programming.
Ada Language that is developed for the Defence Department in the United
States, for its real-time systems.
Lisp For artificial intelligence applications.
Pascal For teaching programming.
Fortran For scientific and engineering applications.
COBOL For processing business data.

ACTIVITY 3.2

Before we continue reading the next topic, create a networked mind map
of the three levels of programming languages that you have just read.
Present that in your tutorial.

Copyright © Open University Malaysia (OUM)


54  TOPIC 3 PROGRAMMING LANGUAGE

SELF-CHECK 3.1

Give two advantages of writing programs in C language compared to


machine language.

3.2 C LANGUAGE
Let us take a step back to the past, to get a brief history of C language.

The C language history has links with the existence of B and BCPL programming
languages. BCPL is a language that was used to write operating systems and
compilers, which was developed by Martin Richards in the year 1967. In the
1970s, Ken Thompson developed the B language that was used in developing the
UNIX operating system at Bell Laboratories. Based on B language, Dennis Ritchie
developed the C language at Bell Laboratories in 1972. This language was then
used to develop UNIX.

The C program development can be done on many computers, as C language


does not depend on the machine or hardware. Through this portability feature,
programs that are written in C language can be transferred to any computer,
with minimal changes. Other than using important concepts from BCPL and B,
many different features like pointers, were added into C, to make it a strong
language. Another advantage of C is its wide usage and its high ability.

Due to the existence of many variations of C language, the American National


Standards Institute (ANSI) has created a standard C, which is called ANSI C.
This standard enables us to write C programs that are portable.

For more information on the history of C programming, visit the web site
http://syazwan.tripod.com/introc.htm or any other relevant sites.

3.3 C PROGRAMMING ENVIRONMENT


Generally, the C programming system is composed of its environment, its
language and standard library:

(a) Environment ă is related to the process it goes through, to develop a C


program that can be executed.

Copyright © Open University Malaysia (OUM)


TOPIC 3 PROGRAMMING LANGUAGE  55

(b) Language ă refers to the aspect of syntax or the representation of algorithms


in C language.

(c) C standard Library ă makes available the supporting functions that can be
reused in program development. Among functions that are available in the
standard library are: mathematical functions like sin( ) and sqrt( ), functions
to do input and output, graphical functions and others.

Figure 3.1 on the next page shows a C development environment. It shows the six
phases involved in the process of developing a C program, which are:

(a) Editing;
(b) Pre-processing;
(c) Compiling;
(d) Linking;
(e) Loading; and
(f) Executing.

Further explanation on each phase will provide an indication on the


development of programs in the MS-DOS environment. For Windows operating
systems, the environment can be created through the MS-DOS prompt icon.

Copyright © Open University Malaysia (OUM)


56  TOPIC 3 PROGRAMMING LANGUAGE

Figure 3.1: C development environment

SELF-CHECK 3.2

Identify a software that you can use to edit and compile programs.

Copyright © Open University Malaysia (OUM)


TOPIC 3 PROGRAMMING LANGUAGE  57

3.4 SAMPLE C PROGRAM

Program 3.1

/* This program will print a greeting


Welcome to OUM */ A

#include <stdio.h> B

void main(void) {
printf(“Welcome to OUM!\n”); C
}

In this section, we will look at an example of a simple C program (see Program


3.1). This program will display the following output when executed:

Welcome to OUM

The statement that is marked as A in Program 3.1 is a comment to explain the


programÊs meaning. Comments are any text that is surrounded by the symbols /*
and */. Comments are inserted to give information or explanation on the
program code. A program can contain as many comments as needed and can be
put anywhere in the program code. Comments are not compulsory in a program,
but we are encouraged to include comments so that programs can be easily read
and understood.

Statements that are marked as B in Program 3.1 are said to be pre-processor


statements. Pre-processor statements are easily recognised because they start with
the symbol #. Here, pre-processor statements used are the #include statement.
When executed, the pre-processor will insert a copy of a header file called stdio.h
into the program code, at the given statement line. This statement is needed in
Program 3.1 because the header file has the function declaration printf(). We
will see later how the program will use the function to output something.

Notice however that the file name stdio.h in the pre-processor statement is
surrounded by the symbol < and >. This is actually to let the pre-processor know
that the file is found in the usual place. This usual place is dependent on the
computerÊs environment. For example, in the UNIX environment, the normal place
is stated in the file .profile. In the DOS mode, it is normally stated in the file
autoexec.bat.

Copyright © Open University Malaysia (OUM)


58  TOPIC 3 PROGRAMMING LANGUAGE

The code that is marked as C in Program 3.1 is a function definition. The function
that is defined in the program is called main() function. The main() function is
a special function that is the starting point in the execution of any C program.

Notice that the function definition consists of two parts. The first part is the
function head that provides specific information on the function defined; for
example, function name. The second part is the function body which consists of
statements enclosed in { and }. When a function is called, the statements within
the body will be executed. In the case of main() function in the program 3.1, the
body only contains one statement which is:

printf(“Welcome to OUM!\n”);

This statement is the calling of the function printf(), which is one of the
functions that is available in the C standard library functions. We can call this
function printf() if we need to output a string on the computer screen. In the
statement above, the string “Welcome to OUM!\n” is passed to the printf()
function to be output.

The function will display every character in the string, except the enclosing
double quotes (which is the character „) and the control character \n at the end of
the string. The double quotes are the markers for the start and end of the string.

The control character \n is called the new line character. When this character is
found in the printf() function, it will move the cursor to the beginning of the
next line. The new line character is one of the many control characters in the
printf() function. Every control character will have a specific meaning in the
functions given.

SELF-CHECK 3.3

In Section 3.4, there are symbols that were used in Program 3.1. Can you
explain them briefly?

Symbols Explanation
/*.........*/
#include
{.........}
“\n”

Copyright © Open University Malaysia (OUM)


TOPIC 3 PROGRAMMING LANGUAGE  59

ACTIVITY 3.3

1. Write/edit program 3.1 using a notepad and save the file with the
name atur.c.

2. Compile the program.

3. Execute the program.

4. Compare the output.

5. Edit the program by changing the statement printf („Open


University Malaysia‰);

6. Edit the program 3.1 so that the output below is displayed:

WELL
WELCOME
WELCOME TO
WELCOME TO OUM!

 Machine language is the computerÊs natural language. Programming in


machine language is in the binary format.

 Assembly language is a mnemonic language and is closer to the English


language in its representation of operations and operands.

 High-level language is closer to natural language.

 A program written in assembly language or high-level language has to be


translated into machine language, before it can be executed.

 C Language is a high-level language.

 In the C environment, a program has to go through the following phases in


its development process: editing, pre-processing, compiling, linking, loading
and executing.

Copyright © Open University Malaysia (OUM)


60  TOPIC 3 PROGRAMMING LANGUAGE

C Language Environment
Assembly language Linker
C standard library Loader
Compiler Machine language
Editor

Copyright © Open University Malaysia (OUM)

You might also like