3

I am actually working C language on Ubuntu 18.04. I don't use any IDE.

#include <stdio.h>

void main()
{
    message();
    printf("\nCry, and you stop the monotomy!\n");
}

void message()
{
    printf("\nSmile, and the worldsmiles with you...");
}

When I run this it returns error message as follows.

msg.c: In function ‘main’:

msg.c:5:2: warning: implicit declaration of function ‘message’ [-Wimplicit-function-declaration]

  message();

  ^~~~~~~

msg.c: At top level:

msg.c:8:6: warning: conflicting types for ‘message’

 void message()

      ^~~~~~~

msg.c:5:2: note: previous implicit declaration of ‘message’ was here

  message();

  ^~~~~~~

When I put the message function above main() then it shows no error. Why is it? Can't we put functions after main()? What is implicit declaration here?

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
KRISHNA I
  • 57
  • 2
  • 11
  • You need a declaration of the function before `main`: `void message();`. – Osiris Nov 20 '18 at 16:09
  • 1
    you need to decalre your function before using it. Just put a prototype before main `void message();` – Yastanub Nov 20 '18 at 16:09
  • 1
    The proper declarations for `main` are `int main (void)` and `int main (int argc, char **argv)` (which you will see written with the equivalent `char *argv[]`). **note:** `main` is a function of `type int` and it returns a value. See: [C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570)](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1). – David C. Rankin Nov 20 '18 at 16:21

4 Answers4

5

You can put functions after main if you want; just if you're calling them in main, before they're defined, you should also declare them before main:

void message();
void main()
...

Without this the compiler assumes that message is an externally linked function returning int, and then when it comes across your actual definition of message it complains about conflicting types for message (since it already decided that message returns int, not void).

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • 1
    better `void message(void);`, as `void message();` means the function is not checked against parameter types, and so the compiler does not check/convert parameter types to the proper types. – Luis Colorado Nov 22 '18 at 13:42
3

You first to define or declare your method, before calling it. In the following example, I declare the method before calling it in main:

#include<stdio.h>

// Declare the method
void message();

void main()
{
    // Call the method
    message();
    printf("\nCry, and you stop the monotomy!\n");
}

// Define the method
void message()
{
    printf("\nSmile, and the worldsmiles with you...");
}

PS: I would change the return type of main() to an int. Read more in What should main() return in C and C++?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

When the compiler gets to message(); (in main) the compiler knows nothing about the function message.

It now tries its best. So it gives you are warning and then assumes that message should have been declared int message();

So when the compiler finally gets to void message() - it says "hello - I thought it would be int messgae". Hence the warning.

Simply put either message before main, so when it gets to compiling main the compiler knows about message.

Or as other posters have said. Declare it at the top.

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

When i put the message function above main() then it shows no error. Why is it? Can't we put functions after main()?

C source files are parsed top-down. So by the time compiler sees the function call message (in main), it must know about it (the same applies to any symbol, basically). That's why putting it above works but below results in diagnostics.

At a minimum, you must provide a declaration for any identifier before their use.

What is implicit declaration here?

When the compiler sees a function call and didn't know about it, it assumes the function returns an int (Such as int message();). This is the "implicit declaration". This was an ancient rule that was valid until C99.

But in C99 and later, this rule has been removed. Thus your code (that puts the definition of "message" below main without a declaration) is invalid in C99 and later. See C function calls: Understanding the "implicit int" rule.

Later when the compiler sees the actual definition of message (i.e., void message() {...), it sees the return type is actually void. Thus this conflicts with its own declaration (where it assumed its int). So it generates:

msg.c:8:6: warning: conflicting types for ‘message’
P.P
  • 117,907
  • 20
  • 175
  • 238