Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

What's the different between void main() and int main (void)??

leekeng81leekeng81 Posts: 27Member
I'm a new programmer here, in my class I have just learn the void main() statement, sometimes when I see you guys using int main(void), I couldn't get it, can someone explain it for me?

Secondly, I have written a program which pretty long [blue]FOR ME[/blue], but still have error with it, can I post the whole coding here for you guys to help me to check??

Comments

  • Eric TetzEric Tetz Posts: 2,141Member
    : I'm a new programmer here, in my class I have just learn the void main() statement, sometimes when I see you guys using int main(void), I couldn't get it, can someone explain it for me?

    void main() is not valid C or C++.

    According to the language standard (for both language) main must return int.
  • jackinCjackinC Posts: 7Member
    void function does not return any value
    If u r simply to print out and send ctrl back to main use void.
    However if u r 2 return a value use any other reqd returntype.

    Try it to explain urself:

    [blue]
    #include
    void print();
    void hello();
    void hi();
    void bye();

    void main()
    {
    print();
    hello();
    hi();
    bye();
    }
    void print()
    {
    printf("
    This is Me");
    }

    void hello()
    {
    printf("
    Hello, ");
    }
    void hi()
    {
    printf(" Welcome to Programmers Gallery");
    }

    void bye()
    {
    printf("
    C U Again");
    }
    [/blue]

    Consider another snippet:
    [green]
    #include
    int main()
    {
    int sqr(int);
    int a,b;
    printf("
    Enter a number");
    scanf("%d",&a);
    b=sqr(a);
    printf("
    The square of %d is %d",a,b);
    return 0;
    }
    int sqr(int x)
    {
    int c;
    c=x*x;
    return (c);
    }[/green]


  • pseudocoderpseudocoder Posts: 679Member ✭✭✭
    : : I'm a new programmer here, in my class I have just learn the void main() statement, sometimes when I see you guys using int main(void), I couldn't get it, can someone explain it for me?
    :
    : void main() is not valid C or C++.
    :
    : According to the language standard (for both language) main must return int.
    :

    [blue]Upon termination, your program needs to return its status to the host OS to signify whether it completed successfully or not. Although void main was ok in the C-89 standard, it was declared invalid in the C-99 standard I *think*, and most modern ANSI compliant compilers will warn you with void or simply main().

    int main() // ok for c++ void arguments are implied
    int main(void) /* required for C programs IIRC */
    int main(int argc, char *arv[]) // valid for either C or C++ and accepts command line arguments.[/blue]
  • MatthewDMatthewD Posts: 297Member
    : void function does not return any value
    : If u r simply to print out and send ctrl back to main use void.
    : However if u r 2 return a value use any other reqd returntype.

    This is not correct. See Eric Tetz's posting in response to the original question. main must return int.

    Matt.

  • stoberstober Posts: 9,765Member ✭✭✭
    : void function does not return any value
    : If u r simply to print out and send ctrl back to main use void.
    : However if u r 2 return a value use any other reqd returntype.
    :

    if you are using a very very old compiler such as Turbo C, you can get way with that. Modern compilers no longer support that construct without producing either a warning or an error.

    http://users.footprints.net/~kaz/cppvoidmain.html
    [code]
    4. The main function is required, by a diagnosable semantic rule, to
    have a return type of int. This is stated in clause 3.6.1 (Main
    function):

    [red][The main function] shall have a return type of type int, but
    otherwise its type is implementation-defined.[/red] Therefore, a program which
    declares main to return something other than int is an ill-formed
    program, because it violates a semantic constraint rule which is not
    accompanied by the words ``no diagnostic is required''. Upon
    processing the erroneous construct, the C++ implementation is required
    to emit a diagnostic, rather than behave in an undefined manner.
    [/code]

Sign In or Register to comment.