query:definition of func in C

Hi all

 include<stdio.h>
 #define ....

 void xyz();        <---- i am referring to this defn of xyz()

 void abc()
  {
   -:statements:-
  }

 void main()
  {
     abc();
     xyz();
  }

 void xyz()
  {
   -:statements:-
  }
      

Is it compulsory to define all the Routines,which are called from main ?(except extern)
ie xyz() was defined at the begginng of the program,but abc() was not defined.
is it so because during compilation,compiler have already met with abc()
before being called from main().

Thanks
Arjun

Parents
  • This is really a C question and not at all specific to the Keil products. You need a good C book.

    However, yes you are right. A C compiler compiles source code in (notionally) a single pass: starting at the top and ending at the bottom. When the C compiler finds a reference to a function (or a variable or anything else) it must already have compiled a definition. A definition of a function may be provided by a prototype (as xyz in your example) or the function itself (as abc in your example).

    It is a common practice to arrange the code so that prototypes are kept to a minimum. It is often possible to write a program with no prototypes at all. Note that this necessarily means that main() will appear at the end of the source code.

Reply
  • This is really a C question and not at all specific to the Keil products. You need a good C book.

    However, yes you are right. A C compiler compiles source code in (notionally) a single pass: starting at the top and ending at the bottom. When the C compiler finds a reference to a function (or a variable or anything else) it must already have compiled a definition. A definition of a function may be provided by a prototype (as xyz in your example) or the function itself (as abc in your example).

    It is a common practice to arrange the code so that prototypes are kept to a minimum. It is often possible to write a program with no prototypes at all. Note that this necessarily means that main() will appear at the end of the source code.

Children
More questions in this forum