If i define a global variable in a .c
file, how can i use the value of the same variable in another .c
file?
如果我在.c文件中定义一个全局变量,我如何在另一个.c文件中使用相同变量的值?
file1.c
在file1.c
#include<stdio.h>
int i=10;
int main()
{
printf("%d",i);
return 0;
}
file2.c
file2.c中
#include<stdio.h>
int main()
{
//some data regarding i
printf("%d",i);
return 0;
}
How can the second file use the value of i
from the first file here.
第二个文件如何使用第一个文件中的i值。
7 个解决方案
#1
55
file 1:
档案1:
int x = 50;
file 2:
文件2:
extern int x;
printf("%d", x);
#2
2
using extern <variable type> <variable name>
in a header or another C file.
在标题或另一个C文件中使用extern
#3
2
Use the extern
keyword to declare the variable in the other .c
file. E.g.:
使用extern关键字在另一个.c文件中声明该变量。例如。:
extern int counter;
means that the actual storage is located in another file. It can be used for both variables and function prototypes.
表示实际存储位于另一个文件中。它可以用于变量和函数原型。
#4
0
In the second .c
file use extern
keyword with the same variable name.
在第二个.c文件中,使用具有相同变量名称的extern关键字。
#5
0
Do same as you did in file1.c In file2.c:
与在file1.c中执行的操作相同在file2.c中:
#include <stdio.h>
extern int i; /*This declare that i is an int variable which is defined in some other file*/
int main(void)
{
/* your code*/
If you use int i; in file2.c under main() then i will be treated as local auto variable not the same as defined in file1.c
如果你使用int i;在main()下的file2.c中,我将被视为本地自动变量,与file1.c中定义的不同
#6
0
Use extern keyword in another .c file.
在另一个.c文件中使用extern关键字。
#7
0
If you want to use global variable i of file1.c in file2.c, then below are the points to remember:
如果你想在file2.c中使用file1.c的全局变量i,那么下面是要记住的要点:
- main function shouldn't be there in file2.c
- file2.c中不应该包含main函数
- now global variable i can be shared with file2.c by two ways:
a) by declaring with extern keyword in file2.c i.e extern int i;
b) by defining the variable i in a header file and including that header file in file2.c. - 现在全局变量i可以通过两种方式与file2.c共享:a)通过在file2.c中使用extern关键字声明,即extern int i; b)通过在头文件中定义变量i并在file2.c中包含该头文件。
#1
55
file 1:
档案1:
int x = 50;
file 2:
文件2:
extern int x;
printf("%d", x);
#2
2
using extern <variable type> <variable name>
in a header or another C file.
在标题或另一个C文件中使用extern
#3
2
Use the extern
keyword to declare the variable in the other .c
file. E.g.:
使用extern关键字在另一个.c文件中声明该变量。例如。:
extern int counter;
means that the actual storage is located in another file. It can be used for both variables and function prototypes.
表示实际存储位于另一个文件中。它可以用于变量和函数原型。
#4
0
In the second .c
file use extern
keyword with the same variable name.
在第二个.c文件中,使用具有相同变量名称的extern关键字。
#5
0
Do same as you did in file1.c In file2.c:
与在file1.c中执行的操作相同在file2.c中:
#include <stdio.h>
extern int i; /*This declare that i is an int variable which is defined in some other file*/
int main(void)
{
/* your code*/
If you use int i; in file2.c under main() then i will be treated as local auto variable not the same as defined in file1.c
如果你使用int i;在main()下的file2.c中,我将被视为本地自动变量,与file1.c中定义的不同
#6
0
Use extern keyword in another .c file.
在另一个.c文件中使用extern关键字。
#7
0
If you want to use global variable i of file1.c in file2.c, then below are the points to remember:
如果你想在file2.c中使用file1.c的全局变量i,那么下面是要记住的要点:
- main function shouldn't be there in file2.c
- file2.c中不应该包含main函数
- now global variable i can be shared with file2.c by two ways:
a) by declaring with extern keyword in file2.c i.e extern int i;
b) by defining the variable i in a header file and including that header file in file2.c. - 现在全局变量i可以通过两种方式与file2.c共享:a)通过在file2.c中使用extern关键字声明,即extern int i; b)通过在头文件中定义变量i并在file2.c中包含该头文件。