What is the significance of forward declaration in C programming?

后端 未结 4 1812
长情又很酷
长情又很酷 2021-01-04 18:57

I am now learning C programming through Learn C the Hard Way by Zed A. Shaw. There is this code (taking from his website):

#include 
#include          


        
相关标签:
4条回答
  • 2021-01-04 19:28

    Forward declarations of functions in C typically have two different uses.

    Modules

    The header of exported functions are declared in a header file which is included in a client module.

    Mutual Recursion

    In mutual recursion two functions call each other repeatedly. Without a forward declaration one of the two functions will be undeclared in the body of the other.

    Example:

    int Odd(int n);
    
    int Even(int n)
    {
        return (n == 0)? 1: Odd(n - 1);
    }
    
    int Odd(int n)
    {
        return (n == 0)? 0: Even(n - 1);
    }
    

    With a function pointer though, we can do without a forward declaration:

    int (*odd)(int n);
    
    int Even(int n)
    {
        return (n == 0)? 1: odd(n - 1);
    }
    
    int Odd(int n)
    {
        return (n == 0)? 0: Even(n - 1);
    }
    
    void Init(void)
    {
        odd = Odd;
        ...
    }
    
    0 讨论(0)
  • 2021-01-04 19:33

    Forward declaration is upto the program's need. Programmer can design it in their own.

    Understand the significance: In C and C++, the line above represents a forward declaration of a function and is the function's prototype. After processing this declaration, the compiler would allow the program code to refer to the entity printThisInteger in the rest of the program. The definition for a function must be provided somewhere (same file or other, where it would be the responsibility of the linker to correctly match references to a particular function in one or several object files with the definition, which must be unique, in another):

    0 讨论(0)
  • 2021-01-04 19:48

    You should declare functions in the order that makes sense. This should be described in the coding style document you follow. One example of common design is:

    • h-file with all publically available function declarations.
    • c-file with all private function declarations at the top.
    • then in the same c-file follows the function definition of the public functions.
    • and then at the end of that c-file, the function definition of the private functions.

    In addition to style and design concerns, ancient versions of C would start to "make up" function parameters and return types if there was no prototype at all visible before the function call.

    0 讨论(0)
  • 2021-01-04 19:50

    Forward declarations of functions are unavoidable whenever your call graph is cyclic; that is, whenever you have (direct or indirect) recursion between functions.

    They are useful if you want to separate your program into more than one translation unit, as they allow separation of declaration and definition of functions (placing the declaration in a .h header and the definition in a .c file).

    0 讨论(0)
提交回复
热议问题