Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How "Hello World" works in C [closed]

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.

like image 698
l0k3ndr Avatar asked Feb 12 '14 19:02

l0k3ndr


People also ask

How do you write Hello World in function?

main() function is the main block of code where the execution of our program begins. Inside the main() function we have two statements, printf("Hello, World!") and return 0. printf() function is used to display the string inside it into the output window. printf("Hello, World!") will print Hello, World!

What does Hello do in C?

h> tells the compiler to include the header file for Standard Input Output file which contains declarations of all the standard input/output library functions. More on Preprocessors in C. This line is used to declare a function named "main" which returns data of integer type.

How do you close a program in C?

And the exit() function can also return a value when executed, like the return statement. So the C family has three ways to end the program: exit(), return, and final closing brace.


2 Answers

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.

like image 171
Ernest Friedman-Hill Avatar answered Oct 14 '22 18:10

Ernest Friedman-Hill


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.

like image 40
Despatcher Avatar answered Oct 14 '22 17:10

Despatcher