C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
What's the different between void main() and int main (void)?? Posted by leekeng81 on 23 Aug 2004 at 8:17 AM
I'm a new programmer here, in my class I have just learn the void main() statement, sometimes when I see you guys using int main(void), I couldn't get it, can someone explain it for me?

Secondly, I have written a program which pretty long FOR ME, but still have error with it, can I post the whole coding here for you guys to help me to check??
Report
Re: What's the different between void main() and int main (void)?? Posted by Eric Tetz on 23 Aug 2004 at 8:19 AM
: I'm a new programmer here, in my class I have just learn the void main() statement, sometimes when I see you guys using int main(void), I couldn't get it, can someone explain it for me?

void main() is not valid C or C++.

According to the language standard (for both language) main must return int.
Report
Re: What's the different between void main() and int main (void)?? Posted by pseudocoder on 23 Aug 2004 at 10:45 AM
: : I'm a new programmer here, in my class I have just learn the void main() statement, sometimes when I see you guys using int main(void), I couldn't get it, can someone explain it for me?
:
: void main() is not valid C or C++.
:
: According to the language standard (for both language) main must return int.
:

Upon termination, your program needs to return its status to the host OS to signify whether it completed successfully or not. Although void main was ok in the C-89 standard, it was declared invalid in the C-99 standard I *think*, and most modern ANSI compliant compilers will warn you with void or simply main().

int main() // ok for c++ void arguments are implied
int main(void) /* required for C programs IIRC */
int main(int argc, char *arv[]) // valid for either C or C++ and accepts command line arguments.

Report
Re: What's the different between void main() and int main (void)?? Posted by jackinC on 23 Aug 2004 at 8:47 AM
void function does not return any value
If u r simply to print out and send ctrl back to main use void.
However if u r 2 return a value use any other reqd returntype.

Try it to explain urself:


#include<stdio.h>
void print();
void hello();
void hi();
void bye();

void main()
{
print();
hello();
hi();
bye();
}
void print()
{
printf(" \n This is Me");
}

void hello()
{
printf(" \n Hello, ");
}
void hi()
{
printf(" Welcome to Programmers Gallery");
}

void bye()
{
printf(" \n C U Again");
}


Consider another snippet:

#include<stdio.h>
int main()
{
int sqr(int);
int a,b;
printf("\n Enter a number");
scanf("%d",&a);
b=sqr(a);
printf("\n The square of %d is %d",a,b);
return 0;
}
int sqr(int x)
{
int c;
c=x*x;
return (c);
}



Report
Re: What's the different between void main() and int main (void)?? Posted by MatthewD on 23 Aug 2004 at 11:06 AM
: void function does not return any value
: If u r simply to print out and send ctrl back to main use void.
: However if u r 2 return a value use any other reqd returntype.

This is not correct. See Eric Tetz's posting in response to the original question. main must return int.

Matt.

Report
Re: What's the different between void main() and int main (void)?? Posted by stober on 23 Aug 2004 at 12:24 PM
: void function does not return any value
: If u r simply to print out and send ctrl back to main use void.
: However if u r 2 return a value use any other reqd returntype.
:

if you are using a very very old compiler such as Turbo C, you can get way with that. Modern compilers no longer support that construct without producing either a warning or an error.

http://users.footprints.net/~kaz/cppvoidmain.html
4. The main function is required, by a diagnosable semantic rule, to 
have a return type of int. This is stated in clause 3.6.1 (Main 
function): 

[The main function] shall have a return type of type int, but
 otherwise its type is implementation-defined.  Therefore, a program which
 declares main to return something other than int is an ill-formed 
program, because it violates a semantic constraint rule which is not
 accompanied by the words ``no diagnostic is required''. Upon
 processing the erroneous construct, the C++ implementation is required 
to emit a diagnostic, rather than behave in an undefined manner. 





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.