Welcome to the new chapter on the C programming language. In the previous videos we have learnt about the computers and the C programming language and in this post we are going to see all the steps involved from writing a C program to generating an executable file from that C program that is the working of C programming language.
Write First Program in C Language
Computer Programming class, Chapter 1 for Beginners
Different types of Programming Languages
Working of C programming language
Contents
- 1 Working of C programming language
- 1.1 Source Code and C Source File
- 1.2 What to do Before writing the instructions?
- 1.3 C standard library
- 1.4 How to differentiate C programming language file from other file?
- 1.5 Why we need to convert the source code?
- 1.6 How machine language is generated?
- 1.7 C Compilers
- 1.8 Avalibale Tool sets in software
- 1.9 The key point for Absolute Beginners in Programming :
Working of C programming language
Source Code and C Source File
The first thing is C is a general purpose programming language and by using the C programming language, we can write the instructions or the computer programs. The instructions written in any programming language in human readable format is called as the source code and here the instructions written in the C programming language is called as the C source code. When we write a computer program, we are going to store that in the Computer memory as the computer file and the file which is containing this source code is called as the source file. If the file is containing the source code written in the C programming language. Then that file is also called as the C source file.
What to do Before writing the instructions?
Before we going to write the instruction using the C programming language. We are going to save that in the Computer memory. To write the instructions or to write the code we are going to use the text editors. you can use any text editor available and you can write the C program and you can save that as a computer file.
C standard library
The C programming language provides a lot of built-in features to implement the common operations that we have to do in the program. For example in programming, someone has to frequently perform the input and output operations or programmers have to work on the string values. All this common operations the C programming language provides the built-in features. So the programmer can just use that built-in features and then he can write the programs and these features are provided in the form of C standard library.
This C standard library contains all the built-in features that will perform the common operations that a programmer have to perform in his program. So while writing the C program you are going to use that built in features and then you will write the program and we will talk more about this when we write the program and just
now remember that C programming language provides a lot of built-in features that we can use in our program and we can easily write the programs.
How to differentiate C programming language file from other file?
How we can differentiate a file which is containing the C programming language from any other 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 that that file is a text file. similarly for the music files which have the MP3 format will have .MP3 extensions and similarly
the PDF files have .PDF extension. so similar to all these files, the file which is containing the c source code will be saved with dot C extension. Here you can give any name to your file while saving it but you have to make sure that it has the extension dot C. For example if I write a program using C programming language by using a text editor and then whilesaving that file I can give any name. for example I can name it one. but it should have the extension dot C. so my file has to be saved as one.c This .C will indicate that it is a file containing the program written in the C programming language.
Why we need to convert the source code?
Once we write the C program using the C language you know we can’t directly execute that code in a computer.
That’s because the computers they can only understand the code which is written in the binary language or the machine code. and that’s why we need to convert the source code that we have written in the C language to machine code and we can do that by performing the compilation process.
How machine language is generated?
So we will perform the compilation process and we will convert this code to the machine code. That machine code generated by this compilation process is also called as the object code. This compilation process, it will take the
source code written in the C programming language and then it will generate a new file containing the machine code representation of the source code. Here the file containing this object code is also called as the object file.
If you are performing this compilation process in the windows environment then this object file containing the object code will have .OBJ Extension.
If you are performing this in the Linux or Unix environment then this object file containing the object code will have .O extension.
During this compilation process several programs are used. One of the program which is used in the compilation
process is the preprocessor. Here before the actual compilation process, your Source code written in the C programming language will be processed and as the name indicates this preprocessor is a program, it takes the source code and then it will perform some operations on it. For example it will remove the new lines and spaces that you have in the source code and also it will remove the comments and it will expand the header files.
I know all of you guys can’t understand this header files and comments. you will I know learn about that later but just remember that this is preprocessor is a program which is called during the first stage of this compilation process and it will process this source code and then it will generate an optimised version of the source code and then that optimised version of the source code produced by this preprocessor will be given to the compiler.
C Compilers
C Compiler will generate the machine code from the source code given to it. There are different C compilers available and although they perform the same thing, converting the source code written in C language to the machine code, they work a bit differently some of the compilers they take the source code and then they generate the Assembly code and then that assembly code has to be converted to machine code. Some C compilers they directly generate the machine code from the source code given to them. Here two programs are mainly used. First one is the preprocessor. This preprocessor will take the source code written in the C programming language. It will produce the optimised form of the source code and then that optimised source code which is produced by this preprocessor is given to this c compiler and then this C compiler will generate the machine code which is also called as the object code. now here what you can do is you can divide your program in separate files.
For example let’s say you are writing a project using the C programming language and your project contains more than 10000 lines of C programming language instructions. so at that time if you have that more than 10000 lines of code in just one single file then it will be difficult for you to manage your project. instead of that if you can do at that project into several different parts and also into several different files then it will be easy for you to manage the code.
While writing the programs in the C programming language you can divide your project into several different files. for example I can have different files here. for example “one.c” then “two.c” and then “three.c” I can have any number of files which is containing the C source code.
If your project contains more than one file containing your programs
If your project contains more than one file containing your programs then you have to individually compile each of them and generate the object code. so you have to perform the compilation process on this one.c and then you have to generate the object code for this one.c and then for this two.c also you have to do the same thing and for this three.c also you have to do the same thing.
For each file which you have in your project, you have to perform the compilation process and you have to generate the object code for that respected source files. so now here and I’m gonna assume that my project that I’m building has three files containing the instructions written in the C programming language. one.c two.c and three.c are my files. so I have to perform this compilation process on each of them and I have to generate the object code for each of them.
After Compilation Of more than one file
After the compilation process I have the object code of these three files. Let’s say “one.OBJ” and “two.OBJ” and “three.OBJ” . This object file is generated after this compilation process. Now here during this compilation process the compilers may detect some errors in your programs. so at that time the compilers will generate the errors instead of creating the object file.
If your program is containing the errors, then the compilers will generate error and you have to go back to your source code and you have to solve those errors. If your program contains errors then this compilation process will detect that then you have to go back to your source code and you have to correct the error and after correcting the errors you have to again perform this compilation process and at that time if your program is not containing any error then the object code will be generated.
How to link all the .OBJ file?
Once you get your programs error free then the object Files will be generated. now the next thing is now we have the object files which is containing the machine code for our project. Now we have to do is we have to link all these files together and we have to generate an executable. And for that purpose a program is used and it is called as the linker.
and this linker, it will take the object files, all of them and then it links them together and then it generates the executable file and in the windows environment this executable will have the .EXE Extension.
Similarly for the different platforms they have their respected the executive extension. And here it takes the object files and also I have said before while writing the C program you are going to use the features provided by the C language. That features are provided in the C standard library. This linker will link the features that you have used from the C standard library with your code and then it will produce the executable.
If your project is containing only one file then it will take that one file and then if you have used any library functions in your program then it links them together and produces the executable.
Similarly let’s say your project is containing 3 files and then after the compilation process you will get the three object files and this linker will take that 3 object files and also it links them together and also it links the standard library functions that you have used and then it produces the executable. now we have this executable file which can
be executed in a computer. During this linking process also the linker may find out that your program is containing some errors.
For example let’s say you have made some mistakes while including or while using the features provided in the standard library. so at that time the linker will generate the error saying that it can’t link the files. Again you have to go back to your source code and you have to find the error that you have made in your source code and then you
have to correct that. And after correcting that you have to perform all the steps again. you have to compile them and then we have to link them.
If there are no errors then the linker will successfully create the executable. now that thing that we have to do once we get an executable is, we have to execute that program and for that purpose a program is used and that is called as the loader. This loader will take your executable and it will load that to the primary memory which is the RAM stand for Random Access Memory and then the processor will execute it.
After executing your program, your program May work or may produce the result successfully or it may not work properly. For example let’s say you have written a program to calculate the area of a triangle and in your source code lets say you have done a mistake while writing the formula to calculate the area of the triangle. so that’s why you will not get any error because the computer doesn’t know the formula to calculate the area of a triangle. you have to provide that correctly. but you have done the mistake In that. that’s why you will get to this execution stage. But your program it will not produce the proper results. it will generate improper results. and that’s why if your output after executing your program is not proper then also you have to go back to your source code and you have to correct your program.
Then you have to perform all these operations again, and then you have to execute your program. so this is the steps involved from writing a C program and then creating an executable and then executing that executables generated.
Now the thing is all these programs which are required to generate the executable from the C program source file, they come in a bundle. so you have to install a bundle which contains all these software’s and that bundle is called as the tool set.
So once you install a tool set for the C programming language, you will get all these softwares. you won’t get the editor you can write it in any text editor but you will get all these programs for example to perform the compilation process and for this linking purpose and all those softwares will be available in this tool set.
Avalibale Tool sets in software
There are many tool sets available for example we have the GCC tool set and we have Turbo C and also we have the Microsoft specific visual C++. You know all these tool sets are available they provide all these programs. so you just need to install the toolset, and then you will get all these programs then you can build and execute your program.
so this is it guys, this is all about the steps involved from writing a C program to generating an executable from it and then executing that generated executable file.
Tell your opinion by writing the comments in the comment section . If you think that this post will help any of your friends then do share this post with them and also if you want to study more tutorials like this, then subscribe to this website by simply poviding your mail id. I’ll see you next tutorial.
The key point for Absolute Beginners in Programming :
This is about the introduction to the Computer programming and types of programming languages, what is its history and why you should study this C programming language. Now if you are an absolute beginner and you really don’t know about computer programming and types of programming languages and all these things, Then its good to read a book called ” Programming in ANSI C ” edition seven by Mac graw hills. Click here buy it from Amazon