简体   繁体   English

C 中 function 的外部链接,不带“extern”可以吗?

[英]extern linkage of function in C, is it okay to not be with “extern”?

Hello I'm viewing an example of external linkage of function and variable in C.您好,我正在查看 function 和 C 中的变量的外部链接示例。

This is an example that produces random variables.这是一个产生随机变量的例子。

  1. random.c随机.c
#define SEED 20
#define MULT 3124
#define INC 2345
#define MOD 5436

unsigned int call_count = 0; 

static unsigned seed = SEED;  

unsigned random_i(void) {

    seed = (MULT * seed + INC) % MOD;

    call_count++;

    return seed;

}

double random_f(void) {

    seed = (MULT * seed + INC) % MOD;
    call_count++;

    return seed / (double)MOD;
}
  1. and this is main.c这是main.c
#include <stdio.h>
#include <stdlib.h> 

#pragma warning (disable:4996)

unsigned random_i(void); // I wonder why this prototype has no "extern" specifier
double random_f(void); // I wonder why this prototype has no "extern" specifier

extern unsigned call_count;

int main(void) {

    register int i;

    for (i = 0; i < 10; i++)
        printf("%d ", random_i());

    printf("\n");

    for (i = 0; i < 10; i++)
        printf("%lf ", random_f());

    printf("\nCall count : %d\n", call_count);
    
    return 0;
}

in this short program, I wonder why those of two function prototype has no " extern " specifier AND why this code is compiled without an error.在这个简短的程序中,我想知道为什么两个 function 原型的那些没有“ extern ”说明符以及为什么这段代码编译没有错误。

Because what I know is that when I use variables or functions that is in other source code, I have to do it with extern specifier for example extern int a=10;因为我知道的是,当我使用其他源代码中的变量或函数时,我必须使用extern说明符,例如extern int a=10; . .

Please let me know.请告诉我。

Thanks.谢谢。

Both functions and data definitions outside of a function block have implicit external linkage. function 块之外的函数和数据定义都具有隐式外部链接。 However when it cones to a declaration , the extern keyword is required for data object otherwise it is a definition .但是,当它涉及到声明时,数据 object 需要extern关键字,否则它是一个定义 For functions a prototype is unambiguously not a definition because it has no body.对于函数,原型显然不是定义,因为它没有主体。

Regarding:关于:

I have to do it with extern specifier for example extern int a=10;我必须使用 extern 说明符,例如extern int a=10;

Not quite, in this case extern int a=10;不完全是,在这种情况下extern int a=10; is a definition not a declaration because you have included an initialiser.是定义而不是声明,因为您包含了初始化程序。 It is the same as int a=10;int a=10; and would result in a duplicate definition error if also defined elsewhere.如果也在别处定义,将导致重复定义错误。

Symbols at global scope are externally visible by default.默认情况下,全局 scope 处的符号在外部可见。 This applies to both variables and functions.这适用于变量和函数。 The extern keyword is only needed to prevent multiple definitions of a symbol.仅需要extern关键字来防止符号的多个定义。 It states that the current declaration is defined elsewhere.它指出当前声明在别处定义。

extern is required for call_count since it would otherwise be a definition. call_count 需要extern ,否则它将是一个定义。 It is not required for the declaration of global functions since there is no definition.全局函数的声明不需要它,因为没有定义。

Note that注意

extern int a=10;

generates a warning in gcc:在 gcc 中生成警告:

warning: ‘a’ initialized and declared ‘extern’ 

All functions are extern by default so prototypes do not need extern .默认情况下,所有函数都是 extern ,因此原型不需要extern But you can, of course, use it if you wish.但是,如果您愿意,当然可以使用它。

unsigned random_i(void); === extern unsigned random_i(void); === extern unsigned random_i(void);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM