简体   繁体   中英

how to clean the implicit declaration of function in C?

I have a question regarding to the function declaration:

I declared a function in bc

//b.c
void getNumber();

//common header
common.h

In ac I use it like this: //ac

#include "common.h"
void getInfo()
{
   getNumber();
}

but it complain the getNumber is implicit declaration of function, what is the reason?

add the following line to common.h

void getNumber();

the bc file should contain the function definition in this way

void getNumber() {
    ....
}

You need to declare or define your function first before using it.

If your declaration void getNumber(); was read by the compiler after its first use ( void getInfo() ), you would get the warning warning: implicit declaration of function 'getNumber' . This happened because the compiler, when first encountering getNumber in void getInfo() had to guess its return type - hence the warning. I think this is what you are doing even though your example code does not show it that way.

If your void getNumber(); was read by the compiler first, no warning would occur.

Putting your function declaration in a header file like is a good idea. Be sure to include "common.h" first before your getInfo() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM