You are on page 1of 19

20th December, 2017

C Program Working (Basic Steps)

Binayak Bhandari, Ph.D.


Assistant Professor
Department of Railroad Integrated System Engineering
Woosong University,
Daejeon 300-718,
Korea
Lecture Overview
 In this lecture we are going to see all the steps involved from writing a
C program to generating an executable file.

C Program Executable File

Department of Railroad Integrated System


Woosong University 2
 C is a general purpose programming language and by using the C
programming language, we can write the instructions or the compute
programs.
 Instructions written in any programming language (in human readable
format) is called as the source code.
 When we write a compute program, we are going to store that in the
computer memory as the computer file, and the file which contains this
source code is called as the source file (Source code written in C
programming is called C source file.

C source
code

Department of Railroad Integrated System


Woosong University 3
 To write the instructions or to write the code we are going to
use the text editors (텍스트 에디터).
 You can use any text editor available to write C program.
 C programming language provides a lot of built-in features to
implement the common operations that we have to do in the
program. E.g., To perform the input and output operations or
to work on string values.
 Programmers can just use that built-in features and can write
the programs.
 These features are provided in the form of C standard library.
C standard library contains all the built-in features that will
perform a common operation that a programmer has to
Department of Railroad Integrated System
perform
Woosong in his program.
University 4
 We are going to write the C program using a text editor (or any
Integrated Development Environment (IDE)) and save it in the
computer memory as a computer file.

Department of Railroad Integrated System


Woosong University 5
How to differentiate C programming file?
 For example: if we have a text file then that text file will have .txt
extension.
 So by looking at this .txt extension we can say it is text file.
 Music files might have extension like .mp3, .wav, .wma etc.
 Portable Document Format (pdf) files have .pdf format
 Similarly C source code will have .c extension.
e.g., hello.c; myFirstProgram.c etc
 .c will indicate that it is a file containing the program written in the C
programming language.

Department of Railroad Integrated System


Woosong University 6
 After writing the C program using the C language, it is not possible to
directly execute that code in a computer. (why?)
 Because the computers can only understand the code which is written
in the binary language or the machine code.
 That is why, we need to convert the source code (written in C language)
to machine code.
 It can be performed by compilation process.

C source
code Compilation Object code

 Machine code generated by this compilation process is also called as


the object code.
Department of Railroad Integrated System
Woosong University 7
 In the compilation process, the source code written in the C
programming language will be compiled to generate a new file
containing the machine code representation of the source code.
 File containing object code is also called object file.
 Object file containing the object code will have .obj extension (in
windows) .o extension (in Linux or Unix environment)
 During the compilation process, several programs are used.
 Preprocessor (This is the 1st stage, before the actual compilation process, your C
source code will be processed, such as removing the new lines, spaces, comments
that you have in source code)
 Compiler (The source code produced by the preprocessor will be given to
compiler. For c programming language, we have to use C compiler. The C
compiler will generate the machine code from the source code given to it.)
Department of Railroad Integrated System
Woosong University 8
C compilers
 There are various C compliers available.
 They perform the same thing , i.e. converting the C source code to the
machine code.
 Some compliers does this a bit differently, some of the compilers they
take the C source code and then they generate the Assembly code and
converted to machine code.
 Some C compilers generate the machine code directly from the source
code given to them.

Department of Railroad Integrated System


Woosong University 9
Managing big C projects
 You can divide your program in separate files.
 For example: Let’s say you are writing a project using the C programing
language and your contains more than 10000 lines of C programming
language instructions in one single file, then it will be difficult for you
to manage your project. Instead, you can make several different parts
and also into several different files then it will be easy for your to
manage the code.
 So while writing the programs in the C programming language you can
divide your project into several different files. For example. One.c.
two.c, three.c etc.
 You can have any number of files which is containing the C source code.
If your project contains more than one file containing your programs
then you have to individually compile each of them and generate the
Department of Railroad Integrated System
Woosong University
object code. 10
 So you have to perform the compilation process on
 Compile 1.c  generate object code for 1.c
 Compile 2. c  generate object code for 2.c etc.
 So for each file which you have in your project, you have to preform
the compilation process and you have to generate the object code for
the respective source code. E.g 1.obj, 2.obj, and 3.obj
 During the compilation process, compilers may detect some errors in
your programs, in such case, the compilers will generate the errors
instead of creating the object file.
 In such case you have to go back to your source code and you have to
solve those errors. Compilation
Preprocessor
C source No Error
code Object code
C compiler
Department of Railroad Integrated System Error
Woosong University 11
 Once you get your programs error free, then the object files will be
generated.
 Once we have the object files containing the machine code for our
project, we need to link all these files together and we have to
generate an executable. C library
1.obj Printf
.

2.obj
Linker
.
.
.

n.obj

For that purpose, a program is used called as the linker, which will take
the object files, and then it links them together and generates
Department of Railroad Integrated System
executable
Woosong Universityfile. 12
 In the windows environment executable file has .exe extension
 Linker will link the features that you have used from the C standard
library.
 If your project is containing 1 file, then it will take that 1 file and then if
you have used any library functions in your program then it links them
together and produces the executables.
 Once we have executable file, we can execute it in a computer.
 During linking process also, the linker might find out that your program
is containing some errors and generate errors (and it can’t link the files).
 We have to go back to our source code and fix the errors in the source
code.
 Preform all the steps again Generate executable.
Department of Railroad Integrated System
Woosong University 13
C source
code

Compilation
Error Preprocessor

C compiler

No Error

1.obj 2.obj n.obj

.
.
.
Linker
Error
No Error

Department of Railroad Integrated System


Woosong University 14
What after executable is generated?
 To execute the executable file, a program called loader is used.
 This loader program will take your executable and it will load that to
the primary memory i.e. Random Access Memory (RAM) and then the
processor will execute it.
 After execution, program may work or produce the result successfully
or it may not work properly. For example: Program for calculating the
area of triangle, in your source code lets say you have done a mistake
while writing the formula to calculate the area of the triangle. In that
case you will not get any error because the computer doesn’t know the
formula to calculate the area of a triangle. So there is no error but it
will not produce the proper results.
 Then you need to go back to source code and make necessary
corrections.
Department of Railroad Integrated System
Woosong University 15
C source
code

Compilation
Error Preprocessor

C compiler

No Error

1.obj 2.obj n.obj

.
.
.
Linker
Error
No Error
Not correct result
Department of Railroad Integrated System
Woosong University 16
Tool Set
 All these steps to generate executable from the C program source file
comes in the bundle.
 You have to install a bundle which contains all these software’s and
that bundle is called as tool set.
 So once you install a tool set for the C programming language, you will
get all these software (compilation, linking etc.). (However, you will not
get text editor and you can use any text editor).
 There are many tool sets available for example, GCC tool set and Turbo
C, Visual C++

Department of Railroad Integrated System


Woosong University 17
Any questions ?

Binayak Bhandari, Ph.D.


Department of Railroad Integrated System Engineering
Building W16, Room , Woosong University
#17-2 Jayang-dong, Dong-Gu, Daejeon, 300-718, Korea
E-mail: binayak@sis.ac.kr
Department of Railroad Integrated System
Woosong University 19

You might also like