developingprogrammer
asked on
repeated initialisation of static variable doesn't overwrite previous value?
hey guys, i saw this piece of code.
when the countByTwo function is called repeatedly, it always hits the line
however from the main method 's comments, it seems that the currentCount variable isn't set to 0 again even though it hits the initialisation line multiple time.
Question: if we have a line "static int currentCount = 0;", when it is run multiple times LVVM will check if currentCount already has a value other than 0 and if it does it will skip the setting of the value to 0 is that correct?
thanks in advance guys!
// main.m
#import <Foundation/Foundation.h>
int countByTwo() {
static int currentCount = 0;
currentCount += 2;
return currentCount;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"%d", countByTwo()); // 2
NSLog(@"%d", countByTwo()); // 4
NSLog(@"%d", countByTwo()); // 6
}
return 0;
}
when the countByTwo function is called repeatedly, it always hits the line
static int currentCount = 0;
however from the main method 's comments, it seems that the currentCount variable isn't set to 0 again even though it hits the initialisation line multiple time.
Question: if we have a line "static int currentCount = 0;", when it is run multiple times LVVM will check if currentCount already has a value other than 0 and if it does it will skip the setting of the value to 0 is that correct?
thanks in advance guys!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
whao perfect! thanks ozo! i do have some problems finding help for some words and i am trying to learn how i can self help instead of asking silly questions. do you think you can help me take a look at this other question ozo - relating to self help on objective c? thanks! = )
https://www.experts-exchange.com/questions/28377602/how-to-search-help-for-words-like-extern-define.html
https://www.experts-exchange.com/questions/28377602/how-to-search-help-for-words-like-extern-define.html
ASKER
sorry for being dense here - but so it means to say that the initialisation line will be skipped each time the countByTwo function is called - because it has already been initialised before programme startup - is that correct?
thanks once again ozo!