3

consider the code below:

main()
{
    func();
    i = 8;
}
int i;
func()
{

}

How my main function can see and call func() but cannot use i which is also defined below main.

5
  • 3
    You are probably using a C89 compiler, implicit function declaration has been removed since C99.
    – Yu Hao
    Jul 30, 2015 at 15:26
  • 1
    I dunno about gcc, but clang 3.6 even running in -std=c99 or -std=c11, will emit a warning, not an error, upon encountering undeclared function invoke regarding implicit declaration. Ironic the warning tells you the implicit declaration is invalid in C99 or later, yet it still allows compilation to continue. Wish it would puke-by-default when in C99 or later mode.
    – WhozCraig
    Jul 30, 2015 at 15:42
  • no I am using c99 compliant compiler
    – deshmanth
    Jul 30, 2015 at 15:43
  • 1
    @user5174296: Did it warn you about the missing types on your function definitions? If not, it's not a C99 compliant compiler. (Most compilers are not fully conforming by default; for example, gcc isn't, but gcc -std=c99 -pedantic is (mostly).) Jul 30, 2015 at 15:47
  • yes it warned me about not declaring a function prototype
    – deshmanth
    Jul 30, 2015 at 15:50

4 Answers 4

5

Quick answer: Don't do that. All functions and variables should be explicitly declared before use. Earlier versions of C let you get away with implicit declarations, but you shouldn't take advantage of that.

In C89/C90 (sometimes called "ANSI C", but that's not strictly accurate), if you call a function with no visible declaration an implicit declaration is created, assuming the function returns int and takes arguments of the (promoted) types that you passed in the call. There are no such implicit declarations for objects.

Depending on this rule is a bad idea. If the function actually returns a type other than int, or if its parameters don't match the arguments passed in the call, the behavior is undefined, and the compiler is not obligated to tell you that you've made a mistake.

All called functions should have an explicit declaration before being called and that declaration should be a prototype, specifying the types of the parameters.

The 1999 ISO C standard dropped the "implicit int" rule, making it illegal (a constraint violation) to call a function with no visible declaration. (Unfortunately the declaration isn't required to be a prototype, but you should always use prototypes anyway.) Many compilers still permit calls using the older rules. You should find out how to get your compiler to at least warn about such calls.

Here's a corrected version of your program, valid in C89/C90, C99, and C11:

void func(void);

int i;

int main(void)
{
    func();
    i = 8;
}

void func(void)
{

}

Or you can simply move the definition of func above the definition of main ("forward declarations" aren't generally necessary unless you have recursive calls):

void func(void);

int i;

int main(void)
{
    func();
    i = 8;
}

void func(void)
{

}
6
  • I wouldn't say it's unfortunate that forward definitions are allowed, instead of prototypes. static inline functions defined in header files can be useful in C, not just C++. Requiring prototypes too would add clutter. Having the standard enforce every aspect of good programming practices isn't always helpful. Jul 30, 2015 at 16:00
  • @PeterCordes: A prototype is a function declaration that specifies the types of the parameters. static inline double func(double); is a prototype; static inline double func(); is not. I see no good reason (other than backward compatibility with pre-ANSI C) to permit the latter. Jul 30, 2015 at 16:03
  • @KeithThompson: ahh, I see what you mean now. I forgot about non-prototype forward declarations that weren't definitions. Yes, those suck. Jul 30, 2015 at 16:07
  • @PeterCordes: Non-prototype declarations are no better if they're part of definitions. double func(){} defines a function with no parameters, but doesn't require incorrect calls to be diagnosed. Jul 30, 2015 at 16:16
  • 1
    @PeterCordes: Old-style non-prototype function definitions and declarations have been officially "obsolescent" since C89, which theoretically means that they could be removed in a future standard. I'm not optimistic that that will ever actually happen. Jul 30, 2015 at 17:21
4

In C, calling an un-declared function triggers some rules for making up a the types of the return value and arguments. This is a terrible idea, and you should always enable compiler warnings that catch this mistake. You will get breakage when passing or returning a floating point value instead of an int, or when passing a pointer wider than an int.

Even better, have your compiler in C99 or later mode, not C89. Implicit declaration was removed in C99, so you need at least a prototype.

There are no such rules for using global variables without a forward declaration.

2
  • is there any standard specification written regarding this
    – deshmanth
    Jul 30, 2015 at 15:32
  • Regarding what the rules are for implicit declarations? Yes, I'm sure the rules are fairly well-defined and not implementation-dependent. I don't know what they are, though. It's always better to declare your functions that to muck around with implicit declarations. Sorry, I don't have the C standards at my fingertips. You sound like you really want to make use of this C89 mis-feature. It's going to be hard to come up with a reason that outweighs how bad an idea this is, since there's so much potential for breakage on x86-64 when int isn't as wide as pointers. Jul 30, 2015 at 15:37
3

In K&R C, functions are implicitly declared returning int and accepting any arguments when they are used without being declared.

Omitting the return type also defaults to int as return type, so the definition does not conflict with the implicit declaration.

3
  • but when I make return type for func as void instead of int it still works no conflict is arised
    – deshmanth
    Jul 30, 2015 at 15:28
  • @user5174296: there's probably another separate compiler warning for that. I guess a compiler could accept that with a warning, rather than an error. Jul 30, 2015 at 15:29
  • The implicit function declaration rule exists in C89/C90, not just K&R C. That and the implicit int rule were dropped in C99. Jul 30, 2015 at 15:38
2

When the source file is parsed top-down, compiler needs to see the declaration for all identifier before their use. The "implicit int" rule for functions without an explicit return type is valid only in C89/90. It has been removed from the standard in C99. So the func() needs an prototype in C99 and later. If you are compiling in C89/90 mode then there's no error for the function func(). GCC generates the following warnings in C99 mode for your code (without the statement i=8):

$gcc -Wall -Wextra -std=c99 file.c

warning: implicit declaration of function func
warning: return type defaults to int

No such implicit implicit declaration is performed for the variable i even in C89/90. One exception is function parameters.

For example,

int main(i)
{
    func();
    i = 8;
}

is OK because i defaults to type int in C89/90. But it's not valid in C99 and later.

For your program to be valid in modern C, it should be:

void func(); // (1) Prototype for func() 

int main(void)  // (2) return type of main() should be int
{
    func();
    extern int i; // (3) refers to the `i` tentatively defined after main()
    i = 8;
}
int i;
void func()
{

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.