Defining global variables in the C language

Source: Internet
Author: User

(1) The problem of defining variables in the header file of the C language

It's best not to be silly about defining something in the header file. such as global variables:
/*XX Header File */
#ifndef
_xx_ header file. H
#define
_xx_ header file. H
int A;
#endif

Well, the bad thing is, here's int a
is a definition of a global variable, so if the header file is referenced multiple times, your A will be duplicated, obviously syntactically wrong. Just have this #ifndef conditional compilation, so can ensure that your header file is only quoted once, but may still not go wrong, but if more than one C file contains this header file will still error, because the macro name valid range is limited to this C source file, so in the compilation of the C file is not error , but you'll get an error when linking , saying you've defined the same variable in multiple places.

Error Warning: Linking ...
Incl2.obj:error LNK2005: "int glb" ([email protected]@3ha)
Already defined in Incl1.obj
Debug/incl.exe:fatal Error Lnk1169:one
or more multiply defined symbols found

The correct approach is to define a global variable in the C source file. Adding a declaration of a global variable to a header file

//var.c 一个主要用于定义全局变量的c源文件

int g_a; char g_x; //var.h 用于声明定义的全局变量 extern int g_a;//使用extern在头文件中声明全局变量 extern char g_x;  //test.c 一个用于测试的头文件 #include <var.h> void f(){      g_a = 1;      g_x =  ‘x‘ ; } (3) The declaration of a variable or function in a header file is not defined for a variable, if you want to use another source file variable in the original file, you need to declare the variable with extern before use, or declare the variable with extern in the header file; for a function, if you want to use another source file function in the original file, you need to declare the variable before use, declaring that the function plus no extern is okay, so the function in the header file can not be added extern. In order to share the variable i in several source files, I first put the definition of the variable i in a source file: int i; If you need to initialize the variable i, you can put the initial value here. When compiling this file, the compiler allocates memory space for the variable I, while the other file will contain the declaration of the variable i: extern int i; By declaring the variable i in each file, you can access/or modify the variable i in these files. However, because of the keyword extern, the compiler does not allocate additional memory space to the variable I each time one of the files is compiled. When you share a variable in a file, you face a similar challenge to sharing the function: ensure that all declarations and variables of a variable are defined consistently. To avoid conflicts, the declaration of shared variables (extern int i;) is usually placed in the header file. Source files that require access to special variables can later contain the appropriate header files. In addition, the source file that contains the variable definition contains each header file that contains the variable declaration, so that the compiler can check whether the two match.

Defining global variables in the C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.