c - if global variable have default external linkage then why can't we access it directly in another file? -


i have gone through following questions:

above links describe if define global variable in 1 file , haven't specified extern keyword accessible in source file because of translation unit.

now have file1.c in have defined following global variable , function:

int testvariable;  void testfunction() {     printf ("value of testvariable %d \n", testvariable); } 

in file2.c have following code

void main() {     testvariable = 40;     testfunction(); } 

now getting error: 'testvariable' undeclared (first use in function) -- why?

note: both files used in same program using makefile.

as per understanding both function , global variable have default external linkage. function can use directly it's name in file variable can't why?

can 1 have idea?

edit:

from below answer idea in case of function old compiler guess , add implicit declaration in case of variable can't. c99 removed implicit declaration still getting warning in c99 mode like:

warning: implicit declaration of function ‘testfunction’. 

now have gone through below link:

implicit int , implicit declaration of functions gcc compiler

it said compiler take diagnostic purpose , not give error. compiler can process forward.

but why in case of variable can't process further? in case of function if compiler proceed , if actual definition not there @ linking time fail. what's benefit move forward??

there 2 things in play here: first there difference between definition , declaration. other thing concept of translation units.

a definition defines variable, it's actual place variable exists, compiler reserves space variable.

a declaration needed compiler know symbol exists somewhere in program. without declaration compiler not know symbol exists.

a translation unit , simplified source file plus included header files. object file single translation unit, , linker takes translation units create final program.

now, program can have single definition, example global variable may exist in single translation unit, or multiple definition errors when linking. declaration on other hand can exist in number of translation units, compiler use tell linker translation references definition in (unknown @ time of compilation) translation unit.

so happens here have definition and declaration in file1.c. source file used input 1 translation unit , compiler generates single object file it, file1.o. in other source file, file2.c, there no definition nor declaration of global variable testvariable, compiler doesn't know exists , give error it. need declare it, example doing

extern int testvariable;  // declaration of variable 

it's little more complicated function, because in older versions of c standard 1 didn't have declare functions being used, compiler guess , add implicit declaration. if definition , implicit declaration doesn't match lead undefined behavior, why implicit function declarations removed in c99 standard. should declare function too:

void testfunction(void);  // declare function prototype 

note extern keyword not needed here, because compiler can automatically tell it's function prototype declaration.

the complete file2.c should like

extern int testvariable;  // declaration of variable  void testfunction(void);  // declare function prototype  void main() {     testvariable = 40;     testfunction(); } 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -