Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort functions in C? "previous implicit declaration of a function was here" error

Tags:

c

function

I'm sure this has been asked before, but I couldn't find anything that would help me. I have a program with functions in C that looks like this

function2(){
  function1()
}
function1 (){
  function2()
}

main () {
 function1()
}

It's more complicated than that, but I'm using recursion. And I cannot arrange the function in the file so that every function would only call functions that are specified above itself. I keep getting an error

main.c:193: error: conflicting types for 'function2'
main.c:127: error: previous implicit declaration of 'function2' was here

How do I avoid this? Thanks in advance for suggestions and answers.

like image 744
Randalfien Avatar asked Dec 08 '10 13:12

Randalfien


People also ask

What is implicit declaration of function in C?

Implicit declaration of the function is not allowed in C programming. Every function must be explicitly declared before it can be called. In C90, if a function is called without an explicit declaration, the compiler is going to complain about the implicit declaration. Here is a small code that will give us an Implicit declaration of function error.

What is the use of Q sort in C++?

The qsort function implements a somewhat generic sorting operation for different data element arrays. Namely, qsort takes the pointer to function as the fourth argument to pass the comparison function for a given array of elements.

Do every function have to be explicitly declared in C?

Every function must be explicitly declared before it can be called. In C90, if a function is called without an explicit declaration, the compiler is going to complain about the implicit declaration.

How to use the standard library sort function in C?

This article will explain several methods of how to use the standard library sort function in C. The qsort function implements a somewhat generic sorting operation for different data element arrays. Namely, qsort takes the pointer to function as the fourth argument to pass the comparison function for a given array of elements.


2 Answers

You need to declare (not define) at least one function before using it.

function2();                 /* declaration */
function1() { function2(); } /* definition */
function2() { function1(); } /* definition */

int main(void) { function1(); return 0; }
like image 79
pmg Avatar answered Oct 22 '22 00:10

pmg


Foward declare your functions...

function1();
function2();

function2(){
  function1()
}
function1 (){
  function2()
}

main () {
 function1()
}
like image 29
Andrew White Avatar answered Oct 21 '22 23:10

Andrew White