Implicit declaration

你离开我真会死。 提交于 2019-12-13 07:53:58

问题


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


回答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.




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/9453587/implicit-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!