Home:ALL Converter>Error in c programme conflicting types for xx and previous implicit declaration of xx was here

Error in c programme conflicting types for xx and previous implicit declaration of xx was here

Ask Time:2012-01-15T20:32:53         Author:bornfree

Json Formatter

Suppose I have a file insert.c in which two functions are defined: 1.insert_after 2.insert_before

The definitions of these func are something like this:

insert_after(arg1)
{ 
  if(condition 1)
    { ......... } 
  else
    insert_before(arg1); 
}

insert_before(arg)
{ 
  if(condition 1)
    { ......... } 
  else
    insert_after(arg); 
}
    

Now if this file insert.c is included in main.c and insert_after function is called

# include "insert.c"
int main()
{
  insert_after(arg);
  return 0;
}

On compiling main.c using gcc,the following error is encountered:

conflicting types for ‘insert_before’

note: previous implicit declaration of ‘insert_before’ was here

What is wrong here and how to avoid it?

Author:bornfree,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/8869634/error-in-c-programme-conflicting-types-for-xx-and-previous-implicit-declaration
Michael Dautermann :

create a .h file and give those functions prototypes (the Wikipedia entry describing prototypes is linked for you).\n\nthe .h file would contain just the functions:\n\ninsert_before(arg);\ninsert_after(arg);\n\n\nAlso, you should probably have a return type and a parameter type (e.g. void insert_before(char * arg); Compilers are really good with type checking and it will save you headaches later.",
2012-01-15T12:35:53
sidyll :

The problem is that you call insert_before before the compiler knows about it. Give them appropriate prototypes (in a header file) and include the header file in both insert.c and main.c",
2012-01-15T12:36:00
fge :

This is because you don't declare prototypes for your functions. A function which has no prototype, by default, has an unknown set of arguments and returns an int. But this is not the case for insert_before.\n\nCreate a file insert.h in which you declare them:\n\n#ifndef INSERT_H\n#define INSERT_H\n\nvoid insert_before(type_of_arg);\nvoid insert_after(type_of_arg);\n\n#endif /* INSERT_H */\n\n\nand include this file at the top of insert.c.\n\nYou should then compile with:\n\ngcc -Wall -Wstrict-prototypes -Wmissing-prototypes -o progname insert.c main.c\n",
2012-01-15T12:38:22
yy