I just wanted to know whether the keyword ‘static’ has the same meaning when it is used with respect to an array variables as well as normal variables ? For instance I do know that static variables once declared have the lifetime of the program and static array means that the size of the array cannot be changed. However I wanted to clarify whether static array variables also have the lifetime of the program and are not initialized over and over again in the function in which they are defined , even if the function is called multiple times ? Or does the keyword ‘static’ with respect to an array only put limits on the size of the array ?
If no , could you please suggest a method by which I could make array variables static so that they can be passed between functions and also have the lifetime of the program like normal static variables?
I am currently using the C language. Please bear with me if this is a bit silly question , I am quite new to prgramming. Thank you !!
A variable declared at block scope as static
, or at file scope, has static storage duration and matches the lifetime of the program. This is equally true for arrays and non-arrays.
Arrays cannot have their size changed in any case . The size specified in the declaration is the size of the array for its lifetime .
If you are talking about the keyword static
inside square brackets of an array declarator in a function parameter (e.g. void f(int x[static 2]);
) then this is an entirely different thing (the same keywords has been reused for unrelated purposes) and it means the function may be optimized as if it were only called with an array of at least that size.