How do define global variable visible across several .c files

前端 未结 1 2019
野的像风
野的像风 2021-01-29 11:26

assume there is a header file: a.h and source file : a1.c a2.c ... an.c; n>=1, which means it may only have one source file, or have several source files.

my question

相关标签:
1条回答
  • 2021-01-29 12:00

    As you said, variable must be defined in the c file and declaration should be located in header file.
    You have to define a global variable in any c file and declare 'extern' in header file.

    Example)

    1) define global variable in a1.c
        int g;
    
    2) declare global variable in a.h
        extern int g;
    
    3) include header file in other c files
        #include "a.h"
        // to do something
    
    0 讨论(0)
提交回复
热议问题