Thread: why is there a main?

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    why is there a main?

    I know this is a newbie question but I never asked it when I had the chance, so why not now?

    Why exactly does there have to be a main function?
    why couldn't you just:
    Code:
    #include <stdio.h>
    
    printf("Hello, World\n");
    it sort of makes sense if you look at it before it's completely compiled but in Asm.

    without the main function you can't really call the memory for the string but...
    even in assembly it still doesn't make sense why C can't just write the string to a different address that is called globally like I want.

    so if I have to have a main
    how can I display something without a main?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is a main so the program has a starting point. Not every one wants to make every line of code be executed in the order it is typed. This is the reason there is a main. Without it, we'd be stuck with BASIC.

    And yes, you have to have a 'main' function. (Java, C++, C, they all have 'main'.)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Well, actually I did it!
    I displayed text without main
    but not in C or C++
    though it is possible

    __asm__ = ...

    or something, I can't remember the assembly call.
    but I did it in asm.

    Created .LC0
    and copied the memory into the top of a temporary stack and displayed it using printf without Main or anyfunction
    just "tmp "
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    No, of course you can write assembly programs
    which don't have main. You still need to use a directive
    to the assembler such as _start so it knows where to
    begin I guess.

  5. #5
    *
    Guest
    You all are just *so cute*.

    The _reason_ you have a main() is because when the compiler builds the program and puts the jump tables in, it has to have somewhere to jump to, to get _your code_ going.

    All programs start at relative location 0x00000000 to wherever the RAM heap is loaded for their memory segment.

    Why? Because zero is the only location the processor knows about-- it's always there.

    So let's say your program is double-clicked on, or told to execute in some like manner. The O/S allocates a block of RAM (the "heap") for your program. It loads it into the heapspace and jumps to location zero.

    At location zero, the header stub begins to execute. It quickly calculates where your globals are, where your function addresses are (the address of main offset from relative "zero") and stuffs these into the jump table so they don't have to be calculated again. When the jump tables are built, the stackframe is initialized, and a jump is made to main() (now that it knows where it is).

    Literally, "main()" is a label. Just like the name of every other function in your program. Even assembly requires the use of labels. Although assembly merely starts executing at the first byte of your program, because there is no jump-table setup in assembly-- such things are resolved by the linker at assemble time.

    So let's say your program gets loaded at location 0x0007FE41A, in RAM.

    That is your relative "zero".

    So when your code gets loaded, the O/S jumps to 0x0007FE41A. Your jumptable and init code starts to run and when its complete it jumps to the main() label to get *your* code going. If that happens to be 754 (or 0x2F2 in hex) bytes into the code segment "from relative zero" then all references to main() become:

    0x0007FE41A + 0x000002F2 = address of first byte of main() function's code.

    ---

    Now, depending on what code snippet you're writing, it may not be called "main()" at all. But there normally is some label you start with.

  6. #6
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Hey!! Smile everybody! We're cute!!!!

    >>You all are just *so cute*.

    Seriously, good post *. I have only a little experience with asm so your post was informative.

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Not to be repetative, but I second Mr. Jdinger's comments.

    Let me get this straight.
    Programming food chain:
    C or C++
    - then -
    ASM
    - then -
    Machine code.

    Programming on an understandable level:
    C or C++ - Most understand fluently.
    ASM - A good number understand fluently.
    Machine code - Good luck

    Am I pretty close?
    The world is waiting. I must leave you now.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Which in a nutshell is exactly what I said in the first reply. Like I often say: "God I love simplicity."

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    I'd just like to point out that the way programs loaded and executed posted upthread is not the only way, and in the modern age there are vast differences.

    The entry point of a program does not have to be absolute, in fact, most formats for executable programs I know of are either automatically relocatable or are made relocatable by using various techniques ranging from the use of address translation to data in the executable format which can be used to retarget all non-relative operations.

    The reason why main exists is to define the entry point of your code: exactly how this is dealt with is an implementation feature. On one platform it might define a label in an object file which is invoked from within some static library. On another it might mean that the routine has a specific base address and invoked by the system (and the address does not have to be 0 or even known). The important detail is that it is an abstraction for the entry point of the program which is implementable across many architectures and platforms. It also happens to be an easy one to understand and to implement.

    Ian Woods

  10. #10
    +
    Guest
    you guys are so cute, and so wrong. The sole reason for main is to allow your code to have substance. Some of the programmers out in the "real world" dont have much to put into their programs so main allows them to have an extra line of coding to make them feel better. Man you cute little "coders" need to read some more Aldous Huxley. (i.e. The Doors of Perception)

  11. #11
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > Some of the programmers out in the "real world" "blah blah blah" *continuing...*
    I'm not trying to be a "real world programmer".
    I'm not a real world programmer who comes to messages boards to make others feel like ants either.
    A very good portion of this board will probably never be professional programmers, nor have the intention of doing so.

    > you guys are so cute
    Cheese!
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why To Use int main()
    By year2038bug in forum C Programming
    Replies: 2
    Last Post: 08-19-2005, 01:28 PM
  2. passing params to main()
    By mike11 in forum C++ Programming
    Replies: 14
    Last Post: 06-21-2005, 12:36 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Int Main or Void Main?
    By FromHolland in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2003, 04:29 PM
  5. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM