Monday, August 20, 2018

Compilers vs. Interpreter in C programming

Compilers vs. Interpreters


It is important to understand that a computer language defines the nature of a program and not the way that the program will be executed. There are two general methods by which a program can be executed. It can be compiled, or it can be interpreted. Although programs written in any computer language can be compiled or interpreted, some languages are designed more for one form of execution than the other. For example, Java was designed to be interpreted, and C was designed to be compiled. However, in the case of C, it is important to understand that it was specifically optimized as a compiled language. Although C interpreters have been written and are available in some environments (especially as debugging aids or experimental platforms like the interpreter developed in Part Six of this book), C was developed with compilation in mind. Therefore, you will almost certainly be using a C compiler and not a C interpreter when developing your C programs. Since the difference between a compiler and interpreter may not be clear to all readers, the following brief description will clarify matters.
In its simplest form, an interpreter reads the source code of your program one line at a time, performing the specific instructions contained in that line. This is the way earlier versions of BASIC worked. In languages such as Java, a program's source code is first converted into an intermediary form that is then interpreted. In either case, a run-time interpreter is still required to be present to execute the program.
A compiler reads the entire program and converts it into object code, which is a translation of the program's source code into a form that the computer can execute directly. Object code is also referred to as binary code or machine code. Once the program is compiled, a line of source code is no longer meaningful in the execution of your program.

In general, an interpreted program runs slower than a compiled program. Remember, a compiler converts a program's source code into object code that a computer can execute directly. Therefore, compilation is a one-time cost, while interpretation incurs an overhead each time a program is run.
The Form of a C Program
Table 1-2 lists the 32 keywords defined by the C89 standard. These are also the C keywords that form the C subset of C++. Table 1-3 shows the keywords added by C99. The keywords, combined with the formal C syntax, form the C programming language.
In addition to the standard keywords, many compilers add nonstandard keywords that better exploit their operating environment. For example, several compilers include keywords to manage the memory organization of the 8086 family of processors, to support interlanguage programming, and to access interrupts. Here is a list of some commonly used extended keywords:
asm _ds huge pascal
cdecl _es interrupt _ss
_cs far near
 
Your compiler may also support other extensions that help it take better advantage of its specific environment.
auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while 

The Library and Linking:- 

Technically speaking, you can create a useful, functional C program that consists solely of statements involving only the C keywords. However, this is quite rare because C does not provide keywords that perform such things as input/output (I/O) operations, high-level mathematical computations, or character handling. As a result, most programs include calls to various functions contained in C's standard library.
All C compilers come with a standard library of functions that perform most commonly needed tasks. Standard C specifies a minimal set of functions that will be supported by all compilers. However, your compiler will probably contain many other functions. For example, the standard library does not define any graphics functions, but your compiler will probably include some.
When you call a library function, the C compiler ''remembers" its name. Later, the linker combines the code you wrote with the object code already found in the standard library. This process is called linking. Some compilers have their own linker, while others use the standard linker supplied by your operating system.
The functions in the library are in relocatable format. This means that the memory addresses for the various machine-code instructions have not been absolutely defined— only offset information has been kept. When your program links with the functions in the standard library, these memory offsets are used to create the actual addresses used. Several technical manuals and books explain this process in more


Compiling a C Program:

Creating an executable form of your C program consists of these three steps:
1. Creating your program
2. Compiling your program
3. Linking your program with whatever functions are needed from the library
Today, most compilers supply integrated programming environments that include an editor. Most also include stand-alone compilers. For stand-alone versions, you must have a separate editor to create your program. In either case, be careful: Compilers only accept standard text files for input. For example, your compiler will not accept files created by certain word processors because they contain control codes and nonprinting characters.
The exact method you use to compile your program will depend upon what compiler you are using. Also, how linking is accomplished will vary between compilers and environments; for example, it may be included as part of the compiler or as a stand-alone application. Consult your compiler's documentation for details.

C's Memory Map:

A compiled C program creates and uses four logically distinct regions of memory. The first region is the memory that actually holds the program's executable code. The next region is memory where global variables are stored. The remaining two regions are the stack and the heap. The stack is used for a great many things while your program executes. It holds the return addresses of function calls, arguments to functions, and local variables. It will also save the current state of the CPU. The heap is a region of free memory that your program can use via C's dynamic memory allocation functions.

  • 0Blogger Comment
  • Facebook Comment

Post a Comment