What is a static variable?

Sdílet
Vložit
  • čas přidán 8. 02. 2019
  • Across the programming universe a "static" variable can mean many things. In this video we'll talk about what it actually means in C.
  • Věda a technologie

Komentáře • 76

  • @fun-damentals6354
    @fun-damentals6354 Před 4 měsíci +3

    that is the most intuitive explanation i have found for static. you are so good at explaining things

  • @ehoihere
    @ehoihere Před rokem +11

    A simple but very visual example, making the subject quick to understand. Thanks a lot for this video.

  • @muhammadluqman3452
    @muhammadluqman3452 Před 2 lety +13

    Thank you a lot my brother , this was the best explanation about differences between regular and static variables , good bless you!.

  • @mirzakadic9174
    @mirzakadic9174 Před rokem +2

    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!

  • @giacomol02
    @giacomol02 Před 2 lety +2

    Thanks a lot, very clear! i couldn't ask for anything better than this!

  • @youseftalal2638
    @youseftalal2638 Před 3 lety +3

    Thank you for this simple and powerful explanation

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

    Nice video, I used to make the mistake of thinking static and const were the same. I doubt I was the only one.

  • @user-qm4or3bt4v
    @user-qm4or3bt4v Před 4 lety +3

    Brilliant as always

  • @nrted3877
    @nrted3877 Před 2 lety

    awesome explanation!! I was just scratching my head by looking bookish definitions

  • @HeatherHolt
    @HeatherHolt Před 3 měsíci

    your videos are very helpful, thank you!!

  • @sergeiabramov282
    @sergeiabramov282 Před 2 lety

    Great explanation, man!

  • @EccentricTuber
    @EccentricTuber Před 2 lety +1

    This video made me finally understand static variables

  • @kyledrewes6552
    @kyledrewes6552 Před 2 lety

    This helped me a lot. Thank you very much.

  • @ankitsrivastavaMV
    @ankitsrivastavaMV Před rokem

    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

  • @shashwatkumarsoni54cse39

    Awesome explanation 👏

  • @kaoutharlakhder8449
    @kaoutharlakhder8449 Před 2 lety

    I love this , following from Morocco ✌🏻

  • @bhadriramlella7697
    @bhadriramlella7697 Před rokem

    It was really helpful and that helped me a lot.

  • @ihffbd9719
    @ihffbd9719 Před 2 lety

    Thanks Sir for clearing my doubts.

  • @codex8797
    @codex8797 Před 2 lety +1

    Thanks bro your videos are really helpful

  • @williammoura8829
    @williammoura8829 Před 2 lety

    YOU'RE AMAZING!

  • @aleceastman
    @aleceastman Před rokem

    great explanation, thank you

  • @xxxmin0uxxx858
    @xxxmin0uxxx858 Před rokem

    thanks a lot brother appreciate that

  • @ysabelfuentespinto2812

    very clear! thanks

  • @AashishTheThakur
    @AashishTheThakur Před 2 lety +1

    very good Thanku🎉

  • @muhammeddikal8551
    @muhammeddikal8551 Před 2 lety

    nice explanation thanks

  • @izazahmed5978
    @izazahmed5978 Před rokem

    Thank you so much.

  • @slasankautube
    @slasankautube Před rokem

    wow totally understand in one go , thanks by subed and liked

  • @manuelconte2127
    @manuelconte2127 Před 2 lety

    very clear!

  • @santhoshk2722
    @santhoshk2722 Před rokem

    thanks to clear my dought.

  • @dhanushammayappan9419

    Please upload more videos bro. Its really helpful for students like me

  • @edgbaston149
    @edgbaston149 Před 2 lety

    Thank you so much

  • @wahabfiles6260
    @wahabfiles6260 Před 4 lety

    Wow! Nice!

  • @mohamedbenslimen978
    @mohamedbenslimen978 Před 2 lety

    saved me 10 hours before exam ; keep it up

  • @mikeros6388
    @mikeros6388 Před 2 lety

    you just save me men, tanks from Mex

  • @IvanDGr8
    @IvanDGr8 Před rokem

    Thanks!!!

  • @honesty2272
    @honesty2272 Před 2 měsíci

    Best Explain in simple english

  • @knownlearner568
    @knownlearner568 Před 2 lety

    great!

  • @vimukthikulasekara2109
    @vimukthikulasekara2109 Před 2 lety +1

    woah thats goooooooooooooooooooooooooooooooooooooooooooooooood thank you very much

  • @fadieid5638
    @fadieid5638 Před rokem

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

      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

  • @audiodiwhy2195
    @audiodiwhy2195 Před rokem

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

      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 Před rokem

    Hi there!
    can you make a video about static variables in structs - in C? Would be a great topic!

  • @mehmetcivan
    @mehmetcivan Před 2 lety

    perfect...

  • @lucasferrini7063
    @lucasferrini7063 Před 3 lety

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

      Yes, it can. It can store anything any other normal variable can

  • @abdelmouizmamouni
    @abdelmouizmamouni Před 2 lety

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

      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

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

    What will be the default value of static storage class variable and when will it be a global variable.👍

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

      static variables are technically just like global variables but with their scope limited to their function. So the default value for them is 0.

  • @cezzar5233
    @cezzar5233 Před 2 lety +1

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

      Are you incrementing after every print you're doing? Can you send me your whole source code?

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

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

      @@CodeVault Thank you! GLHF!

  • @ritikpandey5939
    @ritikpandey5939 Před 2 lety

    Love from india

  • @goedeck1
    @goedeck1 Před 2 lety +1

    Can a static variable be declared wit
    hout initialization?
    static int x;
    x=3;

    • @CodeVault
      @CodeVault  Před 2 lety +1

      Yes! It behaves the same as uninintialized global variables. That x will have the value of 0 by default

  • @----700
    @----700 Před 8 měsíci

    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  Před 8 měsíci

      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

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

      Global vs Local; not Internal vs External

  • @ahmedegymed5853
    @ahmedegymed5853 Před 2 lety

    Thanks ,so this is true for c++ also ?

    • @CodeVault
      @CodeVault  Před 2 lety +1

      Yes, although, "static" in the context of fields and methods in classes has a different meaning in C++

  • @Jonathan-ru9zl
    @Jonathan-ru9zl Před 2 měsíci +1

    Hi! What static mean outside of a function?

    • @CodeVault
      @CodeVault  Před 2 měsíci +1

      Basically it marks the identifier is "private" and cannot be shared between compilation units. I'll make a video on this actually

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

    Could you also give some practical applications of static variables in embedded systems?
    Thanks 😊

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

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

      @@CodeVault
      Thank you. Another example that I could think of is to use static for variables that we could use as counters.

  • @3P5C1
    @3P5C1 Před 2 lety +1

    Somehow the name 'static' seems completely counter-intuitive for what it is.
    Maybe it's just me.
    Thanks for the explanation.

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

    No complaints, however I believe you could have explained it better with memory layout of a C program.

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

      Memory layout? You mean have a visual diagram of sorts?

    • @k_ushagra
      @k_ushagra Před 3 lety

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

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

      @@CodeVault oh okay I understand and I respect your teaching methods.
      I was mere curious that's all.