17

I am searching for a month for blog posts about "how C program works". Most of them go like

  1. Compilers do these things
  2. Linkers do these things
  3. Program is put into memory; and stacks, heaps, etc.

I thought I would first read about how compiler works to understand the flow of the program into the machine. Dragon Book seems to be universally preferred. But truthfully, it's too intensive. I am not good enough now to go through it all.

So I began to read about hardware. But There too, they explain about buses, I/O signals, structure of memory, writing cache-friendly code etc. But with no proper examples.

But still I could not find myself satisfied or being able to completely visualize the process.

2 hours ago I decided to ask this question.(Since I am scared of It might be not useful to SO community, or off-topic question or other down-votable categories) and I did not find out any post relating to this exactly. There was one about "how compiler does the compilation", but the answers showed that it's too broad a question.

My question is this:

I would like to know how, in depth, a C program works. If you cannot tell me explicitly, please redirect me to a book or another post on another website that can give me the answer to this.

I am here until I get a response. If you have any suggestions regarding this post, tell me. And It is not my first language, so please take all my sentences as being soft and polite.

Thanks.

UPDATE:

Along with the accepted answer, there are some very nice links as well as suggestions which give partial answers or the way to proceed further to understanding what I am trying to understand.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
l0k3ndr
  • 536
  • 5
  • 24
  • 4
    It must be black magic! – bblincoe Feb 12 '14 at 19:31
  • 2
    Then the magicians here must be able to explain the trick to me :) – l0k3ndr Feb 12 '14 at 19:32
  • See [this answer](http://stackoverflow.com/a/9495727/841108) to a similar question. – Basile Starynkevitch Feb 12 '14 at 19:32
  • 10
    This is a useful question and it's great that you're asking it. Too many beginners ignore how things work under the hood. BTW, "Hello" and "World" would not be tokens from the lexer's POV. – Ed S. Feb 12 '14 at 19:33
  • I changed it, is it okay now Ed? – l0k3ndr Feb 12 '14 at 19:35
  • 4
    @humble_fool: I didn't mean you should change it. You asked the question, I wouldn't expect you to already know the answer. :) – Ed S. Feb 12 '14 at 19:36
  • @BasileStarynkevitch Its related but it is not solving my problem. I want a explaination using example. – l0k3ndr Feb 12 '14 at 19:37
  • 2
    This borders on "too broad". I think you're basically asking how CPU instructions produce screen output; I think you seem to have a pretty good idea of how C is converted into those instructions, just not how they move through the system to produce something like characters on a screen? – user229044 Feb 12 '14 at 19:41
  • 1
    The best way to get an answer is to *study* computer science. This takes several years (e.g. learning at university), after which you'll be able to write a book to answer your question. – Basile Starynkevitch Feb 12 '14 at 19:45
  • @meagar I think he is asking how everything starting from the CPU, source code, compiler, linker, loader etc fit together and produce the effect they do. – ajay Feb 12 '14 at 19:46
  • It's like I know some bits from here and there. But unable to connect all that stuff. But it's not like I want to ask you what are the drawing algorithms or that stuff. But yea, I would like if you explain that "it happens through this module in linux kernel which you can see here" – l0k3ndr Feb 12 '14 at 19:48
  • 1
    One great link that explains how a program gets executed on a linux-based system is [this](http://linuxgazette.net/84/hawk.html) – Fredrik Pihl Feb 12 '14 at 19:51
  • 3
    "_I am unable to continue with programming like this_" - Please don't treat this too seriously, **especially** if you've been learning CS/programming for just 2 years. I bet majority of programmers don't know half of the answer to this. It's good that you even want to know, it just happens that your question is quite complex. Don't see this as your failure just because it might take quite some time to learn and feel confident about understanding the whole process. – user2802841 Feb 12 '14 at 19:51
  • @BasileStarynkevitch Yeah, I am going through the process of learning. But If someone who has studied it and written a book , and he could show me how he/she visualizes it, I can atleast know what I am dealing with. – l0k3ndr Feb 12 '14 at 19:52
  • I don't think it's an unreasonable question to wonder how a simple application runs from start to finish and I do believe there is someone(not me >.>) on SO able to answer this question. I think the answer should be at least [this detailed](http://stackoverflow.com/a/11227902/2033671) –  Feb 12 '14 at 19:54
  • @humble_fool: I believe I know enough to write such a book, but I also believe that not much people actually would be interested in it. So it is not worth the effort writing it. Also, you can find partial answers and mix them yourself. On Linux, you could write a `helloworld.c` program, compile it to a `hello-world-bin`, then `strace ./hello-world-bin` and try to understand all of that trace. – Basile Starynkevitch Feb 12 '14 at 19:54
  • 2
    You're asking for about 12 credit hours worth of information – Sam I am says Reinstate Monica Feb 12 '14 at 19:59
  • 1
    It would take way to long for me to attempt to answer for each of these, nor could I create a great example for every single one. But your general compilation will have some stages: 1. Lexical Analysis 2. Syntax Analysis 3. Type Checking 4. Intermediate Code Generation 5. Register Allocation 6. Machine Code Generation 7. Assembly and Linking. A decent read on the topic is http://www.diku.dk/~torbenm/Basics/basics_lulu2.pdf . Its a little long but its not too hard to digest. – MrHappyAsthma Feb 12 '14 at 20:00
  • @BasileStarynkevitch I am completely sure that it would be a best-seller. – l0k3ndr Feb 12 '14 at 20:00
  • This is a great, broad and off-topic question. – Otávio Décio Feb 12 '14 at 20:00
  • @OtávioDécio I am sorry sir. Can I ask it on some other stackexchange network site which would handle these kinds of topics. Beside I have already mentioned that you can have links supplementing your answer. – l0k3ndr Feb 12 '14 at 20:03
  • And one point of advice. When you're looking for a fundamental explation of computer architecture, just to demystify it. You'll know you're in the right place when you're reading about `and gates` and `not gates` and `or gates`, and eventually half-adders and multiplexers and latches and flip-flops, and other circuit-level stuff – Sam I am says Reinstate Monica Feb 12 '14 at 20:06
  • 2
    The answer can be as broad as the person who is answering wants. I don't think this question is too broad. – fotanus Feb 12 '14 at 20:11
  • @fotanus Exactly. Thanks. I wanted to say it but couldn't find words. – l0k3ndr Feb 12 '14 at 20:13
  • I have edited my question, will it still remain marked too broad to answer??? – l0k3ndr Feb 12 '14 at 20:22
  • I think it will take a few days to get it unhold. Thanks for all your help guys.. Starters like me respect all of you computer-lover-programmers a lot. And I wish that there should be a Like button on Top of SO , so that I could like it everyday.Good night. – l0k3ndr Feb 12 '14 at 20:40
  • 2
    check out [Code: The Hidden Language of Computer Hardware and Software](http://www.amazon.com/Code-Language-Computer-Hardware-Software/dp/0735611319/) – Nick Dandoulakis Feb 13 '14 at 01:40
  • Someone has narrowed it down for me.Thanks a lot for that. Though it's not exactly going to serve my initial purpose. But it will do it more than half way. :) – l0k3ndr Feb 13 '14 at 04:04
  • I think it has been narrowed down, So why they are still keeping it on hold...??? – l0k3ndr Feb 13 '14 at 21:53

2 Answers2

5

The best answer to this question by far comes from the book "The Elements of Computing Systems," by Noam Nisan and Shimon Schocken. This book starts from the simplest possible electronic components, assembles them into a working processor, invents a simple assembly language for it, writes an assembler for that, and ultimately shows you how high-level languages can be compiled onto it. Reading the book, and working all the examples (which use a simulator for the hardware, so no workshop required!), will forever change the way you look at computers; he will literally understand everything from the lowest to the highest levels, and see how they work together. See the book's website for more info.

r3mainer
  • 23,981
  • 3
  • 51
  • 88
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
1

It's too broad a question (as you have observed).

If you really want to understand from bottom up - buy an OLD computer from the 80's off ebay. Sinclair Spectrum/BBC it really doesn't matter but make sure you get plenty of books and manuals that go with it.

You will learn plenty because these machines were well documented and what wasn't documented was discovered and then documented :)

They are also sooooo much simpler than a modern quad core multi-gigabyte memory job. It will all fit inside your head easily

or for a modern start, Arduino or Raspberry pie maybe.

Despatcher
  • 1,745
  • 12
  • 18
  • I disagree. Old machines (unless physical machines before 1940) will not reveal the inner workings any more than AMD64 based machines, for example. In fact they may hide more, since one can easily download the Intel developer manuals and learn the theory of its architecture, whereas you have fewer options for understanding a Z80 to the same extent. – Vortico Feb 12 '14 at 20:09
  • The Z80 (not used in the BBC Micro actually) is probably one of the most documented processors. It took me 1 (one) google to get the entire manual & architecture in detail direct from Zilog. Nothing is hidden. 1 (one) google to find a C compiler and assembler. It's all so much simpler and the resources are all out there. – Despatcher Feb 12 '14 at 21:02
  • @Vortico: The thing about the Speccy/BBC is that they didn't really have an OS, so you were generally dealing directly with the hardware. The CPUs were conceptually much simpler than modern ones - there was no caching, no memory managers, no uops translations and superscalar architectures. They just read an instruction, did something, then read the next instruction and so on. That is the one drawback to the RasPi - the hardware is really complex. – Skizz Feb 13 '14 at 03:05
  • Yeah, doing something like this is my wish too. But it's very much time consuming and might not be possible before holidays. – l0k3ndr Feb 13 '14 at 04:07