0

I recently found a bug in software related to the following warning:

warning: implicit declaration of function ‘my_func’ [-Wimplicit-function-declaration]

Despite the warning, the code was compiling and (most of the time) working.

My question is, what happens when the code is running and that function is called? Is that undefined behaviour, or can I assume that the function call is a no-op?

José D.
  • 4,175
  • 7
  • 28
  • 47
  • The behaviour is **not undefined**, in that the compiler is not given a leeway to do whatever it wants. Instead it **shall** issue a message telling that your program is **an invalid program**. Of course, when it comes to compiling an invalid program, all bets are off. – Antti Haapala -- Слава Україні Oct 26 '18 at 10:30

3 Answers3

0

This usually indicates that the header file that declares this subroutine was not #included. When a subroutine is used without having been declared, many C compilers generate an implicit declaration. This behavior is a common cause of errors, because the compiler may generate incorrect code if the implicit declaration does not match the actual definition.

This link contains a sample. It may be helpful to understand your concern.

Varun Jain
  • 1,371
  • 12
  • 26
-1

An implicitly defined function is assumed to return int and to take an arbitrary number of arguments. That may or may not be true for the real function.

Depending on your platform, the arguments may be passed in an unexpected way, e. g. in registers, via the stack, etc. And if the way of passing the arguments doesn't match the expectations, things go crazy.

glglgl
  • 89,107
  • 13
  • 149
  • 217
-2

The default function declaration in C has parameters of int.

So, if you do not declare the function, the compiler will assume it returns int and has all parameters of int.

However, you still need to define the function or the linker will give an error.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31