void main() or int main()? 
Author Message
 void main() or int main()?

Hi all,
I've started to learn C language.
In my book (beginning C by Ivor Horton, Wrox press)
autor uses:

void main() {
...

Quote:
}

but not:

int main() {
...
return 0;

Quote:
}

As far as I thought C's main method
should return 0 at the end, and be integer.
But, my programs can be compiled, and work
w/o errors. (At least as I think). But compiler
(gcc on linux) shows a warnings that main isn't integer.
So, why it's works both ways, and if it should be integer,
why autor uses void? And, is there any rules for that at all?
Thanks,
John.


Sat, 12 Apr 2003 03:00:00 GMT  
 void main() or int main()?

Quote:

> Hi all,
> I've started to learn C language.
> In my book (beginning C by Ivor Horton, Wrox press)
> autor uses:

> void main() {
> ...
> }

> but not:

> int main() {
> ...
> return 0;
> }

> As far as I thought C's main method
> should return 0 at the end, and be integer.
> But, my programs can be compiled, and work
> w/o errors. (At least as I think). But compiler
> (gcc on linux) shows a warnings that main isn't integer.
> So, why it's works both ways, and if it should be integer,
> why autor uses void? And, is there any rules for that at all?
> Thanks,
> John.

You should get into the habit of using int main(), main() always returns
an int.

--Jason



Sat, 12 Apr 2003 03:00:00 GMT  
 void main() or int main()?
Thanks everyone!
I've just returned this 'good book' to local B&N store.
Also I've spend some time searching for other good book,
and believe it or not, he's is not alone! There is bunch of book's
which uses:
void main() or even main() w/o anything else. And all of them
say that  it teaches ANSI C, etc.
Also I've seen book from Herbert Schildt, and btw even he uses int
main()
in his examples :) (to Kaz).
Other 'ANSI C' book ended up with MFC somehow.
Yes, the world is difficult thing ...
Finally, I've bought "Practical C programming, 3rd ed." by Steve
Oualline, O'reilly press.
I have many O'reilly books, and all of them is truly good . (so far) So
I hope
I wont miss with this one.
And I wont buy any more books by Wrox press, that's it!. I've 4 more,
but in GTK+/GNOME
programming for example, there is an error in very first -"Hello World"
application!!!
Can you believe it?
Anyway...
Thanks again,
John.
Quote:

> Hi all,
> I've started to learn C language.
> In my book (beginning C by Ivor Horton, Wrox press)
> autor uses:

> void main() {
> ...
> }

> but not:

> int main() {
> ...
> return 0;
> }

> As far as I thought C's main method
> should return 0 at the end, and be integer.
> But, my programs can be compiled, and work
> w/o errors. (At least as I think). But compiler
> (gcc on linux) shows a warnings that main isn't integer.
> So, why it's works both ways, and if it should be integer,
> why autor uses void? And, is there any rules for that at all?
> Thanks,
> John.



Sat, 12 Apr 2003 03:00:00 GMT  
 void main() or int main()?

Quote:


> >Also I've seen book from Herbert Schildt, and btw even he uses int
> >main()
> >in his examples :) (to Kaz).

> Sure he does now, after about a decade of flaming.

:p

The book I got,
in preface autor says:
"The bad programmers should be shot." ... I like this style :)
John.



Sat, 12 Apr 2003 03:00:00 GMT  
 void main() or int main()?

Quote:

>> Hi all,
>> I've started to learn C language.
>> In my book (beginning C by Ivor Horton, Wrox press)
>> autor uses:
>You should get into the habit of using int main(), main() always returns
>an int.

Or better yet int main(void). On some embedded systems it would be a fatal
error to return from main(), thus we are told to use an extension and write
void main(void) as the entry point to our infinite loop. If you're on a
hosted system (i.e. Windows 2000, Linux, etc. and not a pump controller
running on an 8051) then stick with int main(void).

- Mark



Sun, 13 Apr 2003 09:45:50 GMT  
 void main() or int main()?

Quote:
>So, why it's works both ways,

By pure accident, not by design.

Quote:
>and if it should be integer, why autor uses void?

Because he is clueless.  I strongly recommend avoiding such books,
because, if the author couldn't get something as basic right, he can be
hardly expected to have gotten everything else right.

Quote:
>And, is there any rules for that at all?

Yes: main has to be defined as a function returning int.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 13 Apr 2003 09:51:26 GMT  
 void main() or int main()?

Quote:
>Or better yet int main(void).

Why is it better than int main()?

Quote:
>On some embedded systems it would be a fatal
>error to return from main(), thus we are told to use an extension and write
>void main(void) as the entry point to our infinite loop.

On a freestanding system, the function called at program startup does
not even need to be called main.  It's the implementation that decides
its name and interface.

That's why we implicitly assume a hosted implementation when discussing
about main.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 13 Apr 2003 10:43:02 GMT  
 void main() or int main()?

Quote:

>w/o errors. (At least as I think). But compiler
>(gcc on linux) shows a warnings that main isn't integer.
>So, why it's works both ways, and if it should be integer,

It works both ways on some compilers by fluke.

Quote:
>why autor uses void?

Because Ivor Horton learned C by reading Herbert Schildt. He's part of a
secret society of people who have invented their own version of C. They
don't actually program; they just write bad books.

Quote:
>And, is there any rules for that at all?

Yes, the rules are defined by the international standard for the C language.
It states clearly that the name of the startup function is main that its types
may be int main(void) or int main(int, char **) .

--
Any hyperlinks appearing in this article were inserted by the unscrupulous
operators of a Usenet-to-web gateway, without obtaining the proper permission
of the author, who does not endorse any of the linked-to products or services.



