Can I declare a variable in .h file with extern, then use it in main (a.c) BUT it gets defined in a function that exists in b.c? Said function get's called from main....
Thanks
Can I declare a variable in .h file with extern, then use it in main (a.c) BUT it gets defined in a function that exists in b.c? Said function get's called from main....
Thanks
No, because that variable, being non-static and having local scope, has no linkage, so trying to declare it as having external linkage in the header makes no sense.
I suggest that you don't worry about extern: for the most part, you're only going to use it when you need to declare a global variable, and since global variables should be rare, you probably don't need to use it. In this case, you may be able to have the function return a value or use an out parameter instead.
Last edited by laserlight; 05-12-2020 at 05:09 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)