I wrote the following:
#include <stdio.h>
int foo(int x, int y=2*x)
{
return y;
}
int main()
{
printf("%d\n",foo(5));
}
But I've compile-time error error: local variable ‘x’ may not appear in this context
But I'm expected that it will be OK, because 3.3.4/1:
In a function declaration, or in any function declarator except the declarator of a function definition (8.4),names of parameters (if supplied) have function prototype scope, which terminates at the end of the nearest enclosing function declarator.
The end nearest enclosing function declarator is }
, the point of declaration is immediately after int x
. So why it doesn't work?
This is not because of scope.
8.3.6/7
says thatand
8.3.6/9
:It makes sense to prohibit this, since the order of evaluation of function arguments is unspecified. What would the value of
y
be if2*x
would be evaluated before the argument ofx
?Does this snippet work for you?
you are sending one arg to foo function when you have written it taking 2 param this way it should work :