Reputation: 7
Function prototype is different than call and definition; still it does not give any error on Visual studio and gcc compiler
#include<stdio.h>
void print(); //prototype
void main()
{
print(2,2); //calling
}
void print(int a,int b) //definition
{
printf("\na=%d\tb=%d",a,b);
}
Upvotes: 0
Views: 404
Reputation: 40145
6.7.5.3 Function declarators (including prototypes) 14
An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.
void print();//prototype
will be considered a prototype such as having a no specified argument.
Upvotes: 0
Reputation: 5351
With the above declared function prototype,
you can pass any number of arguments to the function irrespective of the number of arguments it takes.
Like for example
#include<stdio.h>
void print();//prototype
void main()
{
print(2,2);//calling
print(3); // This would also work output a = 3, b = garbage value
print(4,5,6); // This would also work output a = 4, b = 5
}
void print(int a,int b)//defination
{
printf("\na=%d\tb=%d",a,b);
}
Note : If you don't want to pass any arguments to a function, it is advisable to use void like void print(void);
Upvotes: 1