Here's a small mnemonic tip You can think of "static" like "static electricity"... a static local variable "sticks" in memory across function calls kind of like a static-electrically charged piece of plastic might stick to your finger. And a static global variable or static function "sticks" to the translation unit it was put in, and it doesn't work in other places
Hello Jacob, there are also other uses of static local variables, I have been using them to implement singletons in my C programs. They are very useful for this technique and allows you to not make use of global variables (that are often prohibited or are being very limited in their usage in a project for x reason). To show an example, you could use a function that returns an address to a static struct. Then you could assign and access this struct across your whole program in a very flexible way by calling the function, and dereferencing a pointer to the data you are interested in. best regards, Karim
Static int declared inside a function makes it a ram variable, as normally it's a scratch temporary register (R2 etc) As it's ram variable it will keep its value when function is called again.
could you please make a video, in which you explain how to develop a project using separate files for each piece of functionality, how to connect them later on, what to store in header files etc. would be nice to watch it. thank you for your videos.
"Making a local variable static" does not mean anything about its lexical visibility. All local variables are only visible (by name) by the function itself. Making a local variable static affects only its lifetime. It will live and retain its value across function calls.
I use static functions for private ones. you're writing a function that does X, but it also needs to do Q, pop Q into a static function. Primarily, static functions are best used for defining an API.
It should be pointed out that using static local variables makes that function non-reentrant and thread-unsafe, which is the same problem as global variables. In practice this doesn't mean much unless you intend to call the function from a signal handler or multiple threads. But you can easily make the counter function reentrant and thread-safe by using a static atomic counter.
I am trying to understand this keyword. For now, the analogy that seems to work with static variables in functions is to compare them with files and users. The same way a user can have permission to read, write and execute a file, a function can have permission to see and modify a variable. Variables that are static are private to the function. I haven't come across static functions yet, so I don't know if the analogy holds with them. Thanks for the video!
Things I got from this: 1. static in C isn't what I thought it was (I thought it was what it was in Java) 2. static appears to do nothing i.e. if you use it in a function on a variable, it has the scope of the function, if you use it outside of a function, it has global scope 3. If you use it on a function, something about translation units... I've never heard of translation units.
It definitely does something. A static local is locally scoped, but persists across function calls (other locals do not). Glad the extern video helped you understand translation units.
@@JacobSorber oooh so if I call the function and set a static local to 10 say, next time I call the same function and read the static local it will still be 10? but because it is local it will remain inaccessible outside of the function.
@JacobSorber Thank for the video. Will you please explain the key differences in 'global variable' and 'static global variable', yes global variable can be accessed outside the file using extern but are they same if extern is not used?
Very Nice video and nice explanation. I have a doubt over here around: 2:40 -2:55. Please help me to clear it.(Disclaimer: I am not a C expert) Any variable declared in the scope of a function is local to that function be it static or not and any other function will not be aware of it anyways. Whereas static allows initialisation only once and no matter how many time the function is called it will not reinitialise its value and will retain the value from previous call. Though this functionality cannot be achieved by non-static variable. Please correct me if I am wrong.
@@JacobSorber Thanks for such a quick response and clarification!! :) . Your videos are short and concise. Thanks once again and keep making the these amazing videos.
I'm thinking that is one key issue in learning programming (for me): encountering programming jargon that is not effectively revealing of its true purpose. One day I just sat down and thought to myself why the hell would anyone prefer a term like `private` in modelling a hierarchy of objects and then match it with another term like `method`. If the idea was to describe the «unique capabilities» of an object, why utilize the terms `private` and `methods` instead of just saying `capabilities` `unique` to the object? Ugh.
I think you make a fair point about "capability" (though "method" is a bit easier to type). With "unique", I think "unique" would be worse than "private", since "private" isn't saying that this thing is unique to the object or to the class, it says it's scoped so it can only be accessed from within the object or class. It's a secret from the rest of the program. So, in that case, private seems (to me) to be a more accurate fit.
I imagine the keyword for «capability» would've been `capab` (something along those lines, like «print function» became `printf`). Have you covered macros? :D
Yes, local function variables are always only visible *by name* from inside the function. No other code may refer to them by name. However, other code may refer to them by pointer. And for static local variables that works even after the function has returned.
Static globals or static locals? In either case, they're probably being stored in the .data segment with the globals. You could use objdump to check, or just write a program that prints out the address of the variable you're wondering about.
And also suggest you to make a video on the permutations and combinations we can use with all storage classes and modifiers. This we can practice anyhow but most people ask this type of questions in interviews...so it will be helpful..!Thanks jacob..By the way I Love your Video's ❤️
Several reasons, really. I've spent some time in industry, and so far my research interests seem to fit better in academia than in industry. I also love to teach and mentor students, and academia gives me a lot of options in how I craft my life and career. It's just a better fit for me, at least for now. Maybe I should do an academia vs industry video. :)
Here's a small mnemonic tip
You can think of "static" like "static electricity"... a static local variable "sticks" in memory across function calls kind of like a static-electrically charged piece of plastic might stick to your finger. And a static global variable or static function "sticks" to the translation unit it was put in, and it doesn't work in other places
I like it. Thanks.
I'm sure the person who named them both static had that in mind ;)
Hello Jacob,
there are also other uses of static local variables, I have been using them to implement singletons in my C programs. They are very useful for this technique and allows you to not make use of global variables (that are often prohibited or are being very limited in their usage in a project for x reason). To show an example, you could use a function that returns an address to a static struct. Then you could assign and access this struct across your whole program in a very flexible way by calling the function, and dereferencing a pointer to the data you are interested in.
best regards,
Karim
Static int declared inside a function makes it a ram variable, as normally it's a scratch temporary register (R2 etc) As it's ram variable it will keep its value when function is called again.
Oh thanks.....that's a really meaningful difference that should have been mentioned on the video. I guess few people know that.
this is fantastic! please continue making videos!!!
Thanks. Will do. Any specific requests?
could you please make a video, in which you explain how to develop a project using separate files for each piece of functionality, how to connect them later on, what to store in header files etc. would be nice to watch it. thank you for your videos.
Sure. I'll add it to the future video list.
"Making a local variable static" does not mean anything about its lexical visibility. All local variables are only visible (by name) by the function itself. Making a local variable static affects only its lifetime. It will live and retain its value across function calls.
I use static functions for private ones.
you're writing a function that does X, but it also needs to do Q, pop Q into a static function.
Primarily, static functions are best used for defining an API.
It should be pointed out that using static local variables makes that function non-reentrant and thread-unsafe, which is the same problem as global variables. In practice this doesn't mean much unless you intend to call the function from a signal handler or multiple threads. But you can easily make the counter function reentrant and thread-safe by using a static atomic counter.
OMG, finally a reasonable explanation!
Thank you!
My pleasure.
I am trying to understand this keyword. For now, the analogy that seems to work with static variables in functions is to compare them with files and users. The same way a user can have permission to read, write and execute a file, a function can have permission to see and modify a variable. Variables that are static are private to the function.
I haven't come across static functions yet, so I don't know if the analogy holds with them.
Thanks for the video!
Things I got from this: 1. static in C isn't what I thought it was (I thought it was what it was in Java) 2. static appears to do nothing i.e. if you use it in a function on a variable, it has the scope of the function, if you use it outside of a function, it has global scope 3. If you use it on a function, something about translation units... I've never heard of translation units.
It definitely does something. A static local is locally scoped, but persists across function calls (other locals do not). Glad the extern video helped you understand translation units.
@@JacobSorber oooh so if I call the function and set a static local to 10 say, next time I call the same function and read the static local it will still be 10? but because it is local it will remain inaccessible outside of the function.
@@WistrelChianti Yep. Try it out.
@@JacobSorber will do! Ta!!
When you learn C storage classes, it makes total sense about static
your videos are beyond amazing ! great quality
Thanks. I'm glad you enjoyed it.
Ya my first programming classes were in java was the static keyword in C was pretty weird to me.
I started in Pascal, but it was still weird to me.
@JacobSorber Thank for the video. Will you please explain the key differences in 'global variable' and 'static global variable', yes global variable can be accessed outside the file using extern but are they same if extern is not used?
Love the intro!!!
Very Nice video and nice explanation. I have a doubt over here around: 2:40 -2:55. Please help me to clear it.(Disclaimer: I am not a C expert) Any variable declared in the scope of a function is local to that function be it static or not and any other function will not be aware of it anyways. Whereas static allows initialisation only once and no matter how many time the function is called it will not reinitialise its value and will retain the value from previous call. Though this functionality cannot be achieved by non-static variable. Please correct me if I am wrong.
Sounds good. A "static" local is just a local variable that persists across multiple invocations of the function.
@@JacobSorber Thanks for such a quick response and clarification!! :) . Your videos are short and concise. Thanks once again and keep making the these amazing videos.
Awesome, as usual!! 👊😎
I'm thinking that is one key issue in learning programming (for me): encountering programming jargon that is not effectively revealing of its true purpose. One day I just sat down and thought to myself why the hell would anyone prefer a term like `private` in modelling a hierarchy of objects and then match it with another term like `method`. If the idea was to describe the «unique capabilities» of an object, why utilize the terms `private` and `methods` instead of just saying `capabilities` `unique` to the object? Ugh.
I think you make a fair point about "capability" (though "method" is a bit easier to type). With "unique", I think "unique" would be worse than "private", since "private" isn't saying that this thing is unique to the object or to the class, it says it's scoped so it can only be accessed from within the object or class. It's a secret from the rest of the program. So, in that case, private seems (to me) to be a more accurate fit.
I imagine the keyword for «capability» would've been `capab` (something along those lines, like «print function» became `printf`). Have you covered macros? :D
I haven't, but it's one of the many topics that I have planned for the future.
Perhaps 'internal capability'
But aren't function variables always visible only inside the function?
Not neccessarily. You could for example have a function return a pointer to an static variable it owns. And you could modify or see it anywhere
Yes, local function variables are always only visible *by name* from inside the function. No other code may refer to them by name.
However, other code may refer to them by pointer. And for static local variables that works even after the function has returned.
and this is not the only C/C++ keyword that has multiple meanings depending of its context :D
Thank you so much!
amazing could !! you pleas explain where the weak key word is used
Where are static variables stored?
Static globals or static locals? In either case, they're probably being stored in the .data segment with the globals. You could use objdump to check, or just write a program that prints out the address of the variable you're wondering about.
@@JacobSorber thanks.
Hi jacob, can you please make a video on auto storage class..i am bit confused.
And also suggest you to make a video on the permutations and combinations we can use with all storage classes and modifiers. This we can practice anyhow but most people ask this type of questions in interviews...so it will be helpful..!Thanks jacob..By the way I Love your Video's ❤️
Thanks. I'll see what I can do.
dope vid,
Why did you decide to be a professor? Just curious.
Several reasons, really. I've spent some time in industry, and so far my research interests seem to fit better in academia than in industry. I also love to teach and mentor students, and academia gives me a lot of options in how I craft my life and career. It's just a better fit for me, at least for now. Maybe I should do an academia vs industry video. :)