No video

What is a static variable?

  Рет қаралды 40,494

CodeVault

CodeVault

5 жыл бұрын

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-damentals6354
@fun-damentals6354 4 ай бұрын
that is the most intuitive explanation i have found for static. you are so good at explaining things
@ehoihere
@ehoihere Жыл бұрын
A simple but very visual example, making the subject quick to understand. Thanks a lot for this video.
@muhammadluqman3452
@muhammadluqman3452 2 жыл бұрын
Thank you a lot my brother , this was the best explanation about differences between regular and static variables , good bless you!.
@mirzakadic9174
@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!
@rockydo2307
@rockydo2307 2 жыл бұрын
Nice video, I used to make the mistake of thinking static and const were the same. I doubt I was the only one.
@ankitsrivastavaMV
@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
@nrted3877
@nrted3877 2 жыл бұрын
awesome explanation!! I was just scratching my head by looking bookish definitions
@youseftalal2638
@youseftalal2638 3 жыл бұрын
Thank you for this simple and powerful explanation
@HeatherHolt
@HeatherHolt 3 ай бұрын
your videos are very helpful, thank you!!
@EccentricTuber
@EccentricTuber 2 жыл бұрын
This video made me finally understand static variables
@giacomol02
@giacomol02 2 жыл бұрын
Thanks a lot, very clear! i couldn't ask for anything better than this!
@user-qm4or3bt4v
@user-qm4or3bt4v 4 жыл бұрын
Brilliant as always
@AashishTheThakur
@AashishTheThakur 2 жыл бұрын
very good Thanku🎉
@honesty2272
@honesty2272 2 ай бұрын
Best Explain in simple english
@mikeros6388
@mikeros6388 2 жыл бұрын
you just save me men, tanks from Mex
@slasankautube
@slasankautube Жыл бұрын
wow totally understand in one go , thanks by subed and liked
@xxxmin0uxxx858
@xxxmin0uxxx858 Жыл бұрын
thanks a lot brother appreciate that
@santhoshk2722
@santhoshk2722 Жыл бұрын
thanks to clear my dought.
@vimukthikulasekara2109
@vimukthikulasekara2109 2 жыл бұрын
woah thats goooooooooooooooooooooooooooooooooooooooooooooooood thank you very much
@codex8797
@codex8797 2 жыл бұрын
Thanks bro your videos are really helpful
@kaoutharlakhder8449
@kaoutharlakhder8449 2 жыл бұрын
I love this , following from Morocco ✌🏻
@mohamedbenslimen978
@mohamedbenslimen978 2 жыл бұрын
saved me 10 hours before exam ; keep it up
@kyledrewes6552
@kyledrewes6552 2 жыл бұрын
This helped me a lot. Thank you very much.
@aleceastman
@aleceastman Жыл бұрын
great explanation, thank you
@bhadriramlella7697
@bhadriramlella7697 Жыл бұрын
It was really helpful and that helped me a lot.
@izazahmed5978
@izazahmed5978 Жыл бұрын
Thank you so much.
@ihffbd9719
@ihffbd9719 2 жыл бұрын
Thanks Sir for clearing my doubts.
@dhanushammayappan9419
@dhanushammayappan9419 Жыл бұрын
Please upload more videos bro. Its really helpful for students like me
@shashwatkumarsoni54cse39
@shashwatkumarsoni54cse39 2 жыл бұрын
Awesome explanation 👏
@sergeiabramov282
@sergeiabramov282 2 жыл бұрын
Great explanation, man!
@muhammeddikal8551
@muhammeddikal8551 2 жыл бұрын
nice explanation thanks
@goedeck1
@goedeck1 2 жыл бұрын
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.
@whathuh6965
@whathuh6965 2 жыл бұрын
Global vs Local; not Internal vs External
@audiodiwhy2195
@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
@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
@onlycomputershit6687 Жыл бұрын
Hi there! can you make a video about static variables in structs - in C? Would be a great topic!
@CodeVault
@CodeVault Жыл бұрын
I will look into it
@williammoura8829
@williammoura8829 2 жыл бұрын
YOU'RE AMAZING!
@casandrutza88
@casandrutza88 3 жыл бұрын
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! 🖖
@CodeVault
@CodeVault 3 жыл бұрын
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)
@casandrutza88
@casandrutza88 3 жыл бұрын
@@CodeVault Thank you! GLHF!
@cezzar5233
@cezzar5233 2 жыл бұрын
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?
@CodeVault
@CodeVault 2 жыл бұрын
Are you incrementing after every print you're doing? Can you send me your whole source code?
@fadieid5638
@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
@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
@IvanDGr8 Жыл бұрын
Thanks!!!
@knownlearner568
@knownlearner568 2 жыл бұрын
great!
@ysabelfuentespinto2812
@ysabelfuentespinto2812 2 жыл бұрын
very clear! thanks
@chainsinghpawar9138
@chainsinghpawar9138 3 жыл бұрын
What will be the default value of static storage class variable and when will it be a global variable.👍
@CodeVault
@CodeVault 3 жыл бұрын
static variables are technically just like global variables but with their scope limited to their function. So the default value for them is 0.
@----700
@----700 9 ай бұрын
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.
@CodeVault
@CodeVault 9 ай бұрын
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
@manuelconte2127
@manuelconte2127 2 жыл бұрын
very clear!
@edgbaston149
@edgbaston149 2 жыл бұрын
Thank you so much
@Jonathan-ru9zl
@Jonathan-ru9zl 2 ай бұрын
Hi! What static mean outside of a function?
@CodeVault
@CodeVault 2 ай бұрын
Basically it marks the identifier is "private" and cannot be shared between compilation units. I'll make a video on this actually
@goedeck1
@goedeck1 2 жыл бұрын
Can a static variable be declared wit hout initialization? static int x; x=3;
@CodeVault
@CodeVault 2 жыл бұрын
Yes! It behaves the same as uninintialized global variables. That x will have the value of 0 by default
@3P5C1
@3P5C1 2 жыл бұрын
Somehow the name 'static' seems completely counter-intuitive for what it is. Maybe it's just me. Thanks for the explanation.
@mehmetcivan
@mehmetcivan 2 жыл бұрын
perfect...
@wahabfiles6260
@wahabfiles6260 4 жыл бұрын
Wow! Nice!
@abdelmouizmamouni
@abdelmouizmamouni 2 жыл бұрын
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
@CodeVault
@CodeVault 2 жыл бұрын
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_Shankar
@Vineeth_Shankar 3 жыл бұрын
Could you also give some practical applications of static variables in embedded systems? Thanks 😊
@CodeVault
@CodeVault 3 жыл бұрын
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_Shankar
@Vineeth_Shankar 3 жыл бұрын
@@CodeVault Thank you. Another example that I could think of is to use static for variables that we could use as counters.
@ritikpandey5939
@ritikpandey5939 2 жыл бұрын
Love from india
@lucasferrini7063
@lucasferrini7063 3 жыл бұрын
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
@CodeVault
@CodeVault 3 жыл бұрын
Yes, it can. It can store anything any other normal variable can
@k_ushagra
@k_ushagra 3 жыл бұрын
No complaints, however I believe you could have explained it better with memory layout of a C program.
@CodeVault
@CodeVault 3 жыл бұрын
Memory layout? You mean have a visual diagram of sorts?
@k_ushagra
@k_ushagra 3 жыл бұрын
@@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.
@CodeVault
@CodeVault 3 жыл бұрын
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_ushagra
@k_ushagra 3 жыл бұрын
@@CodeVault oh okay I understand and I respect your teaching methods. I was mere curious that's all.
@ahmedegymed5853
@ahmedegymed5853 2 жыл бұрын
Thanks ,so this is true for c++ also ?
@CodeVault
@CodeVault 2 жыл бұрын
Yes, although, "static" in the context of fields and methods in classes has a different meaning in C++
Integer type conversions
8:04
CodeVault
Рет қаралды 1,7 М.
Understanding Static, Stack, and Heap Memory Regions (Examples in C)
33:29
哈莉奎因以为小丑不爱她了#joker #cosplay #Harriet Quinn
00:22
佐助与鸣人
Рет қаралды 10 МЛН
What are variadic functions (va_list) in C?
13:49
CodeVault
Рет қаралды 21 М.
What are double pointers in C?
14:43
CodeVault
Рет қаралды 42 М.
Static Data Member And Static Member Function In C++
17:19
CppNuts
Рет қаралды 51 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
C++ Pointers - Finally Understand Pointers
15:56
Caleb Curry
Рет қаралды 210 М.
Member Initializer Lists | C++ Tutorial
23:10
Portfolio Courses
Рет қаралды 8 М.
Static for Classes and Structs in C++
9:12
The Cherno
Рет қаралды 348 М.
Static vs Non-Static Variables and Methods In Java - Full Simple Tutorial
11:29
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 387 М.
Compilers, How They Work, And Writing Them From Scratch
23:53
Adam McDaniel
Рет қаралды 145 М.
哈莉奎因以为小丑不爱她了#joker #cosplay #Harriet Quinn
00:22
佐助与鸣人
Рет қаралды 10 МЛН