Chapter 1.6 - Recognize the programming style of C language | Part - 3


Creating Your Own Executable File

Read first the previous two part of this post Part -1 & Part - 2 after reading of this two-part, then you can be understood of the following part - 3. 



Chapter 1.6 - Recognize the programming style of C language | Part - 3

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. 
                         mv 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 File

To compile and link multiple source program files, we must append all the file names to the cc command.

                        ccfilename - 1.cfilename-n.c                
These files will be separately compiled into object files called
                                     filename-i.o
and then linked to produce an executable program file a.out as shown in Fig. 11

program executing steps
Fig. 11. Compilation of multiple files
It is also possible to compile each file separately and link them later. For example, the commands
                                cc -c mod1.c                                cc -c mod2.c
will compile the source file mod1.c and mod2.c into object files mod1.o and mod2.o. They can be linked together by the command


                               cc mod1.o mod2.o
we may also combine the source files and object files as follows:
                               cc mod1.c mod2.o
The only mod1.c is compiled and then linked with the object file mod2.o. This approach is useful when one of the multiple source files need to be changed and recompiled or an already existing object files is to be used along with the program to be compiled. 



MS-DOS SYSTEM

The program can be created using any word processing software in non-document mode. The file name should end with the characters ".c" like the program.c, pay.c, etc. Then the command under the MS-DOS operating system would load the program stored in the file pay.c and generate the object code. This code has stored another file under name pay.obj. In case any language errors are found, the compilation is not completed. The program should then be corrected and compiled again.


This linking is done by the command
                                          LINK pay.obj
which generates the executable code with the filename pay.exe. Now the command
                                                   pay
would execute the program and given the result. # allaboutprogramming62

Chapter 1.6 - Recognize the programming style of C language | Part - 2


Executing the C Program

Read first the previous part of this post PART -1. 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 some data to be entered through the keyboard. Sometimes the program does not produce the desired result. 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 program is modified, the entire process of compiling, linking and executing the program should be repeated. 



Recognize the programming style of C language


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.



                                                mv 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. #allaboutprogramming62

Chapter 1.6 - Recognize the programming style of C language | Part - 1



UNIX SYSTEM - Creating the program

Once we load the operating system into the memory, the computer is ready to receive the program. The program must be entered into the 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
                               ebgl.c


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




