void main(void) 
Author Message
 void main(void)

came across this, i.e.,'void main(void)', a few time, however, never
understand what does the 'void' inside the bracket do.

Is it the same as void main() ??

Please advise?



Tue, 27 Jan 2004 00:07:29 GMT  
 void main(void)

Quote:

> came across this, i.e.,'void main(void)', a few time, however, never
> understand what does the 'void' inside the bracket do.

> Is it the same as void main() ??

> Please advise?

First of all:

the return type of main is *always* int.
(man I get tired on this, every 10 minutes the same
thing).

taking a function prototype:
  int foo();
* to a C compiler this means:
   foo is a function which returns an int and
   nothing is said about the arguments.
* to a C++ compiler this means:
   foo is a function, which returns an int and
   takes no arguments

taking a function prototype:
  int foo( void );
* to a C compiler this means:
    foo is a function, which returns an int and
    takes no arguments
* to a C++ compiler this means:
    foo is a function, which returns an int and
    takes no arguments

HTH

--
Karl Heinz Buchegger



Tue, 27 Jan 2004 00:15:31 GMT  
 void main(void)
[Followups to comp.lang.c only]

Quote:

>  came across this, i.e.,'void main(void)', a few time, however, never
>  understand what does the 'void' inside the bracket do.

>  Is it the same as void main() ??

void main(void) and void main() both invoke undefined behavior in C
(and as far as I know, in C++ as well), so in that respect they are
not much different. For a bit more detail, see the C FAQ at
http://www.eskimo.com/~scs/C-faq/top.html

Maybe you really wanted to know the difference between
    void foo();
and
    void foo(void);
in a C program.
The difference is this: the former declares foo as a function _with
a fixed but unspecified number of parameters_ and returning void;
the latter is a prototype that declares foo as a function _taking no
arguments_ and returning void. The first type is allowed in C for
backwards compatibility with old code but is widely considered bad
style. Just use a prototype everywhere.
In a C++ program, the declarations mean exactly the same thing
AFAIK, but that's off-topic in comp.lang.c.

Gergo

--
cursor address, n:
        "Hello, cursor!"
                -- Stan Kelly-Bootle, "The Devil's DP Dictionary"



Tue, 27 Jan 2004 00:18:21 GMT  
 void main(void)

Quote:

> came across this, i.e.,'void main(void)', a few time, however, never
> understand what does the 'void' inside the bracket do.

It's not the void in brackets (parentheses actually) that's the
problem, but the void return type of main: main returns int! So

int main(void)

Now to the void in parentheses. It means that void doesn't expect
any arguments.

Quote:
> Is it the same as void main() ??

int main() tells the compiler that main takes an unspecified (but
fixed) number of arguments. The compiler will find out how many and
their type when main is called somewhere.
Note that this is true for all functions, not only main.
Also, remember that main returns int.

Tobias.



Tue, 27 Jan 2004 00:18:23 GMT  
 void main(void)

on comp.lang.c:

Quote:
> came across this, i.e.,'void main(void)', a few time, however, never
> understand what does the 'void' inside the bracket do.
> Is it the same as void main() ??
> Please advise?

PRECAUTION: void main(void), void main() and void main(int argc, char
*argv[]) are *ALL* illegal forms of main(). The return type (written
before the function name) *MUST* be int, not void.

On to your question...
No, it's technically not the same. void function(void) means that
the function shall never take any parameters of any kind, while
void function() is an obsolete pre-ANSI way of declaring functions
that can take an arbitrary number of parameters. On ANSI compilers,
this way of declaring functions is still supported, but deprecated.
Calling a function with the wrong number of parameters generates
undefined behaviour, if it even compiles in the first place.

So to be 100% ANSI-compliant, use int main(void) or int main(int
argc, char *argv[]).

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"I am looking for myself. Have you seen me somewhere?"
   - Anon



Tue, 27 Jan 2004 00:11:43 GMT  
 void main(void)

Quote:

> So to be 100% ANSI-compliant, use int main(void) or int main(int
> argc, char *argv[]).

