4

What is so special about main() function in C? In my embedded C compiler it tells the program counter where to start from. Whatever appear first (as instruction) into main function it will be placed first in the flash memory. So what about PC programs? What is the meaning of main() when we program for PC?

Radoslaw Krasimirow
  • 1,833
  • 2
  • 18
  • 28
  • 4
    possible duplicate of [In C, how is the main() method initially called?](http://stackoverflow.com/questions/3469955/in-c-how-is-the-main-method-initially-called) – Alfabravo Sep 15 '15 at 14:26
  • 1
    main() is the known entry point when the run-time code is ready to start executing your program. – David Pointer Sep 15 '15 at 14:28

7 Answers7

6

On a hosted implementation (basically, anything with an operating system), main is defined to be the entry point of the program. It's the function that will be called by the runtime environment when the program is launched.

On a freestanding implementation (embedded systems, PLCs, etc.), the entry point is whatever the implementation says it is. That could be main, or it could be something else.

John Bode
  • 119,563
  • 19
  • 122
  • 198
3

In simple terms:

There is nothing special about the the main function apart from the fact that it is called by the system when your program is started.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    Not entirely true. It is the only non-`void` function that you may leave without returning a value (since P99) and in threaded environments (POSIX or C11) a return from `main` terminates the whole process. – Jens Gustedt Sep 15 '15 at 15:06
  • @JensGustedt true, but that's why I wrote "In simple terms". – Jabberwocky Sep 15 '15 at 15:53
1

The main function is where the "C program" starts, as far as the C standard is concerned. But in the real world outside the standard, where there is hardware, other things need to be done before main() is called.

On a typical embedded system, you have a reset interrupt service routine, where you end up after power-on-reset (or other reset reasons). From this ISR, the following should be done, in this order:

  • Set the stack pointer.
  • Set all other memory mapping-related things (MMU registers)
  • Safety features like watchdog and low voltage detect are initialized.
  • All static storage duration variables are initialized.
  • main() is called.

So when main() is called, you have a stable enough environment for standard C programs to execute as expected.

To use main() as the reset vector is unorthodox and non-standard. The C standard requires that static storage duration variables are already initialized before main() is called. Also, you really don't want to do fundamental things like setting the stack pointer inside main(), because that would mess up all local variables you have in main().

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

When your OS runs a program your program needs to pass control over to it. And the OS only knows where to begin inside of your program at the main() function.

Vivek
  • 46
  • 3
0

Have you searched on the internet? Take a look in here, and also here.

When the operating system runs a program in C, it passes control of the computer over to that program ... the key point is that the operating system needs to know where inside your program the control needs to be passed. In the case of a C language program, it's the main() function that the operating system is looking for.

Community
  • 1
  • 1
pedromendessk
  • 3,538
  • 2
  • 24
  • 36
  • 3
    Well the C standard just says, in short, that `main()` is called on program startup (for *hosted* environments, see other answers). That's simple and fine, leaving many implementation details to the implementation. Your citation, however, tries to stay simple while introducing the *operating system* inappropriately. In the common case, operating systems just have their binary formats with their own ideas about an *entry point* and a little *C runtime* linked to your program will get control and take care of the execution of `main()`. Be careful with some online resources ;) –  Sep 15 '15 at 14:46
  • 1
    Seems quite likely that the OP doesn't even have an OS. – Lundin Sep 15 '15 at 14:55
0

Function main is special - your program begins executing at the beginning of main. This means that every program must have a main somewhere. main will usually call other functions to help perform its job, some that you wrote, and others from libraries that are provided for you.

You find it in every possible C book.

Michi
  • 5,175
  • 7
  • 33
  • 58
0

The main function allows the C program to find the beginning of the program. The main function is always called when the program is started.

00100100
  • 64
  • 4