Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between int main() and int main(void) in C.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 611
    Comment on it

    Consider definition of int main():

    int main()
    {
       return 0;
    }

    Consider definition of int main(void):

    int main(void)
    {
       return 0;
    }

    The above two definition work perfectly fine in C but the difference between two is int main() can be called with any number of arguments but int main(void) specifies main need to be called without any argument. This is true not for main but all the function defined in C i.e the function defined in C whose signature did not specify any argument can be called with any number of argument or without any arguments.

    Consider following two programs:

    1.

    void fun() {  } 
    int main(void)
    {
        fun(10, "demo", "first");
        return 0;
    }

    This program will compile in C because fun() does not specify any parameters therefore can be called with any number of parameters or without any parameters.

    2.

    void fun(void) {  }
    int main(void)
    {
        fun(10, "demo", "first");
        return 0;
    }

    This program will  fail in compilation in C because fun() signature specify void as parameter therefore need to be called without any parameters.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: