That's how the C language itself works... from code to execution !
Welcome to the Newbie Programmer Series. In the last post we have learned about scope (click here). The scope, of variables, to be used in a program within functions. In this post we will learn how the c programming language itself work ? Like what we have learned so far is that, to write code and run it in some editor (we used codeblocks, click here) But actual image is a little different, that we will discuss here. Knowing this we would have a better insight of the features of language and so we can use it more effectively. After all it is the part of program structure. So if you are new to this series, please go the index (click here) and read all the previous parts so that you can easily understand everything.
This post is a little theoretical, We need to know the important concepts before we go further ( in short, i mean you would need to read it twice :p )
So let's get started, please continue reading !
We make a program. Lets say the calculator program. We will write the codes for it. And the code is saved in some file, with the ".c" extension. Lets say we save the code in a file named "calc.c".
Inside our code, we have used some header files (intro here). Like we have used "stdio.h" file. So now we have two files to be used.
Suppose we have another program, "graph.c" which adds the feature to draw graphs to our calculator program.
Now we have three files to deal with.
Finally we build and run it (lets stuck with codeblocks only). The program runs.
But we don't know what happens to the files ? How our codes become a program ? We are discussing that here.
The different code files we have written are compiled into object files. That means whatever we have written makes no sense to the computer. (click here to know more) When we "build" it, it actually means to "compile" and through this process, our code is get translated into the language that computer can understand. So our "calc.c" and "graph.c" get converted into "calc.o" and "graph.o". These are called object files.
We have used the "stdio.h" files. Through this we use the functions which are stored in library (click here to know more). So we have to add those codes to our program.
It's like we don't write the code for our "printf" function. We just use it and tell computer that the code of "printf" is written in a library, ask "stdio.h"
Computer then ask "stdio.h", finds the code of "printf" and then use it.
So we have to add these library functions too.
And, we have two files for a single program. We coded our program in two different files but they are actually one single program. But we have two object files.
So these two files and the library functions which we have used are joint together by the process called "linking". This process will links up all the three files, "calc.o", "graph.o" and "stdio.h" to one single executable file. In windows, we finally got a ".exe" file.
So in short,
- Our C code files are compiled into object files.
- These object files and external library functions are linked into a single executable file
This becomes a hard to deal with process, so we have used a software called "CodeBlocks" In which you simply write the code and click "Build and Run" and everything is managed automatically !
There is a question that will come into your mind that How our calculator program can be divided into two different files ? Yes we can write in two different files but how we will use same variable in two different files ?
Is it similar to how we have used same variable with two different functions ? Yep, we studied that under scope part 1 (click here). In the next part, The scope Part 2, we will discuss using many files together for a single program.
That's it for today, Stay connected !
Please share this post as much as you can so that we can get connected to more geeks who wants to learn programming but have no clue. Thanks for reading :) (c)Shubham Ramdeo, All rights reserved.
Please share this as much as you can so that we can get connected with more and more people who want to learn programming.