函数声明顺序在c语言中是重要的还是我做错了什么?

pep*_*dip 3 c declaration function

我收到此错误:

arthur@arthur-VirtualBox:~/Desktop$ gcc -o hw -ansi hw1.c
hw1.c: In function `main':
hw1.c:27:16: warning: assignment makes pointer from integer without a cast [enabled by default]
hw1.c: At top level:
hw1.c:69:7: error: conflicting types for `randomStr'
hw1.c:27:18: note: previous implicit declaration of `randomStr' was here
Run Code Online (Sandbox Code Playgroud)

在编译此代码时:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
        char *rndStr;
        rndStr = randomStr(FILE_SIZE);
        /* Do stuff */
        return 0;
}

/*Generate a random string*/
char* randomStr(int length)
{
        char *result;
        /*Do stuff*/
        return result;
}
Run Code Online (Sandbox Code Playgroud)

如果我切换它周围的功能顺序,为什么呢?

oua*_*uah 8

除少数例外情况外,C语言中的标识符在声明之前不能使用.

以下是如何在其定义之外声明一个函数:

// Declare randomStr function
char *randomStr(int length);
Run Code Online (Sandbox Code Playgroud)


Rob*_*obᵩ 5

发生错误是因为符号通常必须使用之前声明,而不是在之后声明.

一个特殊的例外是函数.如果在声明之前使用它们,则推断出特定声明.在您的情况下,推断声明与最终定义不匹配.