void or int for main?:)

This is a discussion on void or int for main?:) within the C Programming forums, part of the General Programming Boards category; Hi frequently I see mentioned the fact that we should use int main () and not void main().....the trouble here ...

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

    void or int for main?:)

    Hi
    frequently I see mentioned the fact that we should use int main () and not void main().....the trouble here is that some of us here are doing a distance learning course in c programming....where the course provider frowns upon and deducts marks for using int main ()....unless we are passing a value back to the opsys. I personally use int main ()....cause DevC++ wont allow void main and numerous times I have returned exercises...forgetting to change back to void main() after pasting from the compiler...I now have TurboC++ which allows use of void main....
    The point is when people constantly correct newbies who use void main (), think about what there teacher might advocate as correct or not correct. I don't want to start a great debate as to which is correct or not....just to think on which may be correct for the particular post.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > where the course provider frowns upon and deducts marks for using int main ()
    If they can't get page 1 right, I have little confidence that they'll be able to teach you anything else. Sure they'll be able to teach you how to program in one flavour of C, but you'll learn some very bad habits doing that. I know from experience, I learnt a dialect 'C' first, then I wised up and decided to learn 'C' properly (something you can reasonably expect to work on all compilers).

    Just ask them what fflush(stdin) does - you'll be able to tell if it's worth sticking with them from their answer....

    Just one bad habit that you'll learn is that this does something useful...

    > unless we are passing a value back to the opsys
    You always pass back a value - usually 0, indicating success...
    You're telling me that this isn't a proper value???

    If your teacher has a problem with this, ask them to drop by.
    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
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok this has been one that I've been wondering about also. I read the FAQ and got an understanding of when to use VOID and when to use INT.
    In the two compilers I use: Old as Dirt Turbo C, and DJGPP I have never put a VOID or INT on a function. Is this really a problem?

    Thanks

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    When you don't specify the return of a function, it assumes that it is an int function (I'm not sure if this is part of the ANSI standard...). So as long as you don't use functions that return things other than int, you'll be okay.

    It's good practice to use void for functions which don't return any value because that's less space the function has to reserve. So...
    Code:
    int foo() {return 0;}
    Should allocate memory for an int and copy a 0 to it (the 'int' is optional).
    Code:
    int foo() {return;}
    Will allocate memory for an int, but return with copying any value to that int, so it waste memory on a gibberish value.
    Code:
    void foo() {rerurn 0;}
    Should not allocate any memory for the return value... but still copy the 0 somewhere... I'm not sure what this will do, but it's prolly not good.

    Of course, just because something returns an int, doesn't mean it actually allocates memory for that int. The value might be returned in a register. Still, it has some place where the caller will look for a return.

    This is why it is wrong, wrong, wrong to use void main (). (This is DOS specific...) When any program unloads itself, it will return the value in register al, and declaring your program as void main isn't going to change the fact that it returns this value.

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    actually void main is terrible... and DevC++ is quite correct in not allowing it. i don't even think its allowed based on the ANSI/ISO standard is it?

    >I'm not sure if this is part of the ANSI standard...).

    AFAIK - its not.

    >void foo() {rerurn 0;}
    >int foo() {return;}

    however these are illegal in any case.

    i agree with Salem though.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    [quote]
    When you don't specify the return of a function, it assumes that it is an int function (I'm not sure if this is part of the ANSI standard...).
    [/code]
    The new standard c99 and c++ you arn't allowed to
    write code which looks like

    Code:
    #include <stdio.h>
    
    main()
    {
            printf("Hello World!\n");
            return 0;
    }
    It's very strange that your teacher would actually mark code having int main wrong.

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    his teacher needs a smacking...

    tell him to try using CodeWarrior for a day instead of MSVC... lets see how he likes an ANSI/ISO compliant compiler where for loops actually have scope and you can return pointers properly.

    God, it hurt the first time after VC(it should be banned) it allows anything.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  8. #8
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    well then again this is an old debate so maybe hes from the old school void main side...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi folks,
    just the kind of response to this that I was expecting. As I said I was not trying to start of the great debate. Salem I bow to your greater Knowledge, at the start I didnot understand the difference. Now I have progressed with my studies to understand what is happening with returning from main. When this situation does crop up, you can learn to adjust to using void main and int main appropriately. This is not a specific teacher who invokes this rule with the distance learning course (COMPUTEACH), it is adopted as standard with course material. I actually meant to take this up with them when I was down at an in house session recently. I will e-mail them and see what answer they come up with. After all they have been teaching programming and computer related subjects for over 40 years...there words not mine.
    I shall keep you all posted as to their reply. If you want to check out there credentials....www.computeach.co.uk where to find them.
    Last edited by bigtamscot; 09-25-2001 at 12:38 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  10. #10
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    I always use..

    Code:
    int main(void)
    It never get any errors doing this in VC++6.0 - if its wrong then I don't care it compiles and that's all that matters

  11. #11
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >int main(void)<

    this is the legal way to do it ya know

    void main(void) is illegal

    about VC is not quite ANSI/ISO compliant it allows anything to compile and what should compile won't.

    anyways,

    >if its wrong then I don't care it compiles and that's all that matters

    i sadly agree with this sometimes.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  12. #12
    Unregistered
    Guest
    Gee it was my understanding that functions that return ints always are declared as such.
    int main()
    {
    return 0;//<---note the returned int
    }
    Declaring it as void main() when it obviously returns a number just doesn't make any sense.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by Salem


    Just ask them what fflush(stdin) does - you'll be able to tell if it's worth sticking with them from their answer....

    Just one bad habit that you'll learn is that this does something useful...
    ... Yet something which won't work under all compilers, and which is explicitly undefined in ANSI C =]
    Last edited by The V.; 09-27-2001 at 12:31 AM.

  14. #14
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    Originally posted by no-one
    >int main(void)<

    this is the legal way to do it ya know

    void main(void) is illegal

    about VC is not quite ANSI/ISO compliant it allows anything to compile and what should compile won't.

    anyways,

    >if its wrong then I don't care it compiles and that's all that matters

    i sadly agree with this sometimes.
    It is legal? Well that's good then . Another thing I don't have to worry about. Anyway, standards are fine, but are a bit of a pipe dream - sure they sound good in theory, but in reality if you port your code, you'll have to change it one way or the other.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

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