Across the programming universe a "static" variable can mean many things. In this video we'll talk about what it actually means in C.
Пікірлер: 76
@fun-damentals63544 ай бұрын
that is the most intuitive explanation i have found for static. you are so good at explaining things
@ehoihere Жыл бұрын
A simple but very visual example, making the subject quick to understand. Thanks a lot for this video.
@muhammadluqman34522 жыл бұрын
Thank you a lot my brother , this was the best explanation about differences between regular and static variables , good bless you!.
@mirzakadic9174 Жыл бұрын
Came here from udemy c++ course and didn't quite get this and in 4 mins you explained it really well. So I get it now, thanks! Subbed!
@rockydo23072 жыл бұрын
Nice video, I used to make the mistake of thinking static and const were the same. I doubt I was the only one.
@ankitsrivastavaMV Жыл бұрын
Awesome example I watch many videos for Staics variable but I found your example Is very clear lots videos they are creating only length in video and confusing to learners
@nrted38772 жыл бұрын
awesome explanation!! I was just scratching my head by looking bookish definitions
@youseftalal26383 жыл бұрын
Thank you for this simple and powerful explanation
@HeatherHolt3 ай бұрын
your videos are very helpful, thank you!!
@EccentricTuber2 жыл бұрын
This video made me finally understand static variables
@giacomol022 жыл бұрын
Thanks a lot, very clear! i couldn't ask for anything better than this!
@user-qm4or3bt4v4 жыл бұрын
Brilliant as always
@AashishTheThakur2 жыл бұрын
very good Thanku🎉
@honesty22722 ай бұрын
Best Explain in simple english
@mikeros63882 жыл бұрын
you just save me men, tanks from Mex
@slasankautube Жыл бұрын
wow totally understand in one go , thanks by subed and liked
@xxxmin0uxxx858 Жыл бұрын
thanks a lot brother appreciate that
@santhoshk2722 Жыл бұрын
thanks to clear my dought.
@vimukthikulasekara21092 жыл бұрын
woah thats goooooooooooooooooooooooooooooooooooooooooooooooood thank you very much
@codex87972 жыл бұрын
Thanks bro your videos are really helpful
@kaoutharlakhder84492 жыл бұрын
I love this , following from Morocco ✌🏻
@mohamedbenslimen9782 жыл бұрын
saved me 10 hours before exam ; keep it up
@kyledrewes65522 жыл бұрын
This helped me a lot. Thank you very much.
@aleceastman Жыл бұрын
great explanation, thank you
@bhadriramlella7697 Жыл бұрын
It was really helpful and that helped me a lot.
@izazahmed5978 Жыл бұрын
Thank you so much.
@ihffbd97192 жыл бұрын
Thanks Sir for clearing my doubts.
@dhanushammayappan9419 Жыл бұрын
Please upload more videos bro. Its really helpful for students like me
@shashwatkumarsoni54cse392 жыл бұрын
Awesome explanation 👏
@sergeiabramov2822 жыл бұрын
Great explanation, man!
@muhammeddikal85512 жыл бұрын
nice explanation thanks
@goedeck12 жыл бұрын
Thanks, now I understand: it initializes only the first time. I think it is called an internal static variable because its inside a block. Also thanks for showing that sum is not accessible outside the block.
@whathuh69652 жыл бұрын
Global vs Local; not Internal vs External
@audiodiwhy2195 Жыл бұрын
Thanks for the clear explanation. Interesting. I had a program w global arrays defined in a .h file that compiled fine w older version of Cmake. There was no “static” keywords used. After updating Cmake the global variables I used in .h files threw multiple definition errors. Confusing! To fix I added static before the array declarations. Fixed. I think maybe Cmake was correctly inferring static vs extern in older versions but stopped doing that?
@CodeVault Жыл бұрын
cmake and make are just tools for building/executing/debugging projects. The one at fault would be the compiler, although I am not too familiar with older versions of (I assume) gcc
@onlycomputershit6687 Жыл бұрын
Hi there! can you make a video about static variables in structs - in C? Would be a great topic!
@CodeVault Жыл бұрын
I will look into it
@williammoura88292 жыл бұрын
YOU'RE AMAZING!
@casandrutza883 жыл бұрын
Big thanks for all of the tips and great explanations in all of the lessons! Regarding this one, I've been left with some unanswered questions. 1. I wrote same program, but initialized sum to 0 after the declaration and the result was 5, 5, 5, so I suppose on the next calls it ignores just the declaration row, is that right? 2. Then I thought making the function of static int type and got the same result. How does that work? 3. I then tried declaring and initializing sum in the main function and pass it to add() (static and just int) and still got 5, 5, 5.(?) In conclusion, it's still not clear to me how do static variables work. I would really appreciate if you can explain some more characteristics and behaviors of this type. Thank you! Take care! 🖖
@CodeVault3 жыл бұрын
Well, for all intents and purposes a static variable is a global variable except it can only be accessed from that function. Now let me answer your questions: 1) Yes, that is correct 2) static functions have nothing to do with static variables. Static functions are just functions that are only accessible by the same file (sort of as a "private" modifier) 3) Probably because you changed it in the sum function (which means you just changed its copy, not the actual value in the main function)
@casandrutza883 жыл бұрын
@@CodeVault Thank you! GLHF!
@cezzar52332 жыл бұрын
when i write static int x=1; x+=1; then print x value multiple times it gives me 2 everytime & x value doesn't increment, i dont know why?
@CodeVault2 жыл бұрын
Are you incrementing after every print you're doing? Can you send me your whole source code?
@fadieid5638 Жыл бұрын
Thank you, clear and concise as always. Just a question, is it possible to use "sum" as the name of an identifier elsewhere in the program? In that sense does it behave like a normal global variable and what's the scope of sum?
@CodeVault Жыл бұрын
You can use a "sum" global variable without issues. Just keep in mind that the code will use the static variable "sum" inside the function it is defined in. Other than that, you should have no issues
@IvanDGr8 Жыл бұрын
Thanks!!!
@knownlearner5682 жыл бұрын
great!
@ysabelfuentespinto28122 жыл бұрын
very clear! thanks
@chainsinghpawar91383 жыл бұрын
What will be the default value of static storage class variable and when will it be a global variable.👍
@CodeVault3 жыл бұрын
static variables are technically just like global variables but with their scope limited to their function. So the default value for them is 0.
@----7009 ай бұрын
Hello. I am a I beginner learning C language. I have a doubt related to register storage class. If possible please reply. #include register int a; int main() { extern int a; a= 2; printf("%d",a); return 0; } gives output 2 but #include register int a; int main() { printf("%d",a); return 0; } gives error. In both the cases register class is used for global variable definition . So why I got error only in the second case but not in the first case if the reason for it is that register class can't used for global variable definition.
@CodeVault9 ай бұрын
register class values cannot be global. They can only be static or local therefore the "extern int a" implementation works but the second one where "a" is supposed to be global doesn't
@manuelconte21272 жыл бұрын
very clear!
@edgbaston1492 жыл бұрын
Thank you so much
@Jonathan-ru9zl2 ай бұрын
Hi! What static mean outside of a function?
@CodeVault2 ай бұрын
Basically it marks the identifier is "private" and cannot be shared between compilation units. I'll make a video on this actually
@goedeck12 жыл бұрын
Can a static variable be declared wit hout initialization? static int x; x=3;
@CodeVault2 жыл бұрын
Yes! It behaves the same as uninintialized global variables. That x will have the value of 0 by default
@3P5C12 жыл бұрын
Somehow the name 'static' seems completely counter-intuitive for what it is. Maybe it's just me. Thanks for the explanation.
@mehmetcivan2 жыл бұрын
perfect...
@wahabfiles62604 жыл бұрын
Wow! Nice!
@abdelmouizmamouni2 жыл бұрын
Thank you! Can just tell me for example if we have a static variable d inside a function1 and in another function2 we have Int d, the second function we what variable will work
@CodeVault2 жыл бұрын
Both variables would be independent. If you use d inside function1 it would be the static variable d defined in function1, if you use d inside function2 it would be the d variable you defined in function2
@Vineeth_Shankar3 жыл бұрын
Could you also give some practical applications of static variables in embedded systems? Thanks 😊
@CodeVault3 жыл бұрын
I guess the most common example is when returning a string. You'll just have to be careful that the resulting string might get modified the next time you call the function. char* getString() { static char str[50] = "Hello there"; return str; }
@Vineeth_Shankar3 жыл бұрын
@@CodeVault Thank you. Another example that I could think of is to use static for variables that we could use as counters.
@ritikpandey59392 жыл бұрын
Love from india
@lucasferrini70633 жыл бұрын
Can a static variable be used to store a memory adress? Let's say i have an array of ints and a function that receives a memory adress and set the static variable to store the memory adress (only the first time it is called). And it returns said memory adress
@CodeVault3 жыл бұрын
Yes, it can. It can store anything any other normal variable can
@k_ushagra3 жыл бұрын
No complaints, however I believe you could have explained it better with memory layout of a C program.
@CodeVault3 жыл бұрын
Memory layout? You mean have a visual diagram of sorts?
@k_ushagra3 жыл бұрын
@@CodeVault yes with memory segments classification.. like bss data heap and stack.. with reference to the video, you could explain easily how local variables are created and destroyed in stack, where as static variables remain in data seg. or bss (depending on initialisation) even after function ends. also, in that you can also include recursive functions.. it will be so easier to explain it with functionality of how stack and local variables works.
@CodeVault3 жыл бұрын
Usually I want a very simple explanation of the concept, I don't want people to need to know about data segments, local vs global variables and whatnot. But alright, I'll keep that in mind and may make another video on the different types of memory segments out there.
@k_ushagra3 жыл бұрын
@@CodeVault oh okay I understand and I respect your teaching methods. I was mere curious that's all.
@ahmedegymed58532 жыл бұрын
Thanks ,so this is true for c++ also ?
@CodeVault2 жыл бұрын
Yes, although, "static" in the context of fields and methods in classes has a different meaning in C++