C++ : Share variables between two separate files (extern and static)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up people over there and welcome to this video in which we do I would talk in this video I'll be talking about variable sharing between two files so when you build up more complex C++ programs you're not going to be only one main file there going to be several files some file that it stored structure some file that store your functions and other other things so there can be more than one file long story short so in this program this will be a very simple program nothing great here this is just for demonstration purposes I'm using two files the main dot CPP file where every file is kind of linked to it every it is kind of like a tree trunk and every other program is like a root everything is linked to the main dot CPP file and and this is the main ocv file and the next file is extra thing that CPP I just call it extra thing that CPP because well it's just an extra thing in this simple video demonstration alright so let's go ahead and get started let's declare a variable into Z and let's say it set it value something like 13 just 13 who cares now in Z or if you have already familiar with the way variables are stored in the scope of variables you know that int Z has a file scope and what does that mean let me just show you I can just go ahead into there oops a return 0 is important because of the end here alright so what I can go go ahead and go ahead and use my Z variable in the main function and it can be fairly used it has a file scope that means it can be used in any function in side this main dot CPP file so I can go ahead and declare another and another function here let's call it whatever function and we will go ahead and see how Z I'm writing this function above main because I don't want a hassle of function prototypes in the video demonstration but you have to use from some pretty nice when you are building programs that's a good practice so here goes I can use it this this program is see out of 30 here then I can go ahead and call the function my inside main PD and I can run it and this will should display two times 13 so the main idea was this integer Z can be used anywhere in this file alright so let's go ahead into extra think of CPP and I'll declare a new function let's call it a simple function and we're it now okay what we're going to do is you're going to go ahead and see out z because you know the extra thing that CPP compiles along with me not CPP that means it can injure Z be used initial thing that sleepy be well let's compile yeah so before we even go ahead and let's clear this function prototype in the main dot CP because it's always important to declare a function prototypes here all right I declare the function period i p-- and let's go ahead and call this simple function in my main oops I am simple function in the main CBD file I'm gonna run it and oh oh what happened let's look at the error it's a error z was not declared in this coat and you're like z bar well it is there in main why isn't it what is it working well because into Z actually has a file scope I have mentioned before that Z can be used anywhere in this file and not in any another file of the of your project so what you're like but I want to use it here okay then there's C++ has a solution for you it's using the extern keyword so you can go in x turn and Z so what extern does is pretty much up you know gives a gives an external link a of Z to the extra things CPP file so that we can use the same variable here over in this file so I can go ahead and run this program and it works perfectly find like it should work okay and now I'm going to talk about something else let's say you have some variable called count here we just keeping count of something and me as you want to use it we'll use it over here really definitely you can use it over here and anyway you can go and see out count and I can get through it of this year okay I can just go and see on County should display 30 now there's another file in your program which also keeps count of something else which is totally different from what keeps count of and main dot CPP file and you're like okay it keeps count of something an account is a very simple name and it's easy to enter print I want to use the word count here too and then you'll be like okay let's go ahead and declare it my own count variable here and I'll go ahead and see out count over here and that should everything should work fine and then you were in the program and oh there is an error multiple definition of count what's that let me explain into your account is it here also in two counties over here also that's the simplest definition I can give because count you see has been declared twice because these two files are compiled together and they're like okay into your counties here and oh what your account is again here Braman hands up there's an error what do you do then well you want a variable that has the name count because it's keeping count of something simple name and you want that variable to be known to only this file it should not go ahead and interfere with any other file variable names so what you do is you go ahead and declare a static you add a static keyword in front of it and everything is fine again because this static means that this count variable is known only two extra things SE b b main dot cpp has no idea whether exiting out CBB has the count variable in it or not every static variable inside extra thing is static which means that it is in actually no CPB what happens in Vegas stays in Vegas nothing goes out here similarly thing if you are already experienced if you already have a knowledge about how variables are declares tax and statics you might wonder like what static is a whole different thing well yeah it's a whole different thing I mean when you have automatic variables and you within functions which has a function scope and then you're like even you add static it is stored it has a file scope then well I'm talking about intermediate viewers if you are feeling her and you don't know what I'm talking about doesn't matter all right and then you were like okay static means it as a file scope but let me explain even if I remove static from here in count we'll still have a file scope even if put static here still ever files go so when you add static in front of a variable which is a file scope it means that it will be known only to that file it files covers the variable inside it it is not visible to any other files let's go ahead and run our program and everything runs perfectly fine zero and thirteen because it says never I li let's give it some very good 45
Info
Channel: Nash Vail
Views: 48,426
Rating: 4.6227899 out of 5
Keywords: C++, programming, computers, sharing, variables, separate, files, different, static, extern, dynamic, scope, C (programming Language)
Id: hDfDnsvEodQ
Channel Id: undefined
Length: 7min 23sec (443 seconds)
Published: Sat Feb 09 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.