Reputation: 49
I am declaring the function with #define
before it is used in main but I still get:
implicit declaration of function fakultet...
The answer should be 6 ...
#include <stdio.h>
#define fakultet(x) ((x)>1?((x)*(fakultet(x-1))):(1))
int main(void) {
printf(fakultet(3));
}
Upvotes: 1
Views: 256
Reputation: 24788
I am declaring the function with
#define
You are actually declaring a macro, not a C function:
#define fakultet(x) ((x)>1?((x)*(fakultet(x-1))):(1))
Macros are expanded only once. Therefore the when you use fakultet
in your code:
printf(fakultet(3));
is expanded to:
printf(((3)>1?((3)*(fakultet(3-1))):(1)));
and the compiler does not find a C function with named fakultet()
. Therefore you get:
implicit declaration of function fakultet...
Upvotes: 6