why do we 'int main()'?

This is a discussion on why do we 'int main()'? within the C Programming forums, part of the General Programming Boards category; i read somewhere that int main is the rite way and not void main why is that ? where does ...

  1. #1
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99

    why do we 'int main()'?

    i read somewhere that

    int main is the rite way and not void main

    why is that ?
    where does the int get returned to from main??
    to the Operating system?

    and what does the os do with it?
    can someone tell me??

    jv

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > int main is the rite way and not void main
    This is true

    > why is that ?
    Because the standard says so
    http://anubis.dkuug.dk/JTC1/SC22/WG14/www/docs/n869/

    > to the Operating system?
    Yes.
    In DOS, its in the ERRORLEVEL variable

    In most unix/linux shells, its in $status

    In a unix/linux programming environment, you get the status with the wait() system call.


    > and what does the os do with it?
    Nothing much - but it is available to you, should you need to know.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    k got that now ill sleep well tonite

    thanx salem

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247

    Ye ole debate again

    I am sorry salem but I have to disagree with you !!?

    Why, because one of my tutors, who has umpteen letters after his name and is a professional programmer told me so.

    Do I beleive him? Well I tend to make my own mind up.

    If your compiler does both then I would go with the one that is standard across your course material. I have used DevC++, which doesnot allow void main ()...so I had to change my project work after pasting to print format from compiler. I now use TurboC++ and use void main (void)...although I could use int main ()..but then I would have to change the code and remove return 0; before posting.

    So I guess the answer is what ever suits yourself and your compiler..if aint broken why fix it....if you are fortunate enough to gain employment as a programmer then you will probably find that you will have to conform to your department standards when coding. This would include the way we declare variables i.e. uppercase usage, as well as rules for variable and function definitions. So if your teacher or tutor sets rules stick by them.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, the problem with void main() occurs when a program's return value matters. void main() will return a random value -- on an x86, whatever the EAX register happened to be at the end of main(). int main() returns a known value -- on an x86, it explicitly sets eax to the given return value.

    Now, if I wanted to call your program from my program, it might be *really nice* to know if your program succeeded or failed. It might be critical to know if your program successfully did its task. Without returning error values, my program would have to try to independantly verify the correct function of your program (not always even possible). With error values, it's a simple matter to know if your program failed, so mine could take appropriate action.

    Even programs that you wouldn't think would need to return values may surprise you. I wrote an editor for certain filetypes for a series of PC games. The return value told the caller various things: whether an error occured, whether the file edited was saved or discarded, whether the file exists or was a new file (in memory only).

    Someone later wrote a program that would edit many types of files for these games. He used several other people's utilities to do various portions of the editing work. Now, even though I never thought the return values would be used, someone was calling my program from another program (something I never really considered) and the return values + command line parameters really made a huge difference for this person.

    So, even if you DON'T think you need to return anything, at least return 0 for no error, nonzero for error. Maybe someone will find it useful.
    Last edited by The V.; 11-01-2001 at 01:59 PM.

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    If a C compiler has problems in accepting int main(void) then it is broken and should be thrown away , irrespective of whether a compiler supports void main() or not, every ansi-C compiler is bound to accept int main(void) .
    Anyway changing a void main program to a int main(void) program is usually a matter of minutes .

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > Without returning error values, my program would have to try to independantly verify the correct function of your program
    I actually had to do this once, with a program obviously written by a void main'er.

    All sorts of garbage was coming back in the return status, so I had to resort to parsing its output instead - icky or what....

    > Why, because one of my tutors, who has umpteen letters after his name and is a professional programmer told me so.
    Ok, then ask him whether any of these are OK as well?

    float main ( ) { return 0.0; }

    double main ( ) { return 0.0; }

    short main ( ) { return 0; }

    // this ones a doozy, if you can sneak it past the compiler
    typedef struct { int big[10000]; } bogus;
    bogus main( ) { bogus result; return result; }

    Ask him to explain why some or all of these are invalid.

    I mean, void main is just the start of the slippy slope. You can either choose to learn the language properly, or you can choose to learn dialect 'C'. But choose carefully, for if you choose the latter, you will find yourself learning it twice.

    Oh, and while you're at it, ask him what
      fflush( stdin );
    does?
    I'm sure we'll be able to tell what sort of tutor he is from his answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a fun one. Since this:

    char**argv

    is the same as:

    char*argv[]

    is the same as:

    char argv[][]

    Is this valid?
    Code:
    #include <stdio.h>
    int main ( int argc, char argv[2000000000][2000000000] )
    { return printf("Wheee!" ); }
    Compile it and be surprised.

    Ah, but those three are not the same? Really? Then why will it compile with all three of those as the value for argv? Just because it compiles, it doesn't make it working or valid code.

    Hell, this compiles:

    char *x;
    strncpy( x, "Hello. This is valid code!" );


    Quzah.

  9. #9
    free(me);
    Join Date
    Oct 2001
    Location
    Santo Domingo, DN, Dominican Republic
    Posts
    98

    The answer is simple...

    Such unsightly code compiles because C and C compilers assume that the programmers know what they are doing, which is in itself a good thing. But unfortunately programmers don't always know what they are doing.

    I believe that's one of the reasons for standards, so people will do what's right even if they don't really understand why.

    adios,
    biterman.
    Do you know how contemptous they are of you?

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    I knew I could stoke the fires with the
    " OLD DEBATE" on this forum.....

    Ofcourse it may matter whether you use int main() or void main()

    What I say that if you are at the basic learning stage...bothering about what gets passed back to the operating system will seem trivial compared to getting your compiler to accept your misuse of legal arguments and functions.

    Some windows compilers like MS Visual C++, will not let you use void main () and will insist you return a value back to the operating system. The ANSI standard allow you to use void main().

    If you are not returning a value to the operating system, there is no need to use int main and therefore it would be better to use void main. Void main uses less memory, hence it being the better option to use.

    I don't know why people in the chat rooms are saying that, but if you find out why e-mail me.
    Now if you wish to doubt these words, then you must have years experience using the c family of languages like the author, and spend your time lecturing uni grads, which may explain the reason you spend so much time on here. lol....
    Last edited by bigtamscot; 11-02-2001 at 04:43 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  11. #11
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Some windows compilers like MS Visual C++, will not let you use void main () and will insist you return a value back to the operating system. The ANSI standard allow you to use void main().
    LOL, skip the course, this guy can't read. As much as I hate to admit,

    Code:
    void main()
    {
    }
    compiles fine with Microsoft Visual C++.

    IT IS NOT ANSI STANDARD HOWEVER.

    I have some years of C/C++ experience. I have never lectured, but all I need to prove him wrong is an Ansi standard, or MSVC. Just because a farmer says cows are green doesn't mean you have to believe him... especially if it is easy to field at least one living example of the contrary.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  12. #12
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    And another thought:

    He spends his life telling people about it, I spend my life using it...

    How likely is it one of us is right ?

    I'm not saying I know it better... he probably has a lot of knowledge, but in this point he's wrong. You don't have
    to rely on my word, you can just go and test it.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > The ANSI standard allow you to use void main().
    This is an outright lie - the guy has obviously never read the standard.
    You have the link - go and read it - NOW!

    The other part of the statement is wrong - that's 2-0 to us.

    > Void main uses less memory, hence it being the better option to use.
    Wow! - And how does it use less memory?
    Saves a few characters in the source code?
    Saves a couple of instructions placing a proper return result where the OS can find it?

    > Now if you wish to doubt these words,
    You could say that

    > then you must have years experience using the c family of languages
    For sure

    > like the author
    From the face of it, has only ever read one book (written by another void mainer), and only ever used one compiler.

    Speaking of which, by the time you've used several you know what is valid and what isn't.

    > spend your time lecturing uni grads
    No, but I have had the mis-fortune of having to clear up the mess from these so-called experts. How someone can get through college and not know what an uninitialised pointer is is beyond me.

    > I don't know why people in the chat rooms are saying that, but if you find out why e-mail me.
    Copy the whole message thread while you're at it - maybe he'll read the link to the standard.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Sayeh
    Guest

    amazing

    Bear with me while I babble--

    I am probably one of the oldest members on this board (both in age, and the number of years I've been dropping by), and still this basic question is misunderstood by _so many_.

    Salem is, as usual, the most correct. However, let me educate you all (Salem, you probably know this) just a tad.

    First off, a standard is useful _after the fact_ in order to encourage an introduced/contrived conformity. Strength in unity.

    To understand why a value can or cannot be returned, you must first understand a little bit about the evolution of the language compiler. In the C language compiler, it was a natural to return a value from main(), simply because it might be useful to do so (and in some cases necessary). From the application standpoint, this does not mean it has to return something, just that it may.

    For example. In Mac programming, it is virtually unheard of to return a value from main() in an application. On almost all other platforms however, compilers expect something to be returned.

    Now that we know it can be useful to return a value, albeit unrequired in some situations, then let's examine the compiler's view.

    Most compilers were derived off of UNIX sourcecode, and ported to other platforms. In almost every case, in order to be ANSI compliant, a value is returned from main().

    WHETHER OR NOT YOU PROTOTYPE main() to do so.

    there are so many things going on behind the scenes that you are unaware of that these arguments are quite humorous.

    Usually, when your application gets built, if you don't return a value from main, the compiler includes a stub (aka 'glue') that automatically returns a value from main-- unknown and transparently to you. Only 'standard-alone code resources', specifically compiled as such, are exempt from this rule.

    The reason is simple:

    THE OPERATING SYSTEM EXPECTS A VALUE RETURNED TO IT AND IT MAKES SPACE ON THE STACK WHEN THE APPLICATION IS LOADED, LINKED, AND EXECUTED. IT EXPECTS TO BE ABLE TO RECLAIM THAT STACK SPACE WHEN THE APPLICATION IS DONE. FURTHERMORE THE O/S BRANCHES ON THE RETURN VALUE WITHOUT TESTING TO SEE IF ONE EXISTS-- IT EXPECTS IT TO BE THERE.

    No exeptions to the rule. Therefore, something MUST and WILL BE returned on the stack, even if it's just a zero value. then the O/S pulls the return value off the stack, cleans up the stack chain, disposes of the heap and goes on.

    In order to make the O/S code generic, and supportive of the standard, and ready for future changes, it fundamentally always expects a value. Even on the Mac. Mac compilers provide a glue so this isn't required by default.

    It is important to gain a broader understanding of how the pieces interrelate. The int main() .v. void main() argument can only be understood from the perspective of the O/S, not from the point of the application. The returned information is of no value to the application-- it has been disposed of. It is for the O/S' benefit.

    One of the greatest mistakes I see posters making on this board is to confuse 3 seperate entities:

    - the compiler
    - the processor
    - the language (and it's specification)

    These are 3 _different_ things. the compiler is not the language is not the processor. The compiler is an implementation of some percentage of the language standard. The fact that a compiler doesn't warn you if you don't return something from main() doesn't mean it doesn't matter. And THAT'S THE DIFFERENCE. Learn to understand what matters and what doesn't.

    The _smart_ thing is to follow the standard, regardless of platform, and return a value. Not only does this make you compliant and help you form good habits, but it also means if the O/S changes in the future, your application will still run, because the O/S developers will plan that everyone followed the standard.

    Don't be "clever", don't be "smart", don't assume you know things "oh so well"-- you don't. Do the _wise_ thing. The only reason I know so much is because I've been doing this since the beginning with many of the people who developed the tools and the standards you are working with. And I still find people way better than myself.

    You can never be faulted for doing the wise/prudent thing. That is the beginning of wisdom and knowledge.

    enjoy.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Just an observation ... isn't funny how people read something in a book and think it has to be true? ... well I guess if he wrote a book, he has to be an expert, and everything he writes is accurate.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21