Originally Posted by
ssharish2005
What happens when this is the case
Code:
int foo();
int main()
{
foo();
return 0;
}
int foo()
{
printf("I am foo");
}
In this case the foo is defines. Would that still interprert foo undefined? Or where u just speaking about the prototype.
ssharish2005
In C with the above code would be OK to call foo(3) or foo("blah", 47, 4.5), just as well as foo(). In C++, only foo() is accepted by the compiler.
Of course, in C, the compiler may moan about missing prototypes and such if you do this - and rightly so, I would say - but it doesn't stop old/clever code from abusing this rule - a particular example would be using "home-made variable number of arguments", something like this:
Code:
int sumit();
int main() {
printf("sum = ", sumit(2, 19, 23));
printf("sum = ", sumit(3, 19, 23, 42));
}
int sumit(int n, int a, int b, int c, int d, int e)
{
int sum = 0;
switch(n) {
case 5:
sum += e;
case 4:
sum += d;
case 3:
sum += c;
case 2:
sum += b;
case 1:
sum += a;
break;
default:
.... do some error handling ...
}
return sum;
}
[I haven't tested that code, but I believe it works.
--
Mats