Hello everyone
I have seen many programs both int main() and void main() are used as the starting point of a program. I am just confused of when to use int main() and void main(). please someone explain me
Hi
It really depends on the system the program is run on.
But if your running this on a PIC, its a matter of programming style.
Your first example implies that the function "int main()" will return an integer value and accepts no argument.
However, proper C language grammar would be "int main(void)" to also indicate the function accepts no argument.
You would use this when the function "main()" returns a value. For example:
int main (void )
{
// do something;
return (0); <----here, a zero is returned
}
Your second example implies that the function "void main()" does not return a value and accepts no arguments
This statement should be written:
void main (void)
{
// do something
}
For the PIC microcontroller, it really doesn't matter since Main() is the only C program executed on startup and there's no subsequent program executed when main() is exited to read a return value from main(). However, other operating systems might be able to read a return value from main().
eT