+ 1

What is the alternative for extern in C language?

What coding methodology could be the best alternative for extern

7th Dec 2017, 6:24 PM
Nagarajan Kandasamy
Nagarajan Kandasamy - avatar
2 Answers
+ 1
An extern variable is a declaration (thanks to sbi for the correction) of a variable which is defined in another translation unit. That means the storage for the variable is allocated in another file. Say you have two .c-files test1.c and test2.c. If you define a global variable int test1_var; in test1.c and you'd like to access this variable in test2.c you have to use extern int test1_var; in test2.c. Complete sample: $ cat test1.c int test1_var = 5; $ cat test2.c #include <stdio.h> extern int test1_var; int main(void) { printf("test1_var = %d\n", test1_var); return 0; } $ gcc test1.c test2.c -o test $ ./test test1_var = 5
10th Dec 2017, 4:40 PM
Bits!
0
I am just asking for an alternative coding methodology for extern
16th Dec 2017, 9:45 AM
Nagarajan Kandasamy
Nagarajan Kandasamy - avatar