Chapter 4. How to compile & execute C program?

Instance Spend Less Desirable Quotes Educational Inspirational Quotes For Students Quotesgram

Contributed by : Shehrevar Davierwala

( Note below lines of code are with respect to gnu )

The Compiler
A C program is made by running a compiler which takes the typed source
program and converts it into an object file that the computer can execute. A
compiler usually operates in two or more phases.

Phases of compiler
Phases of compiler
  •  Phase 1 scans a source program, perhaps generating an intermediate
    code (quadruples or pcode) which helps to simplify the grammar of the
    language for subsequent processing. It then converts the intermediate
    code into a file of object code (though this is usually not executable
    yet). A separate object file is built for each separate source file. In the
    GNU C compiler, these two stages are run with the command gcc -c;
    the output is one or more .o files.
  • Phase 2 is a Linker. This program appends standard library code to
    the object file so that the code is complete and can “stand alone”. A C
    compiler linker suffers the slightly arduous task of linking together all
    the functions in the C program. Even at this stage, the compiler can
    fail, if it finds that it has a reference to a function which does not exist.
    With the GNU C compiler this stage is activated by the command gcc
    -o or ld.

On GNU systems, this results in the creation of an executable program
with the default name a.out. To tell the compiler what you would like the
executable program to be called, use the -o option for setting the name of
the object code:
gcc -o program-name filname
For example, to create a program called ‘myprog’ from a file called myprog.c,
gcc -o myprog myprog.c

Compile and Execute C Program

Let us see how to save the source code in a file, and how to compile and run it. Following are the simple steps:
1. Open a text editor and add the above-mentioned code.
2. Save the file as hello.c
3. Open a command prompt and go to the directory where you have saved the file.
4. Type gcc hello.c and press enter to compile your code.
5. If there are no errors in your code, the command prompt will take you to the next line and would generate a.out executable file.
6. Now, type a.out to execute your program.
7. You will see the output “Hello World” printed on the screen.
$ gcc hello.c
$ ./a.out
Hello, World!
Make sure the gcc compiler is in your path and that you are running it in the directory containing the source file hello.c.

Types of Errors:

Errors are mistakes which we the programmers make. There are different kinds of error:
1. Syntax:
Errors in the syntax, or word structure of a program are caught before you run it, at compilation time by the compiler program. They are listed all in one go, with the line number, in the text
file, at which the error occurred and a message to say what was wrong.
For example, suppose you write sin (x) y = ; in a program instead of y = sin (x);

A program with syntax errors will cause a compiler program to stop trying to generate machine code and will not create an executable. However, a compiler will usually not stop at the
first error it encounters but will attempt to continue checking the syntax of a program right to the last line before aborting, and it is common to submit a program for compilation only to
receive a long and ungratifying list of errors from the compiler.

Shock!!!  It is a shock to everyone using a compiler for the first time how a single error can throw the compiler off course and result in a huge and confusing list of non-existent errors, following a single true culprit. 😉

Thumb Rule::

As a rule, look for the first error, fix that, and then recompile. Of course, after you have become experienced, you will recognize when subsequent error messages are due to independent problems and when they are due to a cascade. But at the beginning, just look for and fix the first error.

2. Intention
Errors in goal or purpose (logical errors) occur when you write a program that works, but does not do what you intend it to do. You intend to send a letter to all drivers whose licenses
will expire soon; instead, you send a letter to all drivers whose licenses will expire sometime.
If the compilation of a program is successful, then a new file is created.
This file will contain machine code which can be executed according to the rules of the computer’s local operating system.
When a programmer wants to make alterations and corrections to a C program, these have to be made in the source text file itself using an editor; the program, or the salient parts, must then be recompiled.


Leave a Reply

Your email address will not be published.