Reputation: 1873
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?
Upvotes: 4
Views: 2875
Reputation: 64
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.
Upvotes: 0
Reputation: 215350
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:
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().
Upvotes: 1
Reputation: 5307
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.
Upvotes: 0
Reputation: 123598
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.
Upvotes: 6
Reputation: 3646
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.
Upvotes: 0
Reputation: 46
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.
Upvotes: 0
Reputation: 50912
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.
Upvotes: 3