Friday 27 April 2012

Unix System


Creating the program

Once we load the UNIX operating system into the memory, the computer is ready to receive program. The program must be entered into a file. The file name can consist of letters, digits and special characters, followed by a dot and a letter c. Examples of valid file names are:

hello.c
program. c 
ebg 1 . c

The file is created with the help of a text editor, either ed or vi. The command for calling the editor and creating the file is

ed filename

 If the file existed before, it is loaded. If it does not yet exist, the file has to be created so that ready to receive the new program. Any corrections in the program are done under the editor. ITN name of your system's editor may be different. Check your system manual.)


When the editing is over, the file is saved on disk. It can then be referenced any time later by its file name. The program that is entered into the file is known as the source program3 since it represents the original form of the program.

Compiling and Linking

Let us assume that the source program has been created in a file named ebgl.c. Now the program is ready for compilation. The compilation command to achieve this task under UNIX is

cc ebgl c

The source program instructions are now translated into a form that is suitable for execution by the computer. The translation is done after examining each instruction for its correctness. If everything is
alright, the compilation proceeds silently and the translated program is stored on another file with the 'name ebgl.o. This program is known as object code.
Linking is the process of putting together other program files and functions that are required by the program. For example, if the program is using exp() function, then the object code of this function should be brought from the math library of the system and linked to the main program. Under UNIX, the linking is automatically done (if no errors are detected) when the cc command is used.
If any mistakes in the syntax and semantics of the language are discovered, they are listed out and the compilation process ends right there. The errors should be corrected in the source program with the help of the editor and the compilation is done again.
The compiled and linked program is called the executable object code and is stored automatically in another file named a.out.
Note that some systems use different compilation command for linking mathematical functions.
cc filename - I m


is the command under UNIPLUS SYSTEM V operating system.
Executing the Program
Execution is a simple task. The command
a. out


would load the executable object code into the computer memory and execute the instructions. During execution, the program may request for sonic data to be entered through the keyboard. Sometimes the program does not produce the desired results. Perhaps, something is wrong with the program logic or data. Then it would be necessary to correct the source program or the data. In case the source pro¬gram is modified, the entire process of compiling, linking and executing the program should be re¬peated.
Creating Your Own Executable File
Note that the linker always assigns the same name a.out. When we compile another program, this file will be overwritten by the executable object code of the new program. If we want to prevent from happening, we should rename the file immediately by using the command.
my a.out name


We may also achieve this by specifying an option in the cc command as follows:
cc —o name source-file


This will store the executable object code in the file name and prevent the old file a.out from being destroyed.
Multiple Source Files


To compile and link multiple source program files, we must append all the files names to the a command.
cc filename-lc filename-n.c
These files will be separately compiled into object files called
filename-i


and then linked to produce an executable program file a.out as shown in Fig..
It is also possible to compile each file separately and link them later. For example. the commands cc
-c modl.c

cc -c mod2.c
will compile the source files 'nod 1 .c and mod2.c into objects files mod], and mod2.o. They can be linked together by the command
cc modl.o mod2.o
we may also combine the source files and object files as follows:
cc modl.c mod2.o


Only mod I .c is compiled and then linked with the object file mod2.o. This approach is useful when one of the multiple source files need lobe changed and recompiled or an already existing object files is to be used along with the program to be compiled.






Sunday 22 April 2012

Executing C Program

C Programming :

Executing a program written in C involves a series of steps. These are:
1.    Creating the program
2.    Compiling the program
3.    Linking the program with functions that are needed from the C library4.    Executing the program.
Illustrates the process of creating, compiling and executing a C program. Although these steps remain the same irrespective of the operating system, system commands for implementing the steps and conventions for namingfi/es may differ on different systems.
An operating system is a program that controls the entire operation of a computer system. All input/out operations are channeled through the operating system. The operating system, which is an interface between the hardware and the user, handles the execution of user programs.
The two most popular operating systems today are UNIX (for minicomputers) and MS-DOS (for microcomputers). We shall discuss briefly the procedure to be followed in executing C program, under both these operating systems in the following sections.

Sunday 15 April 2012

C Programming Style

C Programming :

Unlike some other programming languages (COBOL. FORTRAN, etc.,) C is afree-form_language. That is, the C compiler does not care, where on the line we begin typing. While this may be a licence for bad programming, we should try to use this fact to our advantage in developing readable pro¬grams. Although several alternative styles are possible, we should select one style and use it with total consistency.
First of all, we must develop the habit of writing programs in lowercase letters. C program state¬ments are written in lowercase letters. Uppercase letters are used only for symbolic constants.
Braces group program statements together and mark the beginning and the end of functions. A proper indentation of braces and statements would make a program easier to read and debug. Note how the braces are aligned and the statements are indented in the program of Fig.
Since C is a free-form language, we can group statements together on one line. The statements


may be written in one line like
main( ) {printf("Hello C")};

However, this style make the program more difficult to understand and should not be used In this book, each statement is written on a separate line.

The generous use of comments inside a program cannot be overemphasized. Judiciously inserted comments not only increase the readability but also help to understand the program logic. This is very important for debugging and testing the program.


Wednesday 11 April 2012

C Programming - Sample - 5 - Basic Structure Of C Programs

BASIC STRUCTURE OF C PROGRAMS :

The examples discussed so far illustrate that a C program can be viewed as a group of building blocks called functions. A function is a subroutine that may include one or more statements designed to perform a specific task. To write a C program, we first create functions and then put them together. A C program may contain one or more sections shown in Fig.




The documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later. The link section provides instructions to the compiler to link functions from the system library. The definition section defines all symbolic constants.
There are sonic variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. This section also declares all the user-defined functions.

Every C program must have one main( ) function section. This section contains two parts, declara¬tion part and executable part. The declaration part declares all the variables used in the executable part. There is at least one statement in the executable part. These two parts must appear between the opening and the closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function section is the logical end of the program. All statements in the declaration and executable parts end with a semicolon.

The subprogram section contains all the user-defined functions that are called in the main function. User-defined functions are generally placed immediately after the main function, although they may appear in any order.

All sections, except the main function section may be absent when they are not required.

Friday 6 April 2012

C Programming - Sample - 4 - USE OF MATH FUNCTIONS

USE OF MATH FUNCTIONS :
We often use standard mathematical functions such as cos, sin, exp. etc. We shall see now the use of a mathematical function in a program. The standard mathematical functions are defined and kept as a part of C math library. If we want to use any of these mathematical functions, we must add an #include instruction in the program. Like #define, it is also a compiler directive that instructs the compiler to link the specified mathematical functions from the library. The instruction is of the form

#include <math.h>

math.h is the filename containing the required function. Figure illustrates the use of cosine func¬tion. The program calculates cosine values for angles 0, 10, 20 180 and prints out the results with headings.

Another #include instruction that is often required is

//include <stdio.h>

stdio.h refers to the standard 1/0 header file containing standard input and output functions




As mentioned earlier, C programs are divided into modules or functions. Some functions are written by users like us and many others are stored in the C library. Library functions are grouped category-wise and stored in different files known as header files. If we want to access the functions stored in the library, it is necessary to tell the compiler about the files to be accessed.

This is achieved by using the preprocessor directive #include as follows: 

#include < filename >

filename is the name of the library file that contains the required function defini¬tion. Preprocessor directives are placed at the beginning of a program. '