If the file existed before, it is loaded. If the files do not exist, the file has to be created so that it is ready to receive the new program. Any correction in the program is done under the editor. (The name of your system's editor may be different. Check your system manual.)


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

UNIX SYSTEM - Creating the program

Compiling and Linking 

Let us assume that the source program has been created in a file named ebg1.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 ebg1.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. 



      Solve Turbo C++ Common Issues             

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 an editor and the compilation is done again. 


The compiled and the linked program is called the executable object code and it stored automatically in another file name a.out.
Note that some system uses different compilation command for linking mathematical functions. 
                               
                           cc filename - 1m

is the command under UNIPLUS SYSTEM V operating system. #allaboutprogramming62

Chapter 1.5 - Recognize the programming style of C language | Part - 2

EXECUTING A 'C' PROGRAM

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 library; and
  4. Executing the program.
Chapter 1 - History of C language                                  


Recognize the programming style of C language


Fig. 10. illustrate 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 conversion of naming files may differ on a different system.


      Turbo C++ Program Compiler Download                                

An operating system is a program that controls the entire operation of a computer system. All input/output operation is channeled through the operating system. The operating system which is an interface between the hardware and the user handles the execution of user programs. 

EXECUTING A 'C' PROGRAM
Fig. 10 Process of compiling & running a C program

          Fix common software Issues 


The two most popular operating system today is UNIX (for minicomputers) and MS-DOS (for microcomputers). We shall discuss briefly the procedure to be followed in executing a C program under both these operating system in the following sections in our next post. #allaboutprogramming62 

Chapter 1.5 - Recognize the Programming style of C language | Part -1

PROGRAMMING STYLE

Unlike some other programming languages (COBOL, FORTRAN, etc.,) C is a free-form_language. That is, the C compiler does not care, where on the line we begin typing. While this may be a license for bad programming. we should try to use this fact to our advantages in developing readable programs. although several alternatives styles are possible, we should select one style and use it with total consistency. 



      
Programming style of C language #allaboutprogramming62


First of all, we must develop the habit of writing a program in lowercase letters. C program statements are written in lowercase letters. Uppercase letters are used for an only symbolic constant.


Download Programming E-BOOK [pdf]                             

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 independent in the program of Fig. 5.

      a = b;              
            x = y + 1;              
            z = a + x;              

Can be written one line as

                                            a = b; x = y + 1;  z = a + x;

The program 

                                                               main()

                                                       {
                                                              printf("hello C");
                                                        }
May be written in one line like

                                             main() { printf ("Hello C"); }

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

    Download Turbo C++ Program Compiler                   


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.   #allaboutprogramming62

Chapter 1.4 - Describe the basic structure of C program



BASIC STRUCTURES 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 subroutine that may include one or more statements designed to perform a specific task.




 explain basic structure of C program

To write a C program, we first create functions and then put them together. A C program may contain one or more sections as shown in Fig. 9.



Describe the basic structure of C program #allaboutprogramming62
Fig. 9 An overview of a C program


Documentation Section  - 

The documentation section consists of a set of comment lines giving the name o program, the author and other details, which the programmer would like to use later.
Link Section  - 

The link section provides instructions to the compiler to link function from the system library. 

Definition Section  - 

The definition section defines all symbolic constants. 

Global Declaration Section  - 

There are some 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 functions. This section also declares all the user-defined functions.

main() Function Section  - 

Every C programs must have one main() function section. This section contains two parts, declaration part, an executable part. The declaration part declares all the variable used in the executable part, There is at least one statement in executable part. These two parts must appear between the opening and the closing braces. The program executation brgins at the opening brace and ends at the closing brace. The closing brace of the main fumction section is the logical end of progtam. All statements in the declaration and executable parts end with a semicolon (;).  



Subprogram Section  - 

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


Note - All Sections, expect the main function section may be absent when they are not required.

Chapter 1.3 - Illustrate the use of user-defined functions and math functions through sample programs | Part - 2



SAMPE PROGRAM 5 - USE OF MATH FUNCTIONS

We often use standard mathematical functions such as cos, sin, exp, etc. We shall see how the use of a mathematical function in the program. The standard mathematical functions are defined and kept as a part of the C math library.

    Chapter 1 - History Of C Programming Language

    Download Let us 'C' [pdf] E-BOOK


use of  math functions through sample programs  #allaboutprogramming62


If we want to use any of these mathematical functions, we must add an #include instructions 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. Fig. 8 illustrates the use of the cosine function. The program calculates cosine values for angles 0, 10, 20.....180 and prints out the result with headings. 

The program is using math functions
Fig. 8 Program using a math function

Output 
The program is using math functions
Fig. 8 Program using a math function

Another #include instruction that is often required is
#include<stdio.h>
stdio.h refers to the standard O/P input and output functions.


                     Download Turbo C++ Program Compiler



The #include Directive

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 groped by 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<file name>
the filename is the same of the library file that contains the required function definition. Preprocessor directives are placed at the beginning of a program #allaboutprogramming62.

Chapter 1.3 - Illustrate the use of user-defined functions and math functions through sample programs | Part - 1



SAMPLE PROGRAM 4: USE OF SUBROUTINES

So far, we have used only printf function that has been provided for us by the C system. The program is shown in Fig. 7 uses a user-defined function. A function defined by the user is equivalent to a subroutine in FORTRAN or subprogram in BASIC. 

      Chapter 1 - History of C Programming Language

      Chapter 1.1 - Produce an overview of C Programming  


Chapter 1.3 - Illustrate the use of user-defined functions and math functions through sample programs | Part - 1

Fig. 7 present a very simple program that uses a mul() function. The program will print the following output. 

           Download Let us C E-BOOK | Yashwant Kanetkar 

           Download Turbo C++ Program Compiler 

Multiplication of 5 and 10 is 50

a program using a user-defined function
Fig. 7 A program using a user-defined function

The mul() function multiplies the value of x & y and the result is returned to the main() function when it is called in the statement
                                                    c = mul(a,b);

Chapter 1.2 - Exemplify the elementary C concept through sample Programs

PART - 1
PART - 2
PART - 3


The mul() has two arguments x and y that are declared as an integer. The values of a and b are passed on to x and y respectively when the function mul() is called. User-defined functions are considered in detail in another our post #allaboutprogramming62.

Chapter 1.2 - Exemplify the elementary C concepts through sample programs | Part 2


SAMPLE PROGRAM 2: ADDING TWO NUMBERS

                 Chapter 1 - History of C Programming


C concepts through sample programs


Consider another program. Which performs addition on two numbers and displays the result. The complete program is shown above fig.

Addition of two numbers sample program
Fig 4 - Program to add two numbers 
This program when executed will produce the following output. 
                                    100
                                    106.10
The first 4 & 5 lines of the program are comment lines. It is good practice to use comment lines in the beginning to give information such as the name of the program, author, date, etc. Comment characters are also used in other lines to indicate line numbers. 




The words number and amount are variable names that are used to store numeric data. The numeric data may be either in an integer form or in real form. In C, all variables should be declared to tell the compiler what the variables name are and what type of data they hold. The variable must be declared before they are used. In lines 10and 11, the declarations. 
                                     int number;                                                                                                                  float amount;

tell the compiler that number is an integer (int) and the amount is floating (float) point number. Declaration statement must appear at the beginning of function as shown in Fig - 4. All Declaration statements end with a semicolon; C supports many other data types and they are discussed in detail in our Next Chapter. 
The words such as int and float are called the keyword and cannot be used as variable names. 


Data is stored in a variable by assigning a data value to it. This is done in lines 14 & 16. In line-14, an integer value 100 is assigned to the integer variable number and in line-16, the result of adding two real numbers 30.75 and 75.35 is assigned to the floating point variable amount. The statements 
                                                number = 100;                                                                                                             amount = 30.75 + 75.35;
are called the assignment statements. Every assignment statement must have a semicolon at the end. 
The next statement is an output statement that prints the value of a number. The print statement
                                   printf("%d\n", numbers);  


contains two arguments. The first argument "%d" tells the compiler that the value of the second argument number should be printed as a decimal integer. Note that these arguments are separated by a comma. 
The newline character \n causes the next output to appear on a new line.
The last statement of the program

                                     printf("5.2f", amount);
prints out the value of the amount in floating point format. The format specification %5.2f tells the compiler that the output must be in floating point, with five places in all and two places to the right of the decimal point.

Comments System

blogger/disqus/facebook