In the snippet below:

#include <stdio.h>

void swap (int*, int*);
// ^----(1)

int main (void) {
    int a = 21;
    int b = 17;

    swap(&a, &b);
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

void swap (int *pa, int *pb) {
// ^----(2)
    int t = *pa;
    *pa = *pb;
    *pb = t;
    return;
}

Which of these between (1) and (2) can be considered as a function prototype?

  • m12421k@iusearchlinux.fyi
    link
    fedilink
    arrow-up
    0
    ·
    6 months ago

    at 1 you are doing forward declaration.

    you declare the interface of a function in the header file. that way the compiler would know that function swap exists and it takes two int pointers but returns nothing.

    from the outside of that module that’s all it needs to know. it can compile them separately and link them together later dynamically.

    you’re separating swap interface in the header file from its implementation in the .c file that contains the body of the function.