Questions tagged [extern]

extern is an access-specifier in C and C++ which defines a global variable that is visible to all object modules.

The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. The extern declaration in C/C++ is to indicate the existence of, and the type of, a global variable or function. A global variable, or a global function, is one that is available to all C/C++ modules. An extern is something that is defined externally to the current module.

schematic representation of global variable declared with extern keyword (source)

1475 questions
24
votes
3 answers

3 questions about extern used in an Objective-C project

When I use the word extern before a method or variable declaration, am I making it global and therefore readable/writable/usable over the entire project ? If I use extern before a keyword, is there any chance it is still not accessible by part of…
aneuryzm
  • 63,052
  • 100
  • 273
  • 488
23
votes
9 answers

extern keyword usage

I have three programs in which I am using extern keyword. I am not able to understand the result. Below are three examples: Example 1: I was expecting that below code will give compilation error that multiple declaration of k. But it works fine? int…
anu
  • 231
  • 1
  • 2
  • 3
23
votes
5 answers

Pointer-array-extern question

File 1.c int a[10]; File main.c: extern int *a; int main() { printf("%d\n", a[0]); return 0; } Gives me a segfault! What's going wrong?
Rajendra Uppal
  • 19,218
  • 15
  • 59
  • 57
23
votes
2 answers

How does "extern C++" work?

I jumped into winnt.h and I found out the code as following: extern "C++" // templates cannot be declared to have 'C' linkage template char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N]; I'd like to ask questions as…
Adam
  • 1,684
  • 1
  • 19
  • 39
23
votes
4 answers

"extern" inside a function?

Well, reading "a bit old" book ("The C programming language", second edition, by Dennis Ritchie), I came a cross the following: An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
22
votes
2 answers

Why do I get "PInvokeStackImbalance was detected" for this simple example?

I'm creating a very simple PInvoke sample: extern "C" __declspec(dllexport) int Add(int a, int b) { return a + b; } [DllImport("CommonNativeLib.dll")] extern public static int Add(int a, int b); return NativeMethods.Add(a, b); But whenever I…
Justin
  • 84,773
  • 49
  • 224
  • 367
22
votes
2 answers

Does C++ allow an optimizing compiler to ignore side effects on the for-condition?

While debugging some legacy code I stumbled upon surprising (for me) compiler behavior. Now I'd like to know whether any clause in the C++ spec allows the following optimization, where side effects from a function call on the for-condition are…
22
votes
7 answers

static const Vs extern const

I have been using static const in my header files as so: static NSString * const myString = @"foo"; But have read that this is not the 'safe' or correct way of doing this. Apparently, if I want my const strings to be accessed from another class, I…
ssh88
  • 393
  • 1
  • 3
  • 14
22
votes
3 answers

C: What is the use of 'extern' in header files?

Pardon me if this sounds a question that has been asked many times but I assure you this is a little different. I use Codeblocks for C programming and lately I have started to wonder why would someone use a header file in C. I understand it is used…
Yogender Singh
  • 291
  • 2
  • 3
  • 9
22
votes
1 answer

Access extern variable in C++ from another file

I have a global variable in one of the cpp files, where I am assigning a value to it. Now in order to be able to use it in another cpp file, I am declaring it as extern and this file has multiple functions that use it so I am doing this globally. …
Venushree Patel
  • 279
  • 1
  • 4
  • 6
21
votes
4 answers

How to declare my very own CGRectZero like constant?

This is a newbie C/Objective-C question :-) Let say I want a CGRectOne and a CGRectTwo constants. How can I declare that? Thanks, Jérémy
jchatard
  • 1,881
  • 2
  • 20
  • 25
21
votes
3 answers

Global variables in C are static or not?

Are global variables in C static or extern by default? If global variables are by default static then it means that we would be able to access them in a single file, but we can use global variables in different files as well. Does this imply that…
Mishthi
  • 1,947
  • 5
  • 16
  • 12
20
votes
4 answers

Create extern char array in C

How to create an external character array in C? I have tried various ways to define char cmdval[128] but it always says undefined reference to 'cmdval' I want to put a string in cmdval in first.c file and use it in other second.c file. I tried…
SMUsamaShah
  • 7,677
  • 22
  • 88
  • 131
20
votes
3 answers

declare extern variable within a C function?

I define a variable in a C file: int x, and I know I should use extern int x to declare it in other files if I want to use it in other files. My question is: where should I declare it in other files? Outside of all functions, // in file a.c: int…
Wang Tuma
  • 893
  • 5
  • 14
  • 24
19
votes
1 answer

"extern const" vs "extern" only

I've seen 2 ways of creating global variables, what's the difference, and when do you use each? //.h extern NSString * const MyConstant; //.m NSString * const MyConstant = @"MyConstant"; and //.h extern NSString *MyConstant; //.m NSString…
Cananito
  • 389
  • 1
  • 2
  • 7
1 2
3
98 99