answersLogoWhite

0

How does C compiler work?

Updated: 8/11/2023
User Avatar

Wiki User

12y ago

Best Answer

C is a programming language.

Compiler is used to convert our source code which is in high-level language or human understandable code into machine language. Compiled source code can be executed any where once if it is compiled .

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

The C compiler works by converting individual translation units into corresponding object code files. The compiler can only process one translation unit at a time however the object files will contain all the information required by the linker to produce an executable. The compiler only needs to perform one complete compilation of all the translation units. After that, only those translation units that have been modified since the last compilation need be re-compiled (assuming the object files are still in place).

An object file is essentially the machine code representation of the corresponding translation unit, however memory offsets are symbolised. Some of these symbols will have internal linkage (relative to the object code) while others will have external linkage (relative to the executable). It is the job of the linker to examine the symbol tables produced by the compiler and to replace the symbolic offsets with actual offsets.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The C++ compiler is actually made up of three separate programs: the preprocessor, the compiler itself and the linker. However, the preprocessor and the compiler are usually implemented as a single program that does both.

Before the compiler can actually compile your source code, the source code must be preprocessed. This is done by the preprocessor. This will scan through your files and headers and create temporary intermediate source files where all the precompiler directives have been acted upon and completely stripped from your source. Macros are also expanded within your source. The intermediate files are what the compiler actually works from. Most compilers will include a switch that will save these intermediate file so you can see what the preprocessor actually does with your source.

For instance, the #include directive instructs the preprocessor to locate the named file and insert its entire contents in place of the directive, just as if you'd copy/pasted the contents yourself. However, prior to insertion, the included file itself must be preprocessed and this may invoke more #include directives. However, no included file can be included more than once, so directives known as inclusion guards must also be processed. Once the preprocessor has processed the included file it can then remove the #include directive and insert the content. If inclusion guards resulted in no content being inserted, the #include directive is simply removed.

The preprocessor also creates a table of #define directives so it can keep track of what is defined (and undefined!), and refer to these definitions throughout the preprocessing stage. Thus when it encounters a defined symbol within your source, it can perform a text-replacement or expand a macro. This effectively strips out all macro definitions from your source and is the reason the compiler cannot debug macros. It can only see the expanded code and can only identify errors in the expanded code; it cannot tell you where those errors originate since the original macro definitions no longer exist as far as the compiler is concerned.

Once the intermediate files are created, the compiler can finally get to work translating the C++ instructions into machine code instructions. Each source file is compiled to produce an object file (object code should not to be confused with C++ objects). Object files are not executable files in their own right because memory offsets cannot be finalised until the object files are linked, a role that is undertaken by another program, known as the linker.

The linker links the object files together, along with any required libraries and resources, and creates the actual executable. Provided there were no compile-time or link-time error, the program can now be run. If runtime errors occur, you have to go back to your original source to fix the bug and then recompile. Compilers are optimised so that they need only recompile the source files that have actually changed since the last compilation. Thus unaffected object files do not need to be recompiled.

Although C++ compilers are not the same as C compilers they both do pretty much the same job. However, C is much simpler to compile in that much of the source has a near 1:1 relationship with the resultant machine code (hence it is a low to intermediate level language). C++ has a higher level of abstraction, but retains much in common with C, to the extent that the original C++ compiler, Cpre, was really nothing more than a C++ to C translator, the actual compilation being handled by the C compiler itself. However, C++ evolved and the next compiler, Cfront, became a true C++ compiler.

Though each release of C++, the compiler has become more and more sophisticated, capable of recognising and implementing coding conventions that even the coder may not be aware of, such as enabling tail-calls in recursive functions to make them more efficient. And since compilers are also platform-specific, they can exploit the underlying hardware to improve efficiency in ways that would otherwise only be possible using low-level languages such as assembler. Compilers also have a wide range of options which the coder can turn on and off in order to tailor the machine code to suit his requirements.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A C compiler translates the program from the C language into machine language (object module).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How does C compiler work?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Does c sharp work on c or c plus plus compilers or you need a c sharp compiler?

They are different languages, each of them requires its own compiler.


It is possible to build a C compiler on top of a C compiler?

It is possible to build a C compiler on top of a C compiler. From the directory, run the shell script.


No of pixel in c compiler?

There are no pixels in the C compiler!


Which compiler is used for writing c plus plus exception handling programs?

C++ compiler, obviously, a C compiler won't do.


What is a C compiler as used in C programming?

In C programming, C compiler is user to translate C source into C object module.


How carefully does the compiler pay attention to indentation?

The compiler does not pay any attention whatsoever to indentation in C and C++.


What is the difference between java compiler and C compiler?

Greetings Technologist. The Java compiler compiles Java code, whereas the C compiler compiles C Code HTH Richard Wolf Software Architect


How a c compiler work?

compiler is just translator it translates the our source program to equivalent langauage,so computer can easily understand and compile that program efficiently................


What is the difference between GCC compiler and turbo c compiler?

1. The Gcc compiler cna support multiple languages such as c,c++,Java,Fortran,Pascal etc.... Whereas in turbo c cpmpiler,it doesn't support multiple languages. 2. GCC compiler is a free software,... Whereas in turbo c cpmpiler , it is not a free software. 3. The GCC compiler is is a portable compiler,it runs on most plateform avilable today... Whereas in turbo c cpmpiler, it is not a portable compiler.


Is it possible to inherit structure in c?

If a C compiler is available, then so is a C++ compiler - just use one that produces C as its output.


What is the work of compiler in dot net?

It translates the .NET-compatible code (i.e. VB, C#) to MSIL code, then, on demand, the JIT compiler compiles it to machine code.


How c compiler works when it's been coded in c?

The very first C-compiler was written in some other language.