Shivanshu
Shivanshu

Reputation: 894

Function Calling and Defining with different number of arguments

#include<stdio.h>

void sum(int ,int);

int main()    
{
    sum(4,5,6);
    return 0;
}

void sum(int a,int b)
{
    printf("%d",a+b);
}

As function signature comprises of function name and parameters, so in above code snippet function calling and its definition are different signatures and must be error. But it is merely showing warning and displaying correct output and when I am writing definition before main function, it doesn't work and shows error. Why? Thanks in advance.

Upvotes: 2

Views: 139

Answers (2)

Petr Skocik
Petr Skocik

Reputation: 60058

You're calling a prototype function with the wrong number of arguments. That's constraint violation (6.5.2.2p2) and the behavior is undefined. A conforming compiler must diagnose a constraint violation, but it isn't obligated to fail the compilation. (My gcc, clang, and tcc all fail it, though.)

Upvotes: 2

O. Th. B.
O. Th. B.

Reputation: 1353

You are attempting to call the method sum with 3 arguments. It only takes 2 arguments.

Upvotes: 0

Related Questions