The Static Keyword in C

Sdílet
Vložit
  • čas přidán 9. 09. 2024

Komentáře • 52

  • @unchayndspersonalaccount7690

    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

  • @karimdhrif6679
    @karimdhrif6679 Před rokem +3

    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

  • @tonysofla
    @tonysofla Před 4 lety +8

    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 Před 3 lety

      Oh thanks.....that's a really meaningful difference that should have been mentioned on the video. I guess few people know that.

  • @jti107
    @jti107 Před 6 lety +23

    this is fantastic! please continue making videos!!!

    • @JacobSorber
      @JacobSorber  Před 6 lety +5

      Thanks. Will do. Any specific requests?

    • @nukacoaal
      @nukacoaal Před 6 lety +2

      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  Před 6 lety +3

      Sure. I'll add it to the future video list.

  • @sverkeren
    @sverkeren Před 2 lety +6

    "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.

  • @shekharmaela2308
    @shekharmaela2308 Před 3 lety +7

    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.

  • @sourestcake
    @sourestcake Před 2 lety +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.

  • @mwein198
    @mwein198 Před 4 lety +2

    OMG, finally a reasonable explanation!
    Thank you!

  • @pablo_brianese
    @pablo_brianese Před 4 lety

    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!

  • @WistrelChianti
    @WistrelChianti Před 3 lety +2

    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  Před 3 lety +1

      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 Před 3 lety

      @@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  Před 3 lety

      @@WistrelChianti Yep. Try it out.

    • @WistrelChianti
      @WistrelChianti Před 3 lety

      @@JacobSorber will do! Ta!!

  • @69k_gold
    @69k_gold Před rokem

    When you learn C storage classes, it makes total sense about static

  • @zawette
    @zawette Před 5 lety +1

    your videos are beyond amazing ! great quality

  • @seal3081
    @seal3081 Před 6 lety +3

    Ya my first programming classes were in java was the static keyword in C was pretty weird to me.

    • @JacobSorber
      @JacobSorber  Před 5 lety

      I started in Pascal, but it was still weird to me.

  • @navalsaini5155
    @navalsaini5155 Před 6 měsíci

    @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?

  • @alantao3810
    @alantao3810 Před 3 lety +1

    Love the intro!!!

  • @sun759
    @sun759 Před 4 lety +1

    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  Před 4 lety +1

      Sounds good. A "static" local is just a local variable that persists across multiple invocations of the function.

    • @sun759
      @sun759 Před 4 lety

      @@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.

  • @lean.drocalil
    @lean.drocalil Před 2 lety

    Awesome, as usual!! 👊😎

  • @chevalierdeloccident5949
    @chevalierdeloccident5949 Před 6 lety +2

    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  Před 6 lety +1

      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 Před 6 lety

      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  Před 6 lety +1

      I haven't, but it's one of the many topics that I have planned for the future.

    • @thux2828
      @thux2828 Před 3 lety

      Perhaps 'internal capability'

  • @RAMB0VI
    @RAMB0VI Před 4 lety +2

    But aren't function variables always visible only inside the function?

    • @carlosarcia5714
      @carlosarcia5714 Před 3 lety

      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 Před 2 lety

      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.

  • @simonhrabec9973
    @simonhrabec9973 Před 3 lety

    and this is not the only C/C++ keyword that has multiple meanings depending of its context :D

  • @jimzhu7654
    @jimzhu7654 Před 3 lety

    Thank you so much!

  • @brahimdjadir2119
    @brahimdjadir2119 Před 4 lety +1

    amazing could !! you pleas explain where the weak key word is used

  • @ilyastarchenko9919
    @ilyastarchenko9919 Před 5 lety +2

    Where are static variables stored?

    • @JacobSorber
      @JacobSorber  Před 5 lety +2

      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 Před 5 lety

      @@JacobSorber thanks.

  • @abubakarshaik3508
    @abubakarshaik3508 Před 3 lety

    Hi jacob, can you please make a video on auto storage class..i am bit confused.

    • @abubakarshaik3508
      @abubakarshaik3508 Před 3 lety

      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  Před 3 lety

      Thanks. I'll see what I can do.

  • @WoWTipsAndGoldGuides
    @WoWTipsAndGoldGuides Před 5 lety

    dope vid,

  • @pianochannel100
    @pianochannel100 Před 4 lety

    Why did you decide to be a professor? Just curious.

    • @JacobSorber
      @JacobSorber  Před 4 lety +1

      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. :)