Thread: newbie main(void)

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Unhappy newbie main(void)

    What's the diff between main(void) and main() ....they seem the same to me!!

    Also what does Return 0 mean??

    Remember i am a newbie to this stuff!!!

  2. #2
    Disagreeably Disagreeable
    Join Date
    Aug 2001
    Posts
    711
    >What's the diff between main(void) and main() ....they seem the same to me!!<

    I don't see a difference. With main(void) you're just explicitly saying something that in main() you are assuming...

    >Also what does Return 0 mean??<

    Functions in C either return a value (to the caller) or they don't.

    In main(), 0 is normally used to indicate to the operating system that the program executed and terminated properly. A non-zero value would imply something went wrong.


    God, I suck at explaining things...
    Last edited by Hillbillie; 03-30-2002 at 02:52 PM.

  3. #3
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134

    RE

    Hi ,

    int main(void) - meaning no parameters are passed to main function
    Return 0 is a value that is returned to main ie. int main expects a n integer to be returned to it.
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    void main(void)
    {
    }

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >void main(void)
    >{
    >}

    Function main should return an int.

    Code:
    int main ()
    {
        int retval;
    
        /* some other code */
        
        return retval;
    }

  6. #6
    Unregistered
    Guest

    Talking woohoo

    thanx to all!!

    you all rock!

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    There's a slight difference, in c but not c++, you can do

    Code:
    void f()
    {
         /* code */
    }
    
    int main(void)
    {
          f(100, 100);
          return 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    As nick rightly pointed out there is a difference in C between main(void) and main().

    main(void) explicitly tells that the function accepts no parameteres.

    main() or for that matter any function(), at the time of being called can accept any number of parameters. But you can't use them inside the function since neither the declaration nor the definition have provided for any stack for the variables.

    so in the following code


    int cfunc();

    void main ()
    {

    int x = 4;
    int y = 8;

    cfunc(x, y);
    }



    int cfunc()
    {

    return (printf ("I don't know how to access the stack of this function call \n");
    }

    if you want to know how is it possible, you should read some of the source files included, and look for the implementation of main.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > void main ()

    Pay no attention to the man behind the curtain. He has no idea what he's doing.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    No, your suppose to use the parameters this way.

    Code:
    int f();
    
    int main()
    {
        int n = f(5);
        printf("f(%d) = %d\n", 5, n);
        return 0;
    }
    
    int f()
    {
        int dummy;
        int* p = &dummy + 3;
    
        return *p + 1;
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, your suppose to use the parameters this way.


    code:--------------------------------------------------------------------------------
    int f();

    int main()
    {
    int n = f(5);
    Um. No. You are SUPPOSED to use parameters THIS WAY:

    int f( int myparam1, char *myparam2, etc etc )

    There is a reason you can specify the arguments to a function: It's easily readable and understandable by people using your code. No one wants to look at that horrible excuse for "programming". That is bad practice and I for one would in no way want to have to fix your screwed up code when you fail to do the simplest things correctly...

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM