Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why func() and func(void) are different [duplicate]

Tags:

c

function

I have a function, can write in 2 ways.

void function(void) {
        // operations....
}

and

void function() {
       // operations......
}

Both functions are of same prototype. Why we have to mention void as argument at function definition?

like image 383
JEM Avatar asked Feb 04 '26 17:02

JEM


1 Answers

No, both have different prototypes.

Compile the below programs you will understand.

void function1(void)
{
   printf("In function1\n");
}

void function2()
{
   printf("In function2\n");
}

int main()
{
   function1();
   function2(100); //Won't produce any error
   return 0;
}  

Program 2:

 #include <stdio.h>
 void function1(void)
 {
    printf("In function1\n");
 }

 void function2()
 {
    printf("In function2\n");
 }

int main()
{
    function1(100);   //produces an error
    function2();
    return 0;
}
like image 187
gangadhars Avatar answered Feb 06 '26 07:02

gangadhars