-1

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

dbush
  • 205,898
  • 23
  • 218
  • 273
Rounak
  • 613
  • 3
  • 8
  • 22
  • 1
    possible duplicate of [What does "implicit declaration of function" mean?](http://stackoverflow.com/questions/2161304/what-does-implicit-declaration-of-function-mean) -- please use the search before you ask a new question. – Felix Kling Feb 26 '12 at 13:50

3 Answers3

1

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.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
0

A function name has to be declared before its use in C99.

You can either define your swap function before main or put a declaration of the function before main.

Also you are using the old-style function definition for the swap function. This form is a C obsolescent feature, here is how you should define your function:

void swap(int *i, int *j)
{
    ...
}
ouah
  • 142,963
  • 15
  • 272
  • 331
-1

Declaring a variable means reserving memory space for them. It is not required to declare a variable before using it. Whenever VB encounter a new variable it assigns the default variable type and value. This is called implicit declaration

  • 1
    The question clearly states they are using xcode, why have you made a reference to Visual Basic in your answer. If you want to explain implicit declaration to the user raising the question then use the language which they are familiar with or asking about. – Alan Savage Feb 24 '19 at 23:50