Implicit declaration

Rounak picture Rounak · Feb 26, 2012 · Viewed 9.9k times · Source

I am using Xcode 4.1 on Mac OS 10.7

#include <stdio.h>

int main (int argc, const char * argv[])
{
    int i, j;

    i = 1;
    j = 9;
    printf("i = %d and j = %d\n", i, j);

    swap(&i, &j);
    printf("\nnow i = %d and j = %d\n", i, j);

    return 0;
}

swap(i, j)
int *i, *j;
{
    int temp = *i;
    *i = *j;
    *j = temp;
}

I get the warning "Implicit declaration of function "swap" is invalid in C99

Answer

cnicutar picture cnicutar · Feb 26, 2012

Declare your function before main:

void swap(int *i, int *j);

/* ... */
int main...

And define it later:

void swap(int *i, int *j)
{
    /* ... */
}

Alternatively you can merge the two and move the entire definition before main.