Sun, 13 Apr 2003 11:17:52 GMT  
 void main() or int main()?

comp.lang.c:

Quote:

> >Or better yet int main(void).

> Why is it better than int main()?

        [snip]

Quote:
> Dan

#include <stdio.h>
#include <stdlib.h>
void silly_example(void);

int main()
{
   silly_example();

Quote:
}

void silly_example(void)
{
   main(NULL, 3.14159, "Now is the time for all good men");

Quote:
}

Must be accepted by a conforming compiler.

Jack Klein
--
Home: http://jackklein.home.att.net



Sun, 13 Apr 2003 12:53:58 GMT  
 void main() or int main()?

Quote:


>>Or better yet int main(void).

>Why is it better than int main()?

See Jack Klein's response for the answer.

Quote:
>>On some embedded systems it would be a fatal
>>error to return from main(), thus we are told to use an extension and
>>write void main(void) as the entry point to our infinite loop.

Correct! But the crt0 or startup asm. files default to main as the jump to
label, I didn't want to confuse the issue further. For instance, ATI's
Nucleus PLUS RTOS calls Application_Initialize() to start, there is no main.

- Mark



Sun, 13 Apr 2003 03:00:00 GMT  
 void main() or int main()?

Quote:

>comp.lang.c:


>> >Or better yet int main(void).

>> Why is it better than int main()?

>    [snip]

>> Dan

>#include <stdio.h>
>#include <stdlib.h>
>void silly_example(void);

>int main()
>{
>   silly_example();
>}

>void silly_example(void)
>{
>   main(NULL, 3.14159, "Now is the time for all good men");
>}

>Must be accepted by a conforming compiler.

Can you provide an intelligent example? :-)

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 13 Apr 2003 03:00:00 GMT  
 void main() or int main()?

Quote:


> >why autor uses void?

> Because Ivor Horton learned C by reading Herbert Schildt. He's part of a
> secret society of people who have invented their own version of C. They
> don't actually program; they just write bad books.

And they also secretly introduce language into the C Standard itself
to cause some to believe that their version of C is truly Standard C.
It's true!   Step up to the tent and let the lovely lady tear your ticket
to see the freakiest latest addition to the C Standard introduced under
Cover of Darkness...

 - Larry Weiss



Sun, 13 Apr 2003 03:00:00 GMT  
 void main() or int main()?
as fas as i know  main() can return any thing. This is not obious that
it need to return only 0. i am not sure why author used viod instead of
any thing but it is not wrong. In C lang u can implement in your own
waywhat ever u want, C lang provide that facility.
When u compile the program and u ran the program if that program is
successfull then it will return 0 to OS no matter what u used whether 0
or viod in your C language program. But it is a good intension that use
int main() instead of void main().

-Prasad

Quote:

> Hi all,
> I've started to learn C language.
> In my book (beginning C by Ivor Horton, Wrox press)
> autor uses:

> void main() {
> ...
> }

> but not:

> int main() {
> ...
> return 0;
> }

> As far as I thought C's main method
> should return 0 at the end, and be integer.
> But, my programs can be compiled, and work
> w/o errors. (At least as I think). But compiler
> (gcc on linux) shows a warnings that main isn't integer.
> So, why it's works both ways, and if it should be integer,
> why autor uses void? And, is there any rules for that at all?
> Thanks,
> John.



Sun, 13 Apr 2003 03:00:00 GMT  
 void main() or int main()?

Quote:

> as fas as i know  main() can return any thing. This is not obious that
> it need to return only 0. i am not sure why author used viod instead of
> any thing but it is not wrong. In C lang u can implement in your own
> waywhat ever u want, C lang provide that facility.
> When u compile the program and u ran the program if that program is
> successfull then it will return 0 to OS no matter what u used whether 0
> or viod in your C language program. But it is a good intension that use
> int main() instead of void main().

> -Prasad

No, No, No, No.
    Any compiler that accepts void main() is using a non-standard
extension. In ASNI/ISO C, main can *only* return an int. If you define
main as returning a different type, then you code is not standard C, and
should not be posted to this newsgroup.

--
Clark S. Cox, III

http://www.whereismyhead.com/clark/



Sun, 13 Apr 2003 03:00:00 GMT  
 void main() or int main()?


Quote:
> as fas as i know  main() can return any thing. This is not obious that
> it need to return only 0. i am not sure why author used viod instead of
> any thing but it is not wrong. In C lang u can implement in your own
> waywhat ever u want, C lang provide that facility.
> When u compile the program and u ran the program if that program is
> successfull then it will return 0 to OS no matter what u used whether 0
> or viod in your C language program. But it is a good intension that use
> int main() instead of void main().

> -Prasad

typedef struct foo_
{
  char bar;
  double baz;

Quote:
}  Foo;

Foo main(void)
{
  Foo f;

  f.bar = 'a';
  f.baz = 3.14159;

  return f;

Quote:
}

$ gcc -Wall -W -O2 -ansi -pedantic what.c
what.c:8:  warning: return type of 'main' is not 'int'

[snip]

in ANSI C, main returns an int.
--

Quote:
> I have a dumb question: Are external static variables allocated storage in
> stack or the heap?

No.
        - Ben Pfaff


Sun, 13 Apr 2003 03:00:00 GMT  
 
 [ 24 post ]  Go to page: [1] [2]

 Relevant Pages 

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

2. void main or int main?

3. main() vs int main() vs int main(void)

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

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

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

7. int main() vs int main(void)

8. int main vs. void main

9. int main() OR void main()...

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

11. int main() or void main()

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

 

 
Powered by phpBB® Forum Software