Thread: void fuction, confusing haha

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    8

    Question void fuction, confusing haha

    What is the different between those main()? I quite confusing about it

    • void main (void)
    • void main ()
    • int main (void)
    • int main ()



    What is the function of void and int when starting, haha


    Thank You

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    the biggest thing that stands out to me is that void main is just simply wrong. int main is the only correct way to define main.

    apart from that, what do you think the difference is?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    "void main()" is simply not valid C, in either of the forms you mentioned. However, void indicates a function that returns no value. Yes, I know some books tell you main() can return void. Some compilers even support that. But it is not guaranteed with all compilers. Why? Because the C standard says all permitted forms of main() return int.

    The int in "int main()" means the function returns an integer to the caller (which in the case of main() means that the calling environment can retrieve that integral value, and use it as an error code or something). The way that value is retrieved, after main() returns, is implementation defined (i.e. it depends on your compiler, operating system, etc).

    When defining (aka implementing) a main() function, as in
    Code:
    #include <stdio.h>
    int main()    /*  definition of main()  */
    {
         printf("Hello World\n");
         return 0;
    }
    the () on main() means it accepts no parameters. The 0 on the return statement is the value that the caller can retrieve. In this context, int main() and int main(void) mean the same thing - that the function accepts no arguments.

    If you were declaring main() rather than defining (aka implementing) it, then you might do
    Code:
        int main();    /*   Invalid declaration of main() */
    or
    Code:
        int main(void);    /*   Valid declaration of main() - has to be defined elsewhere */
    In this case (when declaring but not defining main()) the () means that the function can accept a variable number of parameters, while (void) still means it accepts no arguments. Since main() is not allowed to accept a variable number of arguments (again, that's the law, as laid down by standard) only the second declaration here is valid C.

    In practice, it is very unusual for programmers (other than people who implement compilers and the associated libraries) to separately declare main(), since it is very rare that a programmer needs to write code that calls main() directly (and most programmers who think they need to are not understanding what they are actually doing).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    Thanks Elkvis, grumpy,
    i am new beginner from c programming, i doesnt know so much haha,
    mostly we use main() in the program and didnt care about int main() and other, because lecturer said main() is default, so that we use it haha. mostly i see programming book, they always use int main(). and doesnt know what function,
    i doesnt know why, mostly the book put void main()/int main() in coding there, i tap the code and run, dk why the program auto end.
    what is the function of return 0.
    thanks

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    grumpy the analysis that you have done about main according to its defining and declarations I think is the same for the other functions?

    Code:
      int foo( ); // Declaration of function of which has a variable number of parameters

  6. #6
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by Mr.Lnx View Post
    grumpy the analysis that you have done about main according to its defining and declarations I think is the same for the other functions?

    Code:
      int foo( ); // Declaration of function of which has a variable number of parameters
    This is correct; in C, function() means it can accept any number of parameters, function(void) means no parameters. In C++, function() means it accepts no parameters.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    I think you can denote that a function has a variable number of arguments with ellipsis.

    http://publications.gbdirect.co.uk/c...r9/stdarg.html

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    there is a difference between declaration and definition.

    consider this declaration:
    Code:
    int foo();
    it declares a function that returns an int and accepts an unknown number of arguments.

    now consider this function definition:
    Code:
    int foo()
    {
      return 0;
    }
    this defines a function that returns an int and accepts exactly zero arguments.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    So there is no problem if I declare a function :

    Code:
     int foo(void) ;
    and then define it like :

    Code:
     int foo() {
    
    // ...
    
    return some_integer_ value ;
    }
    but not vice versa
    Code:
     int foo() ; //declaration
    Code:
    int foo(void) {
    
    //...
    
    return some_integer_value; 
    }
    or

    Code:
     int foo() ;
    Code:
    int foo() {
    
    // ...
    
    return some_integer_value;
    }
    right?

    EDIT: Although I checked it with compiler and does not seem to complain even we use a () in declaration and after void in definition.
    Last edited by Mr.Lnx; 06-05-2013 at 10:58 AM.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    that is correct.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Mr.Lnx View Post
    Although I checked it with compiler and does not seem to complain even we use a () in declaration and after void in definition.
    that's right, because if you declare it as (), the compiler doesn't care what arguments you give it in the definition.

    Edit: if you declare it with arguments, the definition must match exactly those arguments.
    Last edited by Elkvis; 06-05-2013 at 11:08 AM.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    while its true that the various (void) and () declarations may be acceptable per the standard, it is bad form to have different definitions and declarations of a function (or variable, or anything for that matter). everything should match up exactly. otherwise it is sloppy programming.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Ok guys. Thank you.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Mr.Lnx View Post
    grumpy the analysis that you have done about main according to its defining and declarations I think is the same for the other functions?
    Yes, except for the fact that the standard specifies the acceptable forms of main(). The only constraints on acceptable forms for other functions is syntactic and semantic correctness.

    As Epy points out, C++ is a little different. A function declared with no arguments accepts no arguments, rather than a variable argument list.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by briankoh View Post
    What is the different between those main()? I quite confusing about it

    • void main (void)
    • void main ()
    • int main (void)
    • int main ()



    What is the function of void and int when starting, haha

    Thank You
    In ANSI C, main() always returns an integer to the environment, which can be 0 or EXIT_SUCCESS (almost always 0), to indicate that the program completed normally,, and EXIT_FAILURE to indicate that it encountered some sort of error.
    However many environments simply throw that integer away, so a lot of compilers will accept void main(). This is not portable, a really strict compiler won't accept it.
    main takes two parameters, argc and argv, which are the number of comm and line arguments passed to the program, and the arguments themselves. However it's not uncommon not to use any arguments. So main can be declared as void or with an empty parameter list to indicate this. There's a technical difference between the two, but it doesn't really matter.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a Bit of confusion (haha - Pun)
    By Junior89 in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2007, 11:22 PM
  2. Santa humor, haha
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-25-2002, 05:53 AM
  3. Your tech tales haha
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 10-03-2002, 01:19 PM