No video

The Static Keyword in C

  Рет қаралды 36,552

Jacob Sorber

Jacob Sorber

Күн бұрын

Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.th...
Website ➤ www.jacobsorbe...
---
Students and static occasionally run into trouble, either because they use it when they shouldn't, don't use it when they should, or guess and end up right but don't understand why.
This video explains what "static" means in C (not C++ or Java) and how it changes a variable's scope and sometimes it's persistence.
Enjoy.
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
people.cs.clem...
persist.cs.clem... Note that any Amazon links in my video descriptions are generated by Amazon. If you click one of them and then buy something it helps support this channel. Thanks.
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorbe...
people.cs.clem...
persist.cs.clem...
To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon --- [ / jacobsorber ]
+ rep the channel with nerdy merch --- [teespring.com/...]
Source code is also available to Patreon supporters. --- [jsorber-youtub...]
Want me to review your code?
Email the code to js.reviews.code@gmail.com. Code should be simple and in one of the following languages: C, C++, python, java, ruby. You must be the author of the code and have rights to post it. Please include the following statement in your email: "I attest that this is my code, and I hereby give Jacob Sorber the right to use, review, post, comment on, and modify this code on his videos."
You can also find more info about code reviews here.
• I want to review your ...

Пікірлер: 52
@unchayndspersonalaccount7690
@unchayndspersonalaccount7690 3 жыл бұрын
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
@JacobSorber
@JacobSorber 3 жыл бұрын
I like it. Thanks.
@andrewnorris5415
@andrewnorris5415 Жыл бұрын
I'm sure the person who named them both static had that in mind ;)
@tonysofla
@tonysofla 4 жыл бұрын
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.
@keen2461
@keen2461 2 жыл бұрын
Oh thanks.....that's a really meaningful difference that should have been mentioned on the video. I guess few people know that.
@karimdhrif6679
@karimdhrif6679 Жыл бұрын
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
@shekharmaela2308
@shekharmaela2308 3 жыл бұрын
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.
@jti107
@jti107 6 жыл бұрын
this is fantastic! please continue making videos!!!
@JacobSorber
@JacobSorber 6 жыл бұрын
Thanks. Will do. Any specific requests?
@nukacoaal
@nukacoaal 6 жыл бұрын
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.
@JacobSorber
@JacobSorber 6 жыл бұрын
Sure. I'll add it to the future video list.
@sverkeren
@sverkeren 2 жыл бұрын
"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.
@sourestcake
@sourestcake 2 жыл бұрын
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.
@seal3081
@seal3081 6 жыл бұрын
Ya my first programming classes were in java was the static keyword in C was pretty weird to me.
@JacobSorber
@JacobSorber 5 жыл бұрын
I started in Pascal, but it was still weird to me.
@mwein198
@mwein198 4 жыл бұрын
OMG, finally a reasonable explanation! Thank you!
@JacobSorber
@JacobSorber 4 жыл бұрын
My pleasure.
@pablo_brianese
@pablo_brianese 4 жыл бұрын
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!
@zawette
@zawette 5 жыл бұрын
your videos are beyond amazing ! great quality
@JacobSorber
@JacobSorber 5 жыл бұрын
Thanks. I'm glad you enjoyed it.
@69k_gold
@69k_gold Жыл бұрын
When you learn C storage classes, it makes total sense about static
@WistrelChianti
@WistrelChianti 3 жыл бұрын
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.
@JacobSorber
@JacobSorber 3 жыл бұрын
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.
@WistrelChianti
@WistrelChianti 3 жыл бұрын
@@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
@JacobSorber 3 жыл бұрын
@@WistrelChianti Yep. Try it out.
@WistrelChianti
@WistrelChianti 3 жыл бұрын
@@JacobSorber will do! Ta!!
@navalsaini5155
@navalsaini5155 5 ай бұрын
@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?
@lean.drocalil
@lean.drocalil 2 жыл бұрын
Awesome, as usual!! 👊😎
@sun759
@sun759 4 жыл бұрын
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
@JacobSorber 4 жыл бұрын
Sounds good. A "static" local is just a local variable that persists across multiple invocations of the function.
@sun759
@sun759 4 жыл бұрын
@@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.
@alantao3810
@alantao3810 3 жыл бұрын
Love the intro!!!
@RAMB0VI
@RAMB0VI 3 жыл бұрын
But aren't function variables always visible only inside the function?
@carlosarcia5714
@carlosarcia5714 3 жыл бұрын
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
@sverkeren
@sverkeren 2 жыл бұрын
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.
@jimzhu7654
@jimzhu7654 3 жыл бұрын
Thank you so much!
@brahimdjadir2119
@brahimdjadir2119 4 жыл бұрын
amazing could !! you pleas explain where the weak key word is used
@simonhrabec9973
@simonhrabec9973 3 жыл бұрын
and this is not the only C/C++ keyword that has multiple meanings depending of its context :D
@ilyastarchenko9919
@ilyastarchenko9919 5 жыл бұрын
Where are static variables stored?
@JacobSorber
@JacobSorber 5 жыл бұрын
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.
@ilyastarchenko9919
@ilyastarchenko9919 5 жыл бұрын
@@JacobSorber thanks.
@chevalierdeloccident5949
@chevalierdeloccident5949 6 жыл бұрын
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.
@JacobSorber
@JacobSorber 6 жыл бұрын
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.
@chevalierdeloccident5949
@chevalierdeloccident5949 6 жыл бұрын
I imagine the keyword for «capability» would've been `capab` (something along those lines, like «print function» became `printf`). Have you covered macros? :D
@JacobSorber
@JacobSorber 6 жыл бұрын
I haven't, but it's one of the many topics that I have planned for the future.
@thux2828
@thux2828 3 жыл бұрын
Perhaps 'internal capability'
@abubakarshaik3508
@abubakarshaik3508 3 жыл бұрын
Hi jacob, can you please make a video on auto storage class..i am bit confused.
@abubakarshaik3508
@abubakarshaik3508 3 жыл бұрын
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 ❤️
@JacobSorber
@JacobSorber 3 жыл бұрын
Thanks. I'll see what I can do.
@WoWTipsAndGoldGuides
@WoWTipsAndGoldGuides 5 жыл бұрын
dope vid,
@pianochannel100
@pianochannel100 4 жыл бұрын
Why did you decide to be a professor? Just curious.
@JacobSorber
@JacobSorber 4 жыл бұрын
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. :)
Creating new processes with fork()!
6:21
Jacob Sorber
Рет қаралды 70 М.
The What, How, and Why of Void Pointers in C and C++?
13:12
Jacob Sorber
Рет қаралды 52 М.
Kind Waiter's Gesture to Homeless Boy #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 6 МЛН
Советы на всё лето 4 @postworkllc
00:23
История одного вокалиста
Рет қаралды 5 МЛН
CHOCKY MILK.. 🤣 #shorts
00:20
Savage Vlogs
Рет қаралды 27 МЛН
Look at two different videos 😁 @karina-kola
00:11
Andrey Grechka
Рет қаралды 12 МЛН
The Inline Keyword in C.
16:18
Jacob Sorber
Рет қаралды 57 М.
why do header files even exist?
10:53
Low Level Learning
Рет қаралды 389 М.
Making variables atomic in C
11:12
Jacob Sorber
Рет қаралды 36 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 786 М.
How to Check Your Pointers at Runtime
14:12
Jacob Sorber
Рет қаралды 31 М.
Strings can get you hacked! (buffer overflows, strcpy, and gets)
9:04
Make your Data Type more Abstract with Opaque Types in C
13:41
Jacob Sorber
Рет қаралды 49 М.
Bit Fields in C. What are they, and how do I use them?
13:26
Jacob Sorber
Рет қаралды 81 М.
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 20 М.
Can I Handle Exceptions with Try Catch in C? (setjmp, longjmp)
11:13
Kind Waiter's Gesture to Homeless Boy #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 6 МЛН