Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

what is difference between int main () and void main ()

Status
Not open for further replies.

Parth86

Member
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
Code:
int main ( )
{
}
Code:
void main( )
{
}
 
Compiler specific.... For example on XC8 compiler PIC18 has to be int main() and pic 16 has to be void main()

I can't get my head rounf it as C18 used void main()...

Reason... On RTOS systems where programs can be run from other programs you can return an error if the program failed in anyway.

On MSDOS you had to return an int so the system knew you were done! otherwise any memory would never be returned and the system wooouulldd ssssttttaaaaarttt ttttooo sssloooowww...

Realistically the programmer should clean up, but they never did...
 
C18 is also void main ( )
{
}
But I thought C18 code was compatible with XC8?? So is this no longer true?

Also XC8 has return at the end of the code.

Seeing as C18 works with MPLAB X I might stick with that.
 
C18 is also void main ( )
{
}
But I thought C18 code was compatible with XC8?? So is this no longer true?

I don't think it ever was?, but differences are probably small, so it's worth making the effort instead of sticking with a long obsolete compiler.

I just checked the XC16 program I currently have loaded, that goes:

int main(int argc, char** argv) {}
 
just checked the XC16 program I currently have loaded, that goes:

int main(int argc, char** argv) {}

And here's another.... When could you possibly pass parameters to main when using a pic...
 
I don't think it ever was?, but differences are probably small, so it's worth making the effort instead of sticking with a long obsolete compiler.

I just checked the XC16 program I currently have loaded, that goes:

int main(int argc, char** argv) {}
Actually I thought XC8 was the same........But i might have been looking at XC16, I dont remember. I would change but XC 8 has no libs in it and the program for setting up dosnt support many of the chips I use (like 18f4685). or the 18f1330.

I have a book for 16 bit pics but its for the old compiler, I really dont want to learn a new one :(. I used to be really pro pics but these days I try and avoid them. Its a real shame as I like the datasheets for pics.
 
And here's another.... When could you possibly pass parameters to main when using a pic...
Isnt all this just because they tried to make it more ANSI?

I wish they had left HITECH alone and the IDE Hi Tech had! They seem to have mashed the compiler up. The docs are total rubbish for it, and unless your using the Kseries the thing for setting peripherals is useless. Writing your own libs is ok, but it shouldnt be that way. Why start making life harder?
 
I wish they had left HITECH alone and the IDE Hi Tech had! They seem to have mashed the compiler up. The docs are total rubbish for it, and unless your using the Kseries the thing for setting peripherals is useless. Writing your own libs is ok, but it shouldnt be that way. Why start making life harder?
Hitec was always low level... I actually prefer it that way...
 
Hitech IDE was great to use, I used to have the compiler and IDE but lost it in a HDD crash, not that it would work with alot of the pics out now.
 
What annoys me is the migration Doc. **broken link removed**

I started to use it a couple of weeks ago, it says "he MPLAB XC8 C compiler has replaced the MPLAB C18. This replacement compiler
is also ANSI compliant, includes a separate assembler and linker, and the same
peripheral library is supplied."
Utter rubish!

No idea why they have the doc on site with errors like that.
 
The XC8 compiler is much better... No need for a linker script and you can have large memory chunks without all the messing... Also the old interrupt shinanigans of C18 are gone....Yeah!!! I am porting all me old C18 stuff over to XC8..
 
Oddly enough in the XC8 compiler handbook it gives examples like

#include <xc.h>
void main(void)
{
PORTA = 0x00;
RA0 = 1;
PORTAbits.RA2 = 1;
}
Seems odd they dont follow the blank C file convention that MPLAB X uses with XC8! And why cant I insert code into the post here?? I went to insert and chose code, but nowt happens!
 
Seems odd they dont follow the blank C file convention that MPLAB X uses with XC8!
All that is just definitions written to the header file.... Its just like including the types.h some use it, others don't..
And why cant I insert code into the post here?? I went to insert and chose code, but nowt happens!
I always do it manually anyway, so I never know when it doesn't work
C:
#include <xc.h>
void main(void)
   {
   PORTA = 0x00;
   RA0 = 1;
   PORTAbits.RA2 = 1;
   }

See....
 
So how do you do it manually?
 
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
Code:
int main ( )
{
}
Code:
void main( )
{
}


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
 
So how do you do it manually?
HTML tags.....

upload_2017-4-10_8-25-11.png
 
object main (string A){return B;}
on PC platform A is to pass parameters in to the program like when you say "restart /L" in cmd.exe, B returns an object when program closes as mentioned
 
Yeah but this is a compiler built for a micro controller..............
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top