Is void main(void) a standard or not? 
Author Message
 Is void main(void) a standard or not?

At this page http://www.*-*-*.com/ ~bs/bs_faq2.html#void-main do
Bjarne Stroustrup say that void main() is not and has never been ISO
standard for eitehr C nor C++. But I have sometimes also heard from
other sources, among other places here at google, that it is standrad
to write the main that way! I have also heard that the standard
recommends that functions that never accepts any arguments should have
"void" between the parenthesis but Bjarne Stroustrup nver use that in
his book!

I am a bit confused abaout this! I am interested to be able to follow
the standard and I do also sometimes work as a trainer in programming
using C and C++ and I would like to be true to my participoants!

Can someone tell me what is the truth? ; ) ... (and also argue why it
is the truth).

Jonny Andersson



Fri, 30 Jul 2004 17:09:54 GMT  
 Is void main(void) a standard or not?
Quote:

> At this page http://www.research.att.com/~bs/bs_faq2.html#void-main do
> Bjarne Stroustrup say that void main() is not and has never been ISO
> standard for eitehr C nor C++. But I have sometimes also heard from
> other sources, among other places here at google, that it is standrad
> to write the main that way! I have also heard that the standard
> recommends that functions that never accepts any arguments should have
> "void" between the parenthesis but Bjarne Stroustrup nver use that in
> his book!

Jonny,

main in c++ and c has always returned an int. main must be able to take
0 or 2  arguments
so:-

int main(int argc, char *argv[])
int main(void) // or in c++ int main()

are the only 2 standard versions of main. Every implementation is
allowed to define additional arguments to main, but these will be
platform specific, so not 100% portable

HTH

--
Adrian Cornish
AP



Fri, 30 Jul 2004 17:28:31 GMT  
 Is void main(void) a standard or not?
Please ignore the OT c++ replies, I thought the OP had cross posted.
Guess he didn't but you only ever find that out after you press send <G>

--
Adrian Cornish
AP



Fri, 30 Jul 2004 17:30:03 GMT  
 Is void main(void) a standard or not?

Quote:
> At this page http://www.research.att.com/~bs/bs_faq2.html#void-main do
> Bjarne Stroustrup say that void main() is not and has never been ISO
> standard for eitehr C nor C++. But I have sometimes also heard from
> other sources, among other places here at google, that it is standrad
> to write the main that way! I have also heard that the standard

Concerning C, in ANSI C1989 you could write main(void), but defining
anything without a type was implicit int. That is

x; definition

was equivalent to int x;

Also especially in main() you were not required to return any value at
all.

In ANSI C99 the implicit int was abolished so the standard definition is
int main(void).

Quote:
> recommends that functions that never accepts any arguments should have
> "void" between the parenthesis but Bjarne Stroustrup nver use that in
> his book!

In C++ empty parenthesis in a function declaration/definition means void,
that is

int main() is equivalent to int main(void).

In C empty parenthesis means no argument checking e.g.:

void f();

int main(void)
{
 f(5);

return 0;

Quote:
}

void f(const char *p) { return *++p; }

In C with the empty parenthesis in the declaration you say do not check
if i pass the correct arguments (=i am nuts!).

Another C example:

void g() { printf("Hello World"); }

int main(void)
{
 g(5, 4, "i am nuts!");

Quote:
}

will not give an error. In C++ that would give an error since the
definition would be equivalent to void g(void) { ... }

Quote:
> I am a bit confused abaout this! I am interested to be able to follow
> the standard and I do also sometimes work as a trainer in programming
> using C and C++ and I would like to be true to my participoants!

For C++ i suggest "The C++ Programming Language" 3rd Edition or Special
Edition by Bjarne Stroustrup (the creator of C++). For C i suggest "The C
Programming Language" 2nd Edition by Brian Kernighan - Dennis Ritchie

But do not read both the same time because you will get confused. I
suggest you learn C++ directly but if it you have to learn C too, learn C
first. Also keep in mind that C++ needs a lot of time to learn it all!
But it is not needed to know it all to start programming with it.

Quote:

> Can someone tell me what is the truth? ; ) ... (and also argue why it
> is the truth).

