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

前端 未结 5 1263
说谎
说谎 2021-01-02 06:41

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(         


        
相关标签:
5条回答
  • 2021-01-02 07:22

    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; }
    
    0 讨论(0)
  • 2021-01-02 07:23

    This is how C works. We need to declare the function before use. like when you use any variable, you must have declare first then you would have use it.

    Declaration:- function1(); function2(); and then put your own code.

    0 讨论(0)
  • 2021-01-02 07:39

    Foward declare your functions...

    function1();
    function2();
    
    function2(){
      function1()
    }
    function1 (){
      function2()
    }
    
    main () {
     function1()
    }
    
    0 讨论(0)
  • 2021-01-02 07:42

    Forward declare your functions, but by using prototypes. If you have a lot of them such that you can't handle this, this is the moment to think of your design and to create a .h file with all your prototypes. Use

    int function1(void);
    int function2(void);
    

    if that was what you meant. int function1() already is different from that in C. Help the compiler such that he can help you.

    0 讨论(0)
  • 2021-01-02 07:44

    Try:

    function1();
    function2();
    
    function2(){
      function1()
    }
    function1 (){
      function2()
    }
    
    main () {
     function1()
    }
    
    0 讨论(0)
提交回复
热议问题