That is, to be 100% ANSI-C compliant. In C++ int main() and int
main(void) mean the same thing.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)



Tue, 27 Jan 2004 00:25:44 GMT  
 void main(void)
Quote:

> came across this, i.e.,'void main(void)', a few time, however, never
> understand what does the 'void' inside the bracket do.

> Is it the same as void main() ??

Maybe (it depends upon context).

In C, a function declaration with no arguments -- say `func()' means
"`func' is a function that takes some fixed argument list, but we don't
know what. To specify that the declared function takes _no_ arguments,
you'd declare it `func(void)'. In C++ these two forms are synonomous.

In a function definition, the two forms are synonomous in either
language.

Of course, there's another problem, `void main(void)' (or for that
matter `void main()') is not standards-conforming, as `main' must return
`int'.[1]

You should _really_ read the following FAQs for more information (of
course, you should have done this before posting -- it's irresponsible
not to have done so -- but now's as good a time as any.

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

HTH,
--ag

[1] In C++, if you don't provide a return value for `main' an implicit
`return 0' is inserted by the compiler. In C not providing a return
value for main is a constraint violation.

--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)

--
If it's of it it's `its'.



Tue, 27 Jan 2004 00:22:13 GMT  
 void main(void)
[Followups to comp.lang.c]

Quote:

>  [1] In C++, if you don't provide a return value for `main' an implicit
>  `return 0' is inserted by the compiler. In C not providing a return
>  value for main is a constraint violation.

That's not true. In C89, falling off the end of main() returns an
undefined exit status. In C99, main() implicitly returns 0 if the
return type is compatible with int (that it, always), an unspecified
status otherwise (that is, never).

Gergo

--
If people concentrated on the really important things in life,
there'd be a shortage of fishing poles.
                -- Doug Larson



Tue, 27 Jan 2004 01:14:31 GMT  
 void main(void)
[Followups set to comp.lang.c]

<snip yet another void main thread>

Quote:

> [1] In C++, if you don't provide a return value for `main' an implicit
> `return 0' is inserted by the compiler. In C not providing a return
> value for main is a constraint violation.

Which constraint does it violate? I was under the impression that C99
allows for this, as the following reference illustrates:

5.1.2.2.3 Program termination
1 If the return type of the main function is a type compatible with int,
a return from the initial call to the main function is equivalent to
calling the exit function with the value returned by the main function
as its argument;10) reaching the } that terminates the main function
returns a value of 0. If the return type is not compatible with int, the
termination status returned to the host environment is unspecified.

Or were you talking about C90?

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Tue, 27 Jan 2004 01:20:36 GMT  
 void main(void)
Quote:

> [Followups to comp.lang.c]


> >  [1] In C++, if you don't provide a return value for `main' an implicit
> >  `return 0' is inserted by the compiler. In C not providing a return
> >  value for main is a constraint violation.

> That's not true. In C89, falling off the end of main() returns an
> undefined exit status. In C99, main() implicitly returns 0 if the
> return type is compatible with int (that it, always), an unspecified
> status otherwise (that is, never).

OOPS! Cerebral flatulence on my part.

Thanks,
--ag

--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)

--
He looked like hell, but the devil was smiling.



Tue, 27 Jan 2004 01:16:52 GMT  
 void main(void)
Quote:

> [Followups set to comp.lang.c]


> <snip yet another void main thread>

> > [1] In C++, if you don't provide a return value for `main' an implicit
> > `return 0' is inserted by the compiler. In C not providing a return
> > value for main is a constraint violation.

> Which constraint does it violate? I was under the impression that C99
> allows for this, as the following reference illustrates:

> 5.1.2.2.3 Program termination
> 1 If the return type of the main function is a type compatible with int,
> a return from the initial call to the main function is equivalent to
> calling the exit function with the value returned by the main function
> as its argument;10) reaching the } that terminates the main function
> returns a value of 0. If the return type is not compatible with int, the
> termination status returned to the host environment is unspecified.

> Or were you talking about C90?

Please see my response to Gergo. ;-(

--ag
--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)

--
A: Find a lwwyer who speaks Aramaic -- about copyright infringement.



Tue, 27 Jan 2004 02:06:10 GMT  
 void main(void)

Quote:

> came across this, i.e.,'void main(void)', a few time, however, never
> understand what does the 'void' inside the bracket do.

> Is it the same as void main() ??

Yes, void func(void) is a C'ish way of saying void func() in C++. A func
that takes no arguments, that is. C++ also allows the func(void) as a
func w/ no arguments, just so it can compile such a C line. Syntax
quirks, that's all.


Tue, 27 Jan 2004 02:37:30 GMT  
 void main(void)

Quote:


>>  [1] In C++, if you don't provide a return value for `main' an implicit
>>  `return 0' is inserted by the compiler. In C not providing a return
>>  value for main is a constraint violation.

>That's not true. In C89, falling off the end of main() returns an
>undefined exit status.

I don't have the standard handy this second, but falling off of
main in C89 is not == returning an undefined exit status.
I'm fairly certain that it is undefined behavior, period.

Quote:
>In C99, main() implicitly returns 0 if the
>return type is compatible with int (that it, always), an unspecified
>status otherwise (that is, never).

IOWs, in C99, functions, including main, can no longer have
an implicit int.  Furthermore, in C99, if you fall off of main,
the compiler implicitly adds a return 0; for you, though you
should probably but it in your self.
--
Greg Comeau                 Countdown to "export": December 1, 2001
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout

Tasty C99/C++ Combo: Dinkumware libs + Comeau Compilers == WOW!!!



Tue, 27 Jan 2004 04:05:58 GMT  
 void main(void)

Quote:

> came across this, i.e.,'void main(void)', a few time, however, never
> understand what does the 'void' inside the bracket do.

> Is it the same as void main() ??

> Please advise?

This had better not be a troll....

main() *NEVER* returns void.  Not ever.  Therefore, void main(void)
and void main() are *both* illegal constructs.  main() always returns
an int.

So I will now rephrase your question:  is 'int main(void)' the same as
'int main()'?  The answer is: not exactly.  The keyword "void" within
parentheses in a prototype means that the function takes no arguments;
an empty pair of parentheses means the same thing for a definition,
but in a function declaration, it means "I accept a certain number
(possibly zero) of arguments, I'm just not telling you how many".
Empty parentheses are not a prototype, and are now obsolescent - they
may not be supported in the future.

There are only two ways to prototype main:

int main(void)
int main(int argc, char *argv[])

They must match one of the above two, though any *equivalent*
declaration is also acceptable (e.g., 'int main (int c, char **v)').

Micah

------------

(Almost posted the above without noticing the lovely cross-post.  The
statements above were meant to apply to C - in C++, int main() is
precisely equivalent in every way to int main(void).  The void main()
stuff still applies, though, as does the demonstration of the only two
ways to declare main()).

--
"Just because Unix is the best operating system doesn't mean it is the
be-all and end-all of computer science. Err, or something."
                      - Joona I Palaste



Tue, 27 Jan 2004 04:14:24 GMT  
 void main(void)

Quote:

> [1] In C++, if you don't provide a return value for `main' an implicit
> `return 0' is inserted by the compiler. In C not providing a return
> value for main is a constraint violation.

Not any more, it isn't :(

Still, since the OP is unlikely to be using a C99 implementation
yet...

Micah

--
"Quite frankly, C isn't a good language for idiots and never will be.
Same for Ferraris, for that matter."
                      - Dann Corbit



Tue, 27 Jan 2004 04:16:57 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

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

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

3. Purpose of void main (void) and Functions

4. Is void main(void) a standard or not?

5. void main(void) ????

6. void main(void) illegal??

7. Is void main(void) ANSI standard??

8. void main(void)

9. void main(void)....revisited!

10. void main(void)

11. Who cares? void main(void)

12. The making of void main(void)

 

 
Powered by phpBB® Forum Software