I hope that i helped.

--

Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.d2g.com
* Alternative URL: http://run.to/noicys



Fri, 30 Jul 2004 17:41:14 GMT  
 Is void main(void) a standard or not?

Quote:

> int main(int argc, char *argv[])
> int main(void) // or in c++ int main()

> are the only 2 standard versions of main. Every implementation is
> allowed to define additional arguments to main, but these will be
> platform specific, so not 100% portable

Strictly speaking int main(int argc, char *argv[]) is portable too.

--

Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.d2g.com
* Alternative URL: http://run.to/noicys



Fri, 30 Jul 2004 17:42:16 GMT  
 Is void main(void) a standard or not?

Quote:


> > int main(int argc, char *argv[])
> > int main(void) // or in c++ int main()

> > are the only 2 standard versions of main. Every implementation is
> > allowed to define additional arguments to main, but these will be
> > platform specific, so not 100% portable

> Strictly speaking int main(int argc, char *argv[]) is portable too.

Oops sorry, i rushed.

--

Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.d2g.com
* Alternative URL: http://run.to/noicys



Fri, 30 Jul 2004 17:45:29 GMT  
 Is void main(void) a standard or not?

Quote:
> At this page http://www.research.att.com/~bs/bs_faq2.html#void-main do
> Bjarne Stroustrup say that void main() is not and has never been ISO
> standard for eitehr C nor C++. But I have sometimes also heard from
> other sources, among other places here at google, that it is standrad
> to write the main that way! I have also heard that the standard
> recommends that functions that never accepts any arguments should have
> "void" between the parenthesis but Bjarne Stroustrup nver use that in
> his book!

Just a nitpick: We're not "here at google". We're at Usenet. Usenet is
a service - Google is a gateway to that service. We're not "at google"
any more than the cast of "Family Ties" live inside your Sony Super
Trinitron TV.

--

| 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' is the most beautiful word in the world."
   - John Nordberg



Fri, 30 Jul 2004 20:18:08 GMT  
 Is void main(void) a standard or not?

Quote:
>main in c++ and c has always returned an int. main must be able to take
>0 or 2  arguments
>so:-

>int main(int argc, char *argv[])
>int main(void) // or in c++ int main()

                   ^^^^^^^^^^^^^^^^^^^^
What's wrong with int main() in C?

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Fri, 30 Jul 2004 21:15:43 GMT  
 Is void main(void) a standard or not?

Quote:


> >main in c++ and c has always returned an int. main must be able to take
> >0 or 2  arguments
> >so:-

> >int main(int argc, char *argv[])
> >int main(void) // or in c++ int main()
>                    ^^^^^^^^^^^^^^^^^^^^
> What's wrong with int main() in C?

Having read the C standard on the topic in a bit more detail, absolutely
nothing <G>

I was trying to avoid the a possible confusion of
void foo();
is not the same as
void foo(void);
in C. But as you pointed out this does not apply to main().

--
Adrian Cornish
AP



Fri, 30 Jul 2004 21:48:32 GMT  
 Is void main(void) a standard or not?

Quote:

> At this page http://www.research.att.com/~bs/bs_faq2.html#void-main do
> Bjarne Stroustrup say that void main() is not and has never been ISO
> standard for eitehr C nor C++. But I have sometimes also heard from
> other sources, among other places here at google, that it is standrad
> to write the main that way!

