Saturday, May 9, 2009

How C Compilers work?

I have some questions regarding compilers .


1%26gt;Since the hardware for every machine is diferent, a compiler then must


take into account the machine architecture.So how does a compiler do it?


Does the creator of the compiler manually tell the compiler about the hardware or does the


compiler itself interacts with the machine and figures out its H/W?





2%26gt;can i download copilers?


if so can i use 2 compilers for the same programming language in a machine?





3%26gt;how are compilers created?(and what kinds of programs are they?)just a basic overview please.





4%26gt;i am interested in the c language,i have heard that a lot of games are created using c/c++.


but then games use sound and graphics.What are library files that contain graphic functions?





5%26gt;can i update a compiler? is it possible to add extra functions to a library?How?





6%26gt; C is a middle level language since it has features of both a high level language and


low level languages.Does compilers work differently for different levels of abstraction


present in a language?what kind of language are used to create operating systems?





please help me out with these questions.

How C Compilers work?
1). A compiler "compiles" a code so that the host Operating System can understand. It's generally speaking. The compiler doesn't interact directly with the hardware, this is the job for the Operating System!


2). Yes. You can download as many compilers as you prefer. A compiler is a software which interacts with the operating system, just like any other..


3). This is difficult to answer... The starting point for creating compilers are formal languages and theory of automata.. Then comes the implementation...


4). C/C++ is the industry standard, so of course many software are written in this language (but not only in this one...). Libraries for grafic function include OpenGL (Glut,FreeGlut and so on), DirectX... Renedering software is also being used in the gaming industry. (let's not foret about Artficial Intelligence)


5). You can update a compiler by downloading the improved version or adding extra libraries to it. You can even create your own libraries if you prefer to start from scratch.


6). Unix for example is entirelly based on C language! C language doesn't support OOP (Object Orientation Programming), that's one of the reason C++ was created. It's hard to say in what language windows is written for example since it's closed source...
Reply:#1 - Compiler creator must know about hardware beforehand. However, his job is not too difficult. He only needs to support C syntax. The magic comes when you add on libraries (ie standard libraries, third party libs etc). Those libraries also are compiled to work on your machine, possibly by the compiler you are using, maybe by another.





#2 - Yes, compilers are available for download (ie GCC). Yes, you can use different C compilers for the same program. You will probably compile different modules into libraries or at least object files and join them all together into an executable as the final step.





#3 - Compilers are created with (would you believe it) other compilers! Once upon a time, there were no compilers and therefore some super-geak built a machine code assembler directly from machine code. The assembler was then used to build a compiler etc.





#4 - No doubt, if you are pursuing a career in PC games, you will want to investigate DirectX, available from Microsoft...that is after you have some mastery of C++. DirectX is a library that allows you to have a direct interface to graphics, sound and networking hardware...without having to know the ins and outs of each different sound card, video card or network card. Hardware manufacturers write device drivers that provide a DirectX interface so you get maximum performance without having to learn the manufacturers particular hardware interface (and they are vastly different for each device). OpenGL is similar to DirectX, bexcept it only gives you control over graphical devices. DirectX is a huge chunk to learn about, especially with respect to 3D graphics. But, it is only for PC games. You can also program for consoles like the PS3, Nintendo, XBox with C; but you will need a different compiler for each.





#5 - You can add libraries and that greatly increases the functionality of the programs you write. As for the compiler, unless it is Open Source, you will need to wait for the publisher to make updates to the compiler.





#6 - C is relatively very low level compared to other languages these days. It is not very far at all from machine language, but it does make code simpler to read by humans. Most OS's are written with C/C++. In fact, aside from maybe some ancient ones, I'd be surprised if any OS was written in something besides C/C++.


No comments:

Post a Comment