Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of forward declaration in C programming?

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 <stdio.h>
#include <ctype.h>

// forward declarations
int can_print_it(char ch);
void print_letters(char arg[]);

void print_arguments(int argc, char *argv[])
{
    int i = 0;

    for(i = 0; i < argc; i++) {
        print_letters(argv[i]);
    }
}

void print_letters(char arg[])
{
    int i = 0;

    for(i = 0; arg[i] != '\0'; i++) {
        char ch = arg[i];

        if(can_print_it(ch)) {
            printf("'%c' == %d ", ch, ch);
        }
    }

    printf("\n");
}

int can_print_it(char ch)
{
    return isalpha(ch) || isblank(ch);
}


int main(int argc, char *argv[])
{
    print_arguments(argc, argv);
    return 0;
}

Don't we just can code it like this (put the can_print_it and print_letters functions at the top and remove the need for forward declaration):

#include <stdio.h>
#include <ctype.h>

int can_print_it(char ch)
{
    return isalpha(ch) || isblank(ch);
}

void print_letters(char arg[])
{
    int i = 0;

    for(i = 0; arg[i] != '\0'; i++) {
        char ch = arg[i];

        if(can_print_it(ch)) {
            printf("'%c' == %d ", ch, ch);
        }
    }

    printf("\n");
}

Is there really times when the forward declaration is important and unavoidable?

like image 860
Hafiz Hilman Mohammad Sofian Avatar asked Jan 29 '16 12:01

Hafiz Hilman Mohammad Sofian


People also ask

What is the significance of forward declaration?

Forward Declaration refers to the beforehand declaration of the syntax or signature of an identifier, variable, function, class, etc. prior to its usage (done later in the program). In C++, Forward declarations are usually used for Classes.

Are forward declarations good?

The Google style guide recommends against using forward declarations, and for good reasons: If someone forward declares something from namespace std, then your code exhibits undefined behavior (but will likely work).

What is meant by a forward reference in C?

Forward reference is when you declare a type but do not define it. It allows you to use the type by pointer (or reference for C++) but you cannot declare a variable. This is a way to say to the compiler that something exists.

Why are declarations needed in C?

A "declaration" establishes an association between a particular variable, function, or type and its attributes. Overview of Declarations gives the ANSI syntax for the declaration nonterminal. A declaration also specifies where and when an identifier can be accessed (the "linkage" of an identifier).


2 Answers

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).

like image 196
ecatmur Avatar answered Nov 16 '22 01:11

ecatmur


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;
    ...
}
like image 30
August Karlstrom Avatar answered Nov 16 '22 02:11

August Karlstrom