Who are these sources?  It's about time to arrange an "accident"
for them.  `void main()' is not and never has been portable.


Sat, 31 Jul 2004 00:40:59 GMT  
 Is void main(void) a standard or not?

[...]

    IV> void f(const char *p) { return *++p; }

Are you sure about that?

[...]

Cheers,
Kevin

--
He that breaks a thing to find out what it is has left the path of
wisdom                                         -- Gandalf the Grey



Sat, 31 Jul 2004 00:58:25 GMT  
 Is void main(void) a standard or not?

Quote:

>At this page http://www.research.att.com/~bs/bs_faq2.html#void-main do
>Bjarne Stroustrup say that void main() is not and has never been ISO
>standard for eitehr C nor C++.

And you think that Bjarne is mistaken, so you came here for a second
opinion?

Quote:
>But I have sometimes also heard from
>other sources, among other places here at google, that it is standrad

There is no ``here at google''; Google is acting as a Usenet portal:
you are effectively on Usenet. That's where ``here'' is.


Sat, 31 Jul 2004 06:05:15 GMT  
 Is void main(void) a standard or not?

Quote:

> [...]

>     IV> void f(const char *p) { return *++p; }

> Are you sure about that?

Shame me. I should write:

char f();

int main(void)
{
 f(5);

return 0;

Quote:
}

char f(const char *p) { return *++p; }

I was after a party at the time. :)

--

Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.d2g.com
* Alternative URL: http://run.to/noicys



Sat, 31 Jul 2004 07:31:58 GMT  
 Is void main(void) a standard or not?
Quote:

> At this page http://www.research.att.com/~bs/bs_faq2.html#void-main do
> Bjarne Stroustrup say that void main() is not and has never been ISO
> standard for eitehr C nor C++. But I have sometimes also heard from
> other sources, among other places here at google, that it is standrad
> to write the main that way! I have also heard that the standard
> recommends that functions that never accepts any arguments should have
> "void" between the parenthesis but Bjarne Stroustrup nver use that in
> his book!

> I am a bit confused abaout this! I am interested to be able to follow
> the standard and I do also sometimes work as a trainer in programming
> using C and C++ and I would like to be true to my participoants!

> Can someone tell me what is the truth? ; ) ... (and also argue why it
> is the truth).

> Jonny Andersson

Hi Jonny,
        Actually the return type of the main is compiler
specific.There are a lot of compilers available for C.
Some compilers like Turbo C allows us to define main as void main() or
as int main().
But some compilers like gcc(GNU c compiler) assumes that main should
return an integer value.If we try to use void main() and compile it in
gcc it will give warning though it will compile it.
I think ANSI has recommended using int as the standard return type of
main()
I never use int or void before main while writing programs!!!!!!!!.


Sat, 31 Jul 2004 14:20:51 GMT  
 Is void main(void) a standard or not?

Quote:


<snip>

> > I am a bit confused abaout [the return type of main()]! I am interested to be able to follow
> > the standard and I do also sometimes work as a trainer in programming
> > using C and C++ and I would like to be true to my participoants!

> > Can someone tell me what is the truth? ; ) ... (and also argue why it
> > is the truth).

> > Jonny Andersson
> Hi Jonny,
>         Actually the return type of the main is compiler
> specific.

Whilst it is true that compilers are allowed to define their own
interfaces, *all* conforming hosted implementations *must* support int
main(void), int main(int argc, char **argv), and their equivalents, and
*no* conforming C implementations are required to support *any* other
kind of definition for main(). Therefore, it is reasonable to say that
the return type of main() is standardised as being int.

Quote:
> There are a lot of compilers available for C.
> Some compilers like Turbo C allows us to define main as void main() or
> as int main().

But all conforming hosted implementations must allow the int return type
from main(), so it makes no sense to use any other type - such usage
would be gratuitously non-portable.

Quote:
> But some compilers like gcc(GNU c compiler) assumes that main should
> return an integer value.

This is a perfectly reasonable assumption, supported by the Standard.

Quote:
> If we try to use void main() and compile it in
> gcc it will give warning though it will compile it.

No implementation is required to give a diagnostic for void main(). Some
/do/ give a diagnostic (as is their right), and some refuse to compile
it altogether (as is also their right).

Quote:
> I think ANSI has recommended using int as the standard return type of
> main()

Correct.

Quote:
> I never use int or void before main while writing programs!!!!!!!!.

Incorrect, if you want your code to port to C99. Implicit int is no
more.

Use int main(void), int main(int argc, char **argv), or an exact
equivalent.

--

"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



Sat, 31 Jul 2004 17:43:29 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

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

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

3. main(void) vs void main ()

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

5. from void (void*) to void (_cdecl*) (void*)

6. Purpose of void main (void) and Functions

7. void main(void) ????

8. void main(void) illegal??

9. void main(void)

10. void main(void)

11. void main(void)....revisited!

12. void main(void)

 

 
Powered by phpBB® Forum Software