This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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
  • "A definition of a function may be provided by a prototype"

    Strictly, a Prototype provides only a declaration of a function; a matching definition must also be provided.

    A declaration simply informs the compiler about the function - it gives just enough information to allow the compiler to generate calls to the function, and handle any return value; ie, the function name, the number & type of any parameters, and the type of any return value.

    The definition provides the actual executable code to implement the function - the definition does not have to be in the same source file, or even the same language!
    The Linker sorts this out - or gives an "Unresolved External" error if it can't!

    As Graham said, you need to read-up on this in any standard 'C' textbook.