Happy
Happy

Reputation: 49

Why does it say implicit declaration of function in main? C programming

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

Answers (1)

jfMR
jfMR

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

Related Questions