2

Is there any difference in these two? If so, what exactly is the difference? Assume they are in a C function that may be called multiple times.

  1. declare and assign in same statement

    static uint32_t value = x; // x varies and may be passed into function.

  2. declare in one statement and assign in next statment.

    static uint32_t value;

    value = x; // x varies;

Is value updated only the first time it is declared/initialized or even on subsequent calls.

My understanding of (1) is that it is only set the first time that line is executed so even if x changes the next time the line executes, value will remain the same. I am not sure about (2) but clarification on both will be very helpful

EDIT: Compiler ARM(ADS1.20). EDIT: A follow up question on (2) from the answers given so far. Is the declaration(not the assignment) repeated on every call or just the first time?

5
  • Since you make it quite clear in your question that your code is in C, I removed the c++ tag. Those languages are not the same, and the answer to your question is highly dependent on which you use. Commented Mar 30, 2009 at 16:22
  • no i think he talks about C++. in C 1.) is not possible. or... well i'm not sure either. looking at the answers, they all take it as the questioner made a mistake with 1) . let's wait what MeThinks says Commented Mar 30, 2009 at 16:45
  • The only place that mentioned C++ was the C++ tag (now removed). The question title and body both said "C" and made no mention of C++. Commented Mar 30, 2009 at 17:51
  • thanks. My bad on the C++ tag. Thanks for the correction. This is indeed C on ARM compiler(ADS1.2) compiler. Commented Mar 30, 2009 at 21:25
  • Re followup: the declaration isn't repeated on every call; the static variable exists for the lifetime of the program. Commented Mar 30, 2009 at 22:56

6 Answers 6

11

The first should not compile; the static variable requires a constant initializer.

The second sets value each time the function is called, so there was no need to make it static in the first place.

If the first notation was correct - initialized value to 1, say - then it would be initialized once when the program starts and would thereafter only take new values when the code changed it. The second notation still sets value on each call to the function, and so renders the use of static pointless. (Well, if you try hard enough, you can devise scenarios under which the second version has a use for static. For example, if the function returns a pointer to it that other code then modifies, then it might be needed, but that is esoteric in the extreme and would be a pretty bad 'code smell'.)

Sign up to request clarification or add additional context in comments.

7 Comments

The first example compiles for me.
Or the value of x could depend on previous the previous value of value. Still seems like a code smell, like you say.
Please check C++ standard 6.7.4. First example given is perfectly valid.
See section 6.7 paragraph 4 in the 2003 C++ spec. Static variables can be initialized with both const and non-const initializers/expressions. If initialized with a non-const expression, the object is initialized the first time control passes through the declaration.
However, this might not be true in C. Maybe the question should be retagged to avoid confusion.
|
2

1 is only executed once, but for 2 value will be reassigned every time. Static variables are initialized only once.

3 Comments

The first does not compile - the initializer must be a constant.
not if the questioner actually talked about C++ tho (given that he tagged it as C++). i often saw question where they asked about C++, but only wrote "C" but tagged as C++ (sadly..) . but also saw they really meant C but tagged C++ . let's wait for MeThinks
The question is explicit about C function, both in the title and the main text; the C++ tag was misplaced, I think.
2

These are very different declarations.

The first one is declaring a static local variable and giving it an initial value (this should not actually compile given that x is not a constant). This will only occur once before the function is every executed. This is almost certainly the initialization you want.

The second declaration is updating the value every time the function is called. If you want the variable to always start the function with the same value this is the right approach. But if this is truly what you want, then why use a static at all? Just use a local variable.

1 Comment

In 2, I was under the impression the declarion is only when the function is called the first time and the assignment is every time the function is called. Is this correct understanding? Is there any benefit in declaring only once and only doing the assignment on every call?
0

Your intuition is right. In the second example value is set to x each time the method is called. Static variables need to be initialized and declared in one statement if you only want it to run once.

If you always want value to have the value x, don't declare it as static.

Comments

0

When compiled, the first one will be put into the ".data" section, where data is initialized, while the second one will be put into the ".bss" section, where data is uninitialized.

Use readelf -S xx.o can check the section size of compiled object file.

example 1: static int i;

void test(){
    i = 2; 
}

example 2:

 static int i=1;

void test(){
    i = 2; 
}

Comments

0

Folks - In C, the first declaration is perfectly legal. It will compile, and could be used to initialize the value. You could combine the line of code from the second declaration to ensure it gets updated every subsequent function execution. This is commonly used, in particular in embedded programs where memory and resources are more scarce than computers or distributed applications.

The reason why you would use a static is to ensure the variable has a data lifecyle that continues throughout program execution while limiting its access to only the function the static is declared, or any function in the file if the static declaration is on top of the file, otherwise, the data will be lost every time the function is exited. This is good programming practice to avoid inadvertent access to data objects that must be secured and restricted. That comment only applies to the C programming language, don't mistake this to apply for C++ (where it does in some instances), or JAVA. Static in Java has a completely different meaning. From what I've read in this thread, few seem to understand how the keyword static works in C, and are confusing the keyword static form other languages to apply in C. In C, static is a very important keyword that helps manage function and data access, and you can initialize a static variable with another variable provided it is within scope, and you can update that value throughout program execution, which would probably what you need to do anyways.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.