what's the difference between void main(void) and int main(void) 
Author Message
 what's the difference between void main(void) and int main(void)

...What's the difference between void main(void) and int main(void)
and would I have to have int main(void) return a value?
  -Zane


Sun, 03 Nov 2002 03:00:00 GMT  
 what's the difference between void main(void) and int main(void)

Quote:

> ...What's the difference between void main(void) and int main(void)

int main(void) is valid c...
void main(void) is not.

Quote:

> and would I have to have int main(void) return a value?

yes, an integer.

Quote:

>   -Zane

--

Bryan Williams

  "Guns don't kill people, undefined behaviour
    (amongst other things) kills people.."



Sun, 03 Nov 2002 03:00:00 GMT  
 what's the difference between void main(void) and int main(void)

Quote:

>...What's the difference between void main(void) and int main(void)
>and would I have to have int main(void) return a value?

In the C language, a function does not have to return a value, if the
caller does not extract it.

In the case of main, returning without providing a value leads to an undefined
termination status. So it's better to return something, either
0, EXIT_SUCCESS or EXIT_FAILURE.

The difference between void main(void) and int main(void) is that
they are different function types, and entirely incompatible.

A C program is not required to work right if its main function does
not have one of these two types:

    int main(void) { /*...*/ }

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

The only variation you are permitted is the names of argc and argv,
which don't affect the function's type.

In some programs you may see old-style declarations like

    int main() { }
    main() { }  /* this one has an ``implicit int'' return value */

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

Of these, the variants that depend on ``implicit int'' are now considered
incorrect according to the 1999 C standard, which has finally banished
implicit int from the language. Prior to the approval 1999 C standard, the
above forms were still considered correct.

--
#exclude <windows.h>



Sun, 03 Nov 2002 03:00:00 GMT  
 what's the difference between void main(void) and int main(void)


Quote:
> A C program is not required to work right if its main function does
> not have one of these two types:

>     int main(void) { /*...*/ }

>     int main(int argc, char **argv) { /*...*/ }

> In some programs you may see old-style declarations like

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

Kaz, has ANSI finally banished the old-style function definitions?  Or, are
they still around (and deprecated, one would hope)?

--
poncho



Sun, 03 Nov 2002 03:00:00 GMT  
 what's the difference between void main(void) and int main(void)

Quote:

> Kaz, has ANSI finally banished the old-style function definitions?  Or, are
> they still around (and deprecated, one would hope)?

They're still around but deprecated.  From the C99 final draft:

       6.9.4  Function definitions

       [#1] The use of function definitions with separate parameter
       identifier   and  declaration  lists  (not  prototype-format
       parameter type and identifier declarators) is an obsolescent
       feature.



Mon, 04 Nov 2002 03:00:00 GMT  
 what's the difference between void main(void) and int main(void)

Quote:

> ...What's the difference between void main(void) and int main(void)
> and would I have to have int main(void) return a value?
>   -Zane

void main(void) is not valid C, while int main(void) is.
The standard requires that main() return an int, but doesn't require
that main()'s caller evaluate the return value.

If main() returns a value, and it's caller expects one, then the
caller can use the return value.
If main() returns a value, and it's caller doesn't expect one, then
the caller can ignore the return value.

However,

If main() doesn't return a value, and it's caller expects one, then
the caller gets garbage and might do something unexpected.
If main() doesn't return a value, and it's caller doesnt expect one,
then the caller can proceed as per normal.

The only failure condition of the four is where main() doesnt return a
value, and the caller expects one. When main() returns a value, either
the effect is beneficial or benign. When main() doesnt return a value,
the effect is benign or failure.

Conclusion: main() needs to return a value, even if it's caller
ignores it.

--
Lew Pitcher

Master Codewright and JOAT-in-training



Tue, 05 Nov 2002 03:00:00 GMT  
 what's the difference between void main(void) and int main(void)

Quote:

>...What's the difference between void main(void) and int main(void)
>and would I have to have int main(void) return a value?
>  -Zane

For practical use, returning a value is good in shell scripting.  You
could say return 1 on failure and 0 on success then do an "if" on the
compiled executable itself in your shell script (in a Unix shell script
or an MS-DOS batch file).


Sat, 09 Nov 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

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

2. main() vs int main() vs int main(void)

3. What's the difference: int main(void) VS void main(void)?

4. main(void) vs void main ()

5. newbie question....int main, void main, main

6. int main() vs int main(void)

7. int main vs. void main

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

9. void main or int main?

10. int main() OR void main()...

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

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

 

 
Powered by phpBB® Forum Software