Thursday, 24 March 2016

Difference between Turbo C and GCC

I started with C on Turbo C and then switched to GCC compiler after I was fed up with Turbo C. There are many significant differences between both of them.

The first significant difference is that Turbo C is an IDE(Integrated Development Environment). On the other hand GCC is a command line compiler.

Let us go through the working on both systems.

Here is a sample code we can run on the system to check.

#include<stdio.h>
main()
{
           printf("Hello World\n");
}


Starting with Turbo C, we need to install the IDE in the system. TC works only with the DOS systems. But, we see it running on the windows machine too. Yes. It is running there. But there is a virtual machine named as "New Technology Virtual DOS Machine". We can find the virtual machine in the location "C:\windows\system32\" as "ntvdm.exe". We can do all the operations which are supported by DOS. And this is the greatest drawback. We can do only the operations supported by DOS. There are some other compilers for C which can be used with Windows programming. But we will talk about them later.

To compile with Turbo C, just start the IDE, type the code and and press CTRL+F9. But DO NOT forget to save the code at first. To save the code "ALT+F --> S" combination will be used.

There is a common problem with the beginners in Turbo C that once they start an infinite loop by accident, they can't get over it, and they have to close the terminal. It results in loosing the code. To overcome this problem, saving the code before each compilation is ultimate solution. And to terminate an infinite loop, we can press "CTRL+C".

Now about the files. Every time we work with Turbo C compiler, it generates four files we can access. The first one is the code file itself. Let us consider that we are working with a file "sample.c".
One thing to remember that since Turbo C is working on NTVDM, it will support a file name with DOS convention. That is, it must have a maximum 8 characters long name and maximum 3 characters extension, and off-course it should start with an alphabet or underscore and must consist of alphabets, numbers and underscore, and nothing else.

Each time we edit and save the file, TC (Turbo C) saves the current version of file as current name, in present scenario "sample.c". TC also provides a backup file which is the last version of the current file, named same as the file but with an extension "bak", "sample.bak" in our scenario. We have to remember that only one backup exists for each file, that is the last backup.

To compile the code, we press F9. In this stage TC compiles the code, and generates an object file with extension "obj", "sample.obj" in our scenario. Then Linker converts the object code into executable code, with extension ".exe", "sample.exe". These are the four files we can access.

Location of all these files are by default "\tc\bin". We can see all files here and can execute the application directly by clicking on it.

There is one thing tickling in the mind, "F9" or "CTRL+F9"?

The difference between them is:
F9: Compile. Compiles the code and display errors if any.
CTRL+F9: Compile & Execute. Compiles the code and display errors if any and then runs the program.

Now, I think it enough for us about TC. Other features and functionalities, we can explore on our own.

It's time now to visualize GCC.

GCC works with the command line interface of Linux based system. We should check for if GCC is already installed in the system. To check it just type the word "gcc" in the terminal. If it is installed, it will show an error that no input file found. Otherwise you will need to install it. To install it, just type the command "sudo apt-get install gcc" and follow the instruction.

Let us suppose that it is already installed on our system.

To start, we will open any text editor, and type in the code. After typing the code save the file with extension "c". Continuing our assumption, let us name it "sample.c".

To execute the code, at first move to the directory where the file is saved. Then type the command "gcc sample.c". If code has no errors it shows nothing and just returns to another prompt. If there are any problems in the code, it lists all of them with line and column numbers. We just find them, debug them and rerun the command. And repeat the process till each there are no bugs in the code.

On the successful compilation of the code, executable file is generated named "a.out". To execute this file type "./a.out". It runs the program. On this stage we can find some logical errors in program such as infinite loop. To break the loop, same combination of keys work here too, "CTRL+C".

Now we have a problem here. GCC always generates "a.out" on compilation. If we have more than one programs, and we compile them one by one, we get only one "a.out".

To overcome this problem we use directed output. Suppose we want our executable file as same name as source file. We will use the command, "gcc sample.c -o sample.out". Now we have our executable file named "sample.out". To execute this file type in "./sample.c". And the code is running now.

About the files generated during the compilation of code in GCC, only file generated is executable file.

This is all we have about this blog. See you soon with other blog, with something more for you from my love.

2 comments: