extern

How do I share a global variable between c files?

六眼飞鱼酱① 提交于 2019-11-28 16:49:52
If i define a global variable in a .c file, how can i use the value of the same variable in another .c file? file1.c #include<stdio.h> int i=10; int main() { printf("%d",i); return 0; } 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. file 1: int x = 50; file 2: extern int x; printf("%d", x); Use the extern keyword to declare the variable in the other .c file. E.g.: extern int counter; means that the actual storage is located in another file. It can be used for both variables and

Static, extern and inline in Objective-C

折月煮酒 提交于 2019-11-28 16:28:51
问题 What do static , extern and inline (and their combinations) mean in Objetive-C using the LLVM compiler? Also, I noticed that there are CG_EXTERN and CG_INLINE macros. Should we be using those instead? (I couldn't find a source with a clear explanation so I thought it might be useful to create one here, or point to it if someone knows one) 回答1: What do static, extern and inline (and their combinations) mean in Objetive-C using the LLVM compiler? The same as in C, unless you compile as ObjC++ -

C++ extern keyword on functions. Why no just include the header file?

空扰寡人 提交于 2019-11-28 16:20:43
If I understand it correctly this means extern void foo(); that the function foo is declared in another translation unit. 1) Why not just #include the header in which this function is declared? 2) How does the linker know where to look for function at linking time? edit: Maybe I should clarify that the above declaration is then followed by using the function foo(); It is never defined in this translation unit. 1) It may not have a header file. But yes, in general, for large projects, you should have a header file if multiple translation units are going to use that function (don't repeat

C/C++中的static和extern关键字

╄→尐↘猪︶ㄣ 提交于 2019-11-28 16:01:24
一.C语言中的static关键字 在C语言中,static可以用来修饰局部变量,全局变量以及函数。在不同的情况下static的作用不尽相同。 (1)修饰局部变量 一般情况下,对于局部变量是存放在 栈区 的,并且局部变量的生命周期在该语句块执行结束时便结束了。但是如果用static进行修饰的话,该变量便存放在 静态数据区 ,其生命周期一直持续到整个程序执行结束。但是在这里要注意的是,虽然用static对局部变量进行修饰过后,其生命周期以及存储空间发生了变化, 但是其作用域并没有改变,其仍然是一个局部变量,作用域仅限于该语句块。 在用static修饰局部变量后, 该变量只在初次运行时进行初始化工作,且只进行一次 。 如: #include<stdio.h> void fun() { static int a=1; a++; printf("%d\n",a); } int main(void) { fun(); fun(); return 0; } 程序执行结果为: 2 3 说明在第二次调用fun()函数时,a的值为2,并且没有进行初始化赋值,直接进行自增运算,所以得到的结果为3. 对于静态局部变量如果没有进行初始化的话,对于整形变量系统会自动对其赋值为0,对于字符数组,会自动赋值为'\0'. (2)修饰全局变量 对于一个全局变量,它既可以在本源文件中被访问到

Should functions be made “extern” in header files?

ε祈祈猫儿з 提交于 2019-11-28 15:46:45
问题 Should functions be made extern in header files? Or are they extern by default? For example, should I write this: // birthdays.h struct person find_birthday(const char* name); or this: // birthdays.h extern struct person find_birthday(const char* name); 回答1: From The C Book: If a declaration contains the extern storage class specifier, or is the declaration of a function with no storage class specifier (or both), then: If there is already a visible declaration of that identifier with file

Objective-C static, extern, public variables

孤人 提交于 2019-11-28 15:28:46
I want to have a variable that I can access anywhere by importing a header file but I also want it to be static in the sense that there is only one of them created. In my .m file, I specify static BOOL LogStuff = NO; and in the initialize method I set the logging value: + (void)initialize { LogStuff = ... //whatever } However I want to be able to access my variable anywhere by importing the .h file so I want to do something like this: static extern BOOL LogStuff; but I'm not allowed to do that. Is it possible to do the thing I'm trying to do? Thanks Adam Rosenfield static in Objective-C means

C/C++中extern关键字详解

让人想犯罪 __ 提交于 2019-11-28 14:25:18
1 基本解释 :extern可以置于 变量或者函数 前, 以标示变量或者函数的定义在别的文件中 , 提示编译器遇到此变量和函数时在其他模块中寻找其定义 。此外extern也可用来进行链接指定。 也就是说extern有两个作用,第一个,当它与"C"一起连用时,如: extern "C" void fun(int a, int b);则告诉编译器在编译fun这个函数名时按着C的规则去翻译相应的函数名而不是C++的,C++的规则在翻译这个函数名时会把fun这个名字变得面目全非,可能是fun@aBc_int_int#%$也可能是别的,这要看编译器的"脾气"了(不同的编译器采用的方法不一样),为什么这么做呢,因为C++支持函数的重载啊,在这里不去过多的论述这个问题,如果你有兴趣可以去网上搜索,相信你可以得到满意的解释! 第二,当extern不与"C"在一起修饰变量或函数时,如在头文件中: extern int g_Int; 它的作用就是声明函数或全局变量的作用范围的关键字,其声明的函数和变量可以在本模块活其他模块中使用,记住它是一个声明不是定义!也就是说B模块(编译单元)要是引用模块(编译单元)A中定义的全局变量或函数时,它只要包含A模块的头文件即可,在编译阶段,模块B虽然找不到该函数或变量,但它不会报错,它会在连接时从模块A生成的目标代码中找到此函数。 2 问题: extern 变量  

C/C++中extern关键字详解

笑着哭i 提交于 2019-11-28 14:25:07
1 基本解释 :extern可以置于 变量或者函数 前, 以标示变量或者函数的定义在别的文件中 , 提示编译器遇到此变量和函数时在其他模块中寻找其定义 。此外extern也可用来进行链接指定。 也就是说extern有两个作用,第一个,当它与"C"一起连用时,如: extern "C" void fun(int a, int b);则告诉编译器在编译fun这个函数名时按着C的规则去翻译相应的函数名而不是C++的,C++的规则在翻译这个函数名时会把fun这个名字变得面目全非,可能是fun@aBc_int_int#%$也可能是别的,这要看编译器的"脾气"了(不同的编译器采用的方法不一样),为什么这么做呢,因为C++支持函数的重载啊,在这里不去过多的论述这个问题,如果你有兴趣可以去网上搜索,相信你可以得到满意的解释! 第二,当extern不与"C"在一起修饰变量或函数时,如在头文件中: extern int g_Int; 它的作用就是声明函数或全局变量的作用范围的关键字,其声明的函数和变量可以在本模块活其他模块中使用,记住它是一个声明不是定义!也就是说B模块(编译单元)要是引用模块(编译单元)A中定义的全局变量或函数时,它只要包含A模块的头文件即可,在编译阶段,模块B虽然找不到该函数或变量,但它不会报错,它会在连接时从模块A生成的目标代码中找到此函数。 2 问题: extern 变量  

变量声明和定义的区别

微笑、不失礼 提交于 2019-11-28 13:50:49
我们在程序设计中,时时刻刻都用到变量的定义和变量的声明,可有些时候我们对这个概念不是很清楚,知道它是怎么用,但却不知是怎么一会事,下面我就简单的把他们的区别介绍如下:(望我的指点对你受益) 变量的声明有两种情况: 1、一种是需要建立存储空间的。例如:int a 在声明的时候就已经建立了存储空间。 2、另一种是不需要建立存储空间的。 例如:extern int a 其中变量a是在别的文件中定义的。 前者是“定义性声明(defining declaration)”或者称为“定义(definition)”,而后者是“引用性声明(referncing declaration)”,从广义的角度来讲声明中包含着定义,即定义是声明的一个特例,所以并非所有的声明都是定义,例如:int a 它既是声明,同时又是定义。然而对于 extern a 来讲它只是声明不是定义。一般的情况下我们常常这样叙述,把建立空间的声明称之为“定义”,而把不需要建立存储空间的声明称之为“声明”。很明显我们在这里指的声明是范围比较窄的,即狭义上的声明,也就是说非定义性质的声明,例如:在主函数中: int main() { extern int A; //这是个声明而不是定义,声明A是一个已经定义了的外部变量 //注意:声明外部变量时可以把变量类型去掉如:extern A; dosth(); //执行函数 } int A; /

const and global

你。 提交于 2019-11-28 11:48:40
This code will produce error in c++ // Foo.cpp const int Foo = 99; // Main.cpp extern const int Foo; int main() { cout << Foo << endl; return 0; } Reason as given by many is global const has internal scope and it is default static. solution to this is :- //Foo.h extern const int Foo; // Foo.cpp #include "Foo.h" const int Foo = 99; // Main.cpp #include "Foo.h" int main() { cout << Foo << endl; } I used to think that extern is used to tell compiler that memory for the indentifer is already allocated somewhere in other files. Applying same logic on above code can anyone explain what is happening