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
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?
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
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?
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
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
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
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
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! 🖖
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)
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.
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
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.
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; }
@@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.
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.
that is the most intuitive explanation i have found for static. you are so good at explaining things
A simple but very visual example, making the subject quick to understand. Thanks a lot for this video.
Thank you a lot my brother , this was the best explanation about differences between regular and static variables , good bless you!.
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!
Thanks a lot, very clear! i couldn't ask for anything better than this!
Thank you for this simple and powerful explanation
Nice video, I used to make the mistake of thinking static and const were the same. I doubt I was the only one.
Brilliant as always
awesome explanation!! I was just scratching my head by looking bookish definitions
your videos are very helpful, thank you!!
Great explanation, man!
This video made me finally understand static variables
This helped me a lot. Thank you very much.
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
Awesome explanation 👏
I love this , following from Morocco ✌🏻
It was really helpful and that helped me a lot.
Thanks Sir for clearing my doubts.
Thanks bro your videos are really helpful
YOU'RE AMAZING!
great explanation, thank you
thanks a lot brother appreciate that
very clear! thanks
very good Thanku🎉
nice explanation thanks
Thank you so much.
wow totally understand in one go , thanks by subed and liked
very clear!
thanks to clear my dought.
Please upload more videos bro. Its really helpful for students like me
Thank you so much
Wow! Nice!
saved me 10 hours before exam ; keep it up
you just save me men, tanks from Mex
Thanks!!!
Best Explain in simple english
great!
woah thats goooooooooooooooooooooooooooooooooooooooooooooooood thank you very much
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?
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
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?
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
Hi there!
can you make a video about static variables in structs - in C? Would be a great topic!
I will look into it
perfect...
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
Yes, it can. It can store anything any other normal variable can
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
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
What will be the default value of static storage class variable and when will it be a global variable.👍
static variables are technically just like global variables but with their scope limited to their function. So the default value for them is 0.
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?
Are you incrementing after every print you're doing? Can you send me your whole source code?
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! 🖖
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)
@@CodeVault Thank you! GLHF!
Love from india
Can a static variable be declared wit
hout initialization?
static int x;
x=3;
Yes! It behaves the same as uninintialized global variables. That x will have the value of 0 by default
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.
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
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.
Global vs Local; not Internal vs External
Thanks ,so this is true for c++ also ?
Yes, although, "static" in the context of fields and methods in classes has a different meaning in C++
Hi! What static mean outside of a function?
Basically it marks the identifier is "private" and cannot be shared between compilation units. I'll make a video on this actually
Could you also give some practical applications of static variables in embedded systems?
Thanks 😊
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;
}
@@CodeVault
Thank you. Another example that I could think of is to use static for variables that we could use as counters.
Somehow the name 'static' seems completely counter-intuitive for what it is.
Maybe it's just me.
Thanks for the explanation.
No complaints, however I believe you could have explained it better with memory layout of a C program.
Memory layout? You mean have a visual diagram of sorts?
@@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.
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.
@@CodeVault oh okay I understand and I respect your teaching methods.
I was mere curious that's all.