8
#include <stdio.h>

void main()
{
    m();
}

void m()
{
    printf("hi");
}

Output

hi

Warnings

main.c:11:10: warning: conflicting types for 'm' [enabled by default]
     void m()
          ^
main.c:7:9: note: previous implicit declaration of 'm' was here
         m();
         ^

Why this program runs successfully even though m() is called before it is defined? And what is the meaning of the warning?

bolov
  • 72,283
  • 15
  • 145
  • 224
Pankaj Mahato
  • 1,051
  • 5
  • 14
  • 26

6 Answers6

11

C89 allow this by implicitly converting the return type of function and parameter passed to it to int. See here.

But, this is not valid in C99 and later. This has been omitted from the standard. Either you have to declare a prototype for your function or define it before main. See the result here. There is a compile time error in this case.

haccks
  • 104,019
  • 25
  • 176
  • 264
9

If you don't declare a function in K&RC/C89, it's implicitly declared as returning int. Since yours returns void there's a mismatch.

If you add a prototype:

void m(void);

...before it's called, it'll fix things.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • I understand that, but why is the code running? Shouldn't there be compile time error? – Pankaj Mahato Jan 31 '14 at 18:59
  • @PankajMahato: At least if memory serves, no. If you try to use the mismatched return type, you get undefined behavior, but since you're ignoring it, you probably even have defined behavior. – Jerry Coffin Jan 31 '14 at 19:03
5

No, but you can declare it:

#include <stdio.h>

// declare m()
void m();

void main()
{
    // use m()
    m();
}

// define m()
void m()
{
    printf("hi");
}
ichramm
  • 6,437
  • 19
  • 30
3

function declaration needs to be add before the first call of the function. A full declaration includes the return type and the number and type of the arguments. This is also called the function prototype.

So you are Missing function prototype.

Add function declaration as void m(); to the code.

Edit:

C program allow to use forward declaration .

In your case void m(); represents forward declaration of a function and is the function's prototype. After processing this declaration, the compiler would allow the user to refer to the entity m in the rest of the program.

Definition for a function must be provided somewhere (same file or other, where it would be responsibility of the linker to correctly match references to particular function in one or several object files with its definition, which must be unique, in another): (From wikipedia page)

That is why defining function after main work in your program.

Community
  • 1
  • 1
sujin
  • 2,813
  • 2
  • 21
  • 33
2

Yes - It is called using prototypes.

I.e. put

void m();

At the start of the file

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

No, you cant normally.
The code would print the error something like this if the function is not defined before invoking it:

s.c: In function 'main':
s.c:4:1: warning: implicit declaration of function 'm' [-Wimplicit-function-declaration]
    4 | m();
      | ^
s.c: At top level:
s.c:8:6: warning: conflicting types for 'm'
    8 | void m(){
      |      ^
s.c:4:1: note: previous implicit declaration of 'm' was here
    4 | m();
      | ^

So, it is always a good practice to declare method before or inside of main method especially if you are defining a method outside the scope of main function. The code snippet for best practice has been provided below:

#include <stdio.h>
void m();

void main()

{

m();

}

void m()

{

printf("hi");

}
Abhishek
  • 546
  • 5
  • 13