Reputation: 611
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
Upvotes: -1
Views: 12105
Reputation: 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
Upvotes: -1
Reputation: 145829
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)
{
...
}
Upvotes: 0
Reputation: 182629
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
.
Upvotes: 1