A question about static.

This is a discussion on A question about static. within the C Programming forums, part of the General Programming Boards category; From what I understand, in a function, static makes the variable retain it's value between calls to the function. What ...

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    A question about static.

    From what I understand, in a function, static makes the variable retain it's value between calls to the function. What about initialization, say I have this function:
    Code:
    int functiona(void)
    {
        static int number_of_times_called = 0;
        return ++number_of_times_called;
    }
    Would the variable be initialized to 0 every time I call this, or would it only be initialized to 0 once, retaining it's value from the last call to "functiona"?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    It would be initialised to 0 on the first call to the function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    What's the difference between const and static?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    const is an abbreviation for constant, and something that is constant should not be changed. static depends on context; you have mentioned one in this thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Const is not suppose to change at all.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can change the value of a static variable, you shouldn't be trying to change the value of a constant variable.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I think I read somewhere that static used on a global makes it only visible to the source being compiled, is that true, and if so, is that also true for const?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by User Name:
    I think I read somewhere that static used on a global makes it only visible to the source being compiled, is that true
    Yes.

    Quote Originally Posted by User Name:
    is that also true for const?
    I do not think so, but I do not have a copy of the C standard at hand to verify, nor do I have a compiler installed. You could check it for yourself, just as you could have checked the answer for your original post in this thread
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by User Name: View Post
    is that also true for const?
    It would be true for a "static const", but for sure not for just const, no.

    Quote Originally Posted by laserlight View Post
    You could check it for yourself, just as you could have checked the answer for your original post in this thread
    Eek! That would require compiling some code!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    just as you could have checked the answer for your original post in this thread
    True. I thought of that right after posting, but I couldn't find the delete thread button. But look at the bright side, I'm sure you didn't get to 12104 by only answering good questions, true?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by User Name: View Post
    True. I thought of that
    As long as everyone is on the same page with this then that's great
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by laserlight View Post
    It would be initialised to 0 on the first call to the function.
    I would say it's initialized to 0 when the program starts executing. Part of initialization procedure... same as where global variables are initialized. i.e. not part of the execution thread.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by User Name: View Post
    True. I thought of that right after posting, but I couldn't find the delete thread button. But look at the bright side, I'm sure you didn't get to 12104 by only answering good questions, true?
    As they say, there are no bad questions, only dumb questions.
    Others can learn from your mistakes and curiosity. That is why most boards don't want to delete threads, and this one is no different.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    So basically, it's the abstract form of declaring an integer of the value 0 in the '.data' segment(In other words, it's never initialized, the '.data' segment is initialized at compile time.)? Sometimes the abstraction of HLLs can lead to ambiguity as to what is really being done, especially for someone like me, who sees everything in asm. It's the different structures within the sources that lead to my confusion.

    And a const would be in '.rodata', I assume?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by User Name:
    So basically, it's the abstract form of declaring an integer of the value 0 in the '.data' segment(In other words, it's never initialized, the '.data' segment is initialized at compile time.)? Sometimes the abstraction of HLLs can lead to ambiguity as to what is really being done, especially for someone like me, who sees everything in asm.
    With respect to the C standard, an object with static storage duration (this include global variables) has a lifetime that is the whole period of execution of the program, and it is initialised before program start-up. The details are implementation defined, so yes, what is really being done is deliberately ambiguous

    Quote Originally Posted by User Name:
    And a const would be in '.rodata', I assume?
    Not really: const may apply to a given context, e.g., you might have a const int* parameter for a function, thus declaring that the int is not to be changed via this pointer parameter. But the int variable in the function that calls this function may well be non-const.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Static Initilization question
    By tezcatlipooca in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2006, 06:09 PM
  3. Static lib question
    By X PaYnE X in forum Windows Programming
    Replies: 2
    Last Post: 08-08-2006, 11:55 AM
  4. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 08:54 AM
  5. question about static members
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2003, 09:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21