newbie question....int main, void main, main 
Author Message
 newbie question....int main, void main, main

What is the difference between these....?

int main ( )

void main ( )

main ( )



Wed, 27 Jul 2005 09:46:29 GMT  
 newbie question....int main, void main, main

Quote:

> What is the difference between these....?

> int main ( )

This is the correct way to declare main if you don't need command-line arguments.  You must return
an integer when you exit the function.  The process that started yours can check this return value
for some sort of status.  A common practice is to return zero upon success, and some sort of error
code upon failure.  In practice, however, program return values are almost never checked.  Still,
you must return one.

Quote:
> void main ( )

This is a common way to incorrectly declare main.  This compiles and, AFAIK, works perfectly okay in
Win32.  If you port your code to other platforms, however, (or possibly even to other compilers) you
may get errors from the runtime library.

Quote:
> main ( )

As an implicit return type of *int* is assumed, this is equivalent to the first declaration, but is
archaic in syntax and will generate a warning in most compilers, including, IIRC, VC6.  You should
explicitly declare the return type.

HTH



Wed, 27 Jul 2005 11:23:49 GMT  
 newbie question....int main, void main, main
Michael,  by default C functions return "int" integer values for functions.

So

    int main()

is the same as

    main()

because the compiler will assume an "int" return.

So if you do not a return a value in a function, then use:

    void main()

If you wish to compare this to other languages like VB,  think of a void
function as a SUB where you don't need to return a value versus a FUNCTION
where you return values.

You normally define what the return value for functions in C before it is
used otherwise the compiler will automatically assume "int" if it sees the
function name before it is declared.

For example:

void funcX()
{
    funcY();

Quote:
}

void funcY()
{

Quote:
}

In the above, the compiler will give you two errors. One because when it
first saw funcY(), it was not declared, and two because it assumed an int
return value and when it finally saw the void funcY() it is going to tell
you that there is a "mismatch" or "redefinition."

So the way you solve this in C (or in any language) is to declare the
"function prototype" before hand, like so:

void funcY();    // function prototype

void funcX()
{
    funcY();

Quote:
}

void funcY()
{

Quote:
}

Now, the compiler will know what "funcY()" really is when it finally sees it
used in funcX().

---


Quote:
> What is the difference between these....?

> int main ( )

> void main ( )

> main ( )



Wed, 27 Jul 2005 17:52:20 GMT  
 newbie question....int main, void main, main
SleazySt,

Thank you for the details!  You have provided exactly what I need!



Quote:

> > What is the difference between these....?

> > int main ( )

> This is the correct way to declare main if you don't need command-line

arguments.  You must return
Quote:
> an integer when you exit the function.  The process that started yours can

check this return value
Quote:
> for some sort of status.  A common practice is to return zero upon

success, and some sort of error
Quote:
> code upon failure.  In practice, however, program return values are almost

never checked.  Still,
Quote:
> you must return one.

> > void main ( )

> This is a common way to incorrectly declare main.  This compiles and,

AFAIK, works perfectly okay in
Quote:
> Win32.  If you port your code to other platforms, however, (or possibly

even to other compilers) you
Quote:
> may get errors from the runtime library.

> > main ( )

> As an implicit return type of *int* is assumed, this is equivalent to the

first declaration, but is
Quote:
> archaic in syntax and will generate a warning in most compilers,

including, IIRC, VC6.  You should
Quote:
> explicitly declare the return type.

> HTH



Wed, 27 Jul 2005 21:43:12 GMT  
 newbie question....int main, void main, main
...another fine example of a good response to a post....

Thank you Hector.


Quote:
> Michael,  by default C functions return "int" integer values for
functions.

> So

>     int main()

> is the same as

>     main()

> because the compiler will assume an "int" return.

> So if you do not a return a value in a function, then use:

>     void main()

> If you wish to compare this to other languages like VB,  think of a void
> function as a SUB where you don't need to return a value versus a FUNCTION
> where you return values.

> You normally define what the return value for functions in C before it is
> used otherwise the compiler will automatically assume "int" if it sees the
> function name before it is declared.

> For example:

> void funcX()
> {
>     funcY();
> }

> void funcY()
> {
> }

> In the above, the compiler will give you two errors. One because when it
> first saw funcY(), it was not declared, and two because it assumed an int
> return value and when it finally saw the void funcY() it is going to tell
> you that there is a "mismatch" or "redefinition."

> So the way you solve this in C (or in any language) is to declare the
> "function prototype" before hand, like so:

> void funcY();    // function prototype

> void funcX()
> {
>     funcY();
> }

> void funcY()
> {
> }

> Now, the compiler will know what "funcY()" really is when it finally sees
it
> used in funcX().

> ---



> > What is the difference between these....?

> > int main ( )

> > void main ( )

> > main ( )



Wed, 27 Jul 2005 21:48:25 GMT  
 newbie question....int main, void main, main


<snip>

Quote:
> > void main ( )

> This is a common way to incorrectly declare main.
> This compiles and, AFAIK, works perfectly okay in
> Win32.  If you port your code to other platforms,
> however, (or possibly even to other compilers) you
> may get errors from the runtime library.

More specifically, it is a non-standard extension in VC.

It is documented to compile the same as

int main()
{
    // some code
    return 0;

Quote:
}

But for other compilers, this is either not permitted or
invokes underfined behavior. It's generally considered
better to use int main().

Quote:
> > main ( )

> As an implicit return type of *int* is assumed, this is
> equivalent to the first declaration, but is archaic in
> syntax and will generate a warning in most compilers,
> including, IIRC, VC6.  You should
> explicitly declare the return type.

It's also forbidden by the newest C standard. As this
standard becomes implemented, expect this to fail to
compile in future.

As before, int main() is considered better.



Thu, 28 Jul 2005 00:00:59 GMT  
 newbie question....int main, void main, main

Quote:


> > void main ( )

> This is a common way to incorrectly declare main.  This compiles and, AFAIK, works perfectly okay in
> Win32.  If you port your code to other platforms, however, (or possibly even to other compilers) you
> may get errors from the runtime library.

In fact, the standard *requires* a diagnostic for use of void main(),
and VC is nonstandard in not doing so.  (Worse still, it assumes
void main() if you use int main() and don't include a return statement.)

--
Craig Powers
MVP - Visual C++



Mon, 01 Aug 2005 00:19:29 GMT  
 newbie question....int main, void main, main

Quote:
> [...]
> > void main ( )

> [...]  This compiles and, AFAIK, works perfectly okay in
> Win32. [...]

  <nitpicking>
    This happens to compile and work on VC and some
    other Win32 compilers (e.g. on BCB). It doesn't
    necessarily compile and work on all Win32
    compilers (e.g. I'd suspect Comeau C++ to reject
    that).
  <nitpicking>

  Schobi

--

I'm Schobi at suespammers org



Mon, 01 Aug 2005 22:40:07 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. main() vs int main() vs int main(void)

2. what's the difference between void main(void) and int main(void)

3. void main(void) or int main() ??

4. int main() vs int main(void)

5. int main vs. void main

6. void main() or int main()?

7. void main or int main?

8. int main() OR void main()...

9. void main() <-> int main()

10. void main() or int main()?

11. int main() or void main()

12. void main() v/s int main()

 

 
Powered by phpBB® Forum Software