Yes, all members are initialized for objects with static storage. See 6.7.8/10 in the C99 Standard (PDF document)

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these rules.

To initialize everything in an object, whether it's static or not, to 0, I like to use the universal zero initializer

sometype identifier0 = {0};
someothertype identifier1[SOMESIZE] = {0};
anytype identifier2[SIZE1][SIZE2][SIZE3] = {0};

There is no partial initialization in C. An object either is fully initialized (to 0 of the right kind in the absence of a different value) or not initialized at all.
If you want partial initialization, you can't initialize to begin with.

int a[2]; // uninitialized
int b[2] = {42}; // b[0] == 42; b[1] == 0;
a[0] = -1; // reading a[1] invokes UB
Answer from pmg on Stack Overflow
Top answer
1 of 5
68

Yes, all members are initialized for objects with static storage. See 6.7.8/10 in the C99 Standard (PDF document)

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these rules.

To initialize everything in an object, whether it's static or not, to 0, I like to use the universal zero initializer

sometype identifier0 = {0};
someothertype identifier1[SOMESIZE] = {0};
anytype identifier2[SIZE1][SIZE2][SIZE3] = {0};

There is no partial initialization in C. An object either is fully initialized (to 0 of the right kind in the absence of a different value) or not initialized at all.
If you want partial initialization, you can't initialize to begin with.

int a[2]; // uninitialized
int b[2] = {42}; // b[0] == 42; b[1] == 0;
a[0] = -1; // reading a[1] invokes UB
2 of 5
3

Yes, they are, as long they have static or thread storage duration.

C11 (n1570), § 6.7.9 Initialization #10

If an object that has static or thread storage duration is not initialized explicitly, then:

[...]

  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

[...]

🌐
Reddit
reddit.com › r/c_programming › initializing a static variable and assigning it a value in the same line doesn't work but in separate lines does? can't understand the logic here...
r/C_Programming on Reddit: Initializing a static variable and assigning it a value in the same line doesn't work but in separate lines does? Can't understand the logic here...
March 13, 2023 -

The following code:

#include <stdio.h>
#define SIZE 5

unsigned fun();
void sod(unsigned*, unsigned);

void main()
{
	unsigned arr[SIZE], i
		;
	sod(arr, SIZE);
	for (i = 0; i < SIZE; i++)
		printf("%u ", arr[i]);
}

unsigned fun()
{
	static unsigned value = 0;
	value = !value ? 1 : value << 1;
	value = !value ? 1 : value;
	return value;
}

void sod(unsigned* arr, unsigned n)
{
	unsigned i;
	for (i = 0; i < n; i++)
		arr[i] = fun();
}

Outputs:

1 2 4 8 16

On the other hand, change this:

static unsigned value = 0;

to this:

static unsigned value;
value = 0;

And the resulting output is:

1 1 1 1 1

Now I would expect the latter to be the output for both cases because we're initializing value to be 0 at every run of "fun". The way it might work in my mind is if value is initializing like this:

static unsigned value;

So we're initializing it to 0 but not changing it at every run of "fun"

the result for this is also as expected:

1 2 4 8 16

So I don't understand the logic here... Is it something with the static keyword or is it because I'm initializing it twice?

Top answer
1 of 5
12
A static variable is initialized once at some point in time before the function is called. So saying static int foo = 7; initializes foo to 7 at some point in time before the function is called. However, static int foo; foo = 7; assigns 7 to foo every time the function is called. The difference is that the assignment is not part of the declaration statement, it's a separate assignment statement. It's important to understand the difference between an initialization and an assignment, which can be misleading because they both use the = symbol. An initialization is part of a variable declaration and the initialization may happen at a time other than when the line is encountered when the program runs. It also has limitations, such as static initializations required to be constant expressions. An assignment is similar, but its effect happens every time the thread of execution goes through the assignment expression. It works as you'd expect with no caveats.
2 of 5
5
Static value initialization is done only once, and has to be done all in one statement. This: static unsigned value; value = 0; Isn’t initialization, this is a declaration followed by an assignment. So you’ve declared a static variable in one statement, then in your next statement you assign 0 to it. Since this assignment is not afforded the privilege of being done only once, you’re setting it’s value to 0 at the start of the function every time you call this function, hence the repeated values.
Discussions

Why should I initialize static class variables in C++? - Stack Overflow
In C and C++ all static variables are initialized by default to ZERO. This is not the case of static class data members. Why is that? #include using namespace std; int var; class More on stackoverflow.com
🌐 stackoverflow.com
Initialize static variables in C++ class? - Stack Overflow
I have noticed that some of my functions in a class are actually not accessing the object, so I made them static. Then the compiler told me that all variables they access must also be static – well, More on stackoverflow.com
🌐 stackoverflow.com
initializing static variable - C++ Forum
I was first introduced to keyword static when going over singleton classes and then saw it again when trying to prevent a class from instantiating on the stack. Now I am trying to simply initialize a static variable, and after many trials I could not seem to do it inside the class unless it ... More on cplusplus.com
🌐 cplusplus.com
March 5, 2022
The ins and outs of local static variable initialization
how the program know that a static variable has been declared You probably mean “initialized”, not “declared”. Each local static variable is associated with a distinct boolean flag that's zero-initialized at program startup. This flag is checked every time execution passes through the point of declaration of the variable. The first time the execution in some (any) thread passes through the declaration, the variable is initialized and the flag is set to indicate that, in a thread safe way. So, every static local variable has a little overhead. However, that overhead is marginal, and even though the initialization is thread safe, after initialization that overhead is not higher than just a check of the flag. More on reddit.com
🌐 r/cpp_questions
5
1
September 16, 2023
People also ask

What is a static variable in C?
A static variable in C is a variable that retains its value between function calls, ensuring it persists throughout the program's execution.
🌐
upgrad.com
upgrad.com › home › blog › software development › static variables in c explained: what they are and how to use them effectively in 2025
Static Variables in C Explained: What They Are and How to Use Them ...
Why use static variables in C?
Static variables are useful for maintaining state across multiple function calls, without the overhead of global variables.
🌐
upgrad.com
upgrad.com › home › blog › software development › static variables in c explained: what they are and how to use them effectively in 2025
Static Variables in C Explained: What They Are and How to Use Them ...
🌐
Cppreference
en.cppreference.com › w › cpp › language › initialization.html
Initialization - cppreference.com
December 31, 2024 - It is implementation-defined whether dynamic initialization happens-before the first statement of the main function (for statics) or the initial function of the thread (for thread-locals), or deferred to happen after. If the initialization of a non-inline variable(since C++17) is deferred to happen after the first statement of main/thread function, it happens before the first ODR-use of any variable with static/thread storage duration defined in the same translation unit as the variable to be initialized.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › g-fact-80
Initialization of Static Variables in C - GeeksforGeeks
December 26, 2024 - If no value is provided during initialization, static variables are automatically initialized to zero for basic data types like int, float, etc., and to NULL for pointers. ... #include<stdio.h> int main() { // Creating and initializing static ...
Top answer
1 of 10
189

They can't be initialised inside the class, but they can be initialised outside the class, in a source file:

// inside the class
class Thing {
    static string RE_ANY;
    static string RE_ANY_RELUCTANT;
};

// in the source file
string Thing::RE_ANY = "([^\\n]*)";
string Thing::RE_ANY_RELUCTANT = "([^\\n]*?)";

Update

I've just noticed the first line of your question - you don't want to make those functions static, you want to make them const. Making them static means that they are no longer associated with an object (so they can't access any non-static members), and making the data static means it will be shared with all objects of this type. This may well not be what you want. Making them const simply means that they can't modify any members, but can still access them.

2 of 10
53

Some answers including even the accepted answer seem to be a little misleading.

You don't have to

  • Always assign a value to static objects when initializing because that's optional.
  • Create another .cpp file for initializing since it can be done in the same header file.

You can even initialize a static object in the same class scope just like a normal variable using the inline keyword.


Initialize with no values in the same file

#include <string>
class A
{
    static std::string str;
    static int x;
};
std::string A::str;
int A::x;

Initialize with values in the same file

#include <string>
class A
{
    static std::string str;
    static int x;
};
std::string A::str = "SO!";
int A::x = 900;

Initialize in the same class scope using the inline keyword

#include <string>
class A
{
    static inline std::string str = "SO!";
    static inline int x = 900;
};
Find elsewhere
🌐
Quora
quora.com › When-does-a-static-variable-get-initialized-in-C-C-Is-it-at-compile-time-or-at-runtime-before-main-starts
When does a static variable get initialized in C/C++? Is it at compile time or at runtime before main() starts? - Quora
Answer (1 of 3): > When does a static variable get initialized in C/C++? Is it at compile time or at runtime before main() starts? Here’s what the C standard says: > An object whose identifier is declared without the storage-class specifier [code ]_Thread_local[/code], and either with external...
🌐
SkillVertex
skillvertex.com › blog › initialization-of-static-variables-in-c
Initialization of static variables in C
May 10, 2024 - In C, static variables must be initialized using constant literals. If you attempt to initialize a static variable with a non-constant value or an expression that cannot be determined at compile time, the program will indeed fail to compile.
🌐
Upgrad
upgrad.com › home › blog › software development › static variables in c explained: what they are and how to use them effectively in 2025
Static Variables in C Explained: What They Are and How to Use Them Effectively in 2025
January 7, 2025 - Initialization Outside Class: Static variables must be initialized outside the class definition. This is done using int MyClass::staticVar = 0; in the source file. Constructor: Each time an object is created, the static variable staticVar is ...
🌐
Cppreference
en.cppreference.com › w › cpp › language › static.html
static members - cppreference.com
July 23, 2025 - Local classes (classes defined inside functions) and unnamed classes, including member classes of unnamed classes, cannot have static data members. If a static data member of integral or enumeration type is declared const (and not volatile), it can be initialized with an initializer in which every expression is a constant expression, right inside the class definition:
🌐
Unstop
unstop.com › home › blog › static variable in c | a comprehensive guide with coding examples
Static Variable In C | A Comprehensive Guide With Coding Examples
July 31, 2024 - The process of declaring and initializing a static variable in C involves specifying the static keyword along with the variable's data type and optionally providing an initial value.
🌐
Quora
quora.com › How-does-a-static-variable-declared-inside-a-function-behave-C-static-development
How does a static variable declared inside a function behave (C++, static, development)? - Quora
May 16, 2021 - By C/C++ standard local static variables are initialized on first use but many compilers, like in above case, initialize them on very startup together with global variables. If static variable is not initialized its value should be set to 0. ...
🌐
Cplusplus
cplusplus.com › forum › beginner › 282511
initializing static variable - C++ Forum
March 5, 2022 - Class definitions are normally put in header files so if the static variable inside the class definition was a variable definition it would lead to multiple definitions because it would get defined in each translation unit where it is included. That's why it's only a declaration so you can define it outside the class in a .cpp file where it only gets defined once.
🌐
Bogotobogo
bogotobogo.com › cplusplus › statics.php
C++ Tutorial: Static Variables and Static Class Members - 2020
July 7, 2017 - Because the member is initialized outside the class definition, we must use fully qualified name when we initialize it: ... The code initializes the static id member to 100. Note again that we cannot initialize a static member variable inside the class declaration.
🌐
Reddit
reddit.com › r/cpp_questions › the ins and outs of local static variable initialization
r/cpp_questions on Reddit: The ins and outs of local static variable initialization
September 16, 2023 -

Hi! I'm looking for an up-to-date article/guide/blog post on the initialization of local static variables. Specifically I am interested in learning i.) how the program know that a static variable has been declared, ii.) more how to distinction between an identifier and a variable is present in program code/compiled code (i.e. how again does the program know exactly to what memory address to look for etc.). Thanks!

Top answer
1 of 3
2
how the program know that a static variable has been declared You probably mean “initialized”, not “declared”. Each local static variable is associated with a distinct boolean flag that's zero-initialized at program startup. This flag is checked every time execution passes through the point of declaration of the variable. The first time the execution in some (any) thread passes through the declaration, the variable is initialized and the flag is set to indicate that, in a thread safe way. So, every static local variable has a little overhead. However, that overhead is marginal, and even though the initialization is thread safe, after initialization that overhead is not higher than just a check of the flag.
2 of 3
2
how the program know that a static variable has been declared You put static there, so its declared as a static...? I assume you meant "how does the program know the variable has been initialized?" First, its important to note that this is an implementation detail and afaik not specified by the standard at all. I suspect (I really have no knowledge of this) that there is an additional byte (somewhere) that indicates whether initialization took place. more how to distinction between an identifier and a variable is present in program code/compiled code After the program is compiled, identifiers no longer exist. A compiled program just knows about memory addresses. how again does the program know exactly to what memory address to look for etc This depends on whether we are talking about dynamically allocated vs statically allocated. In the later case it keeps track of where on the stack it is and then can calculate how to get from there, to the value it is interested in. In the former case, it simply loads a memory address from the stack first and the loads from that memory address.
Top answer
1 of 5
40

Fundamentally this is because static members must be defined in exactly one translation unit, in order to not violate the One-Definition Rule. If the language were to allow something like:

struct Gizmo
{
  static string name = "Foo";
};

then name would be defined in each translation unit that #includes this header file.

C++ does allow you to define integral static members within the declaration, but you still have to include a definition within a single translation unit, but this is just a shortcut, or syntactic sugar. So, this is allowed:

struct Gizmo
{
  static const int count = 42;
};

So long as a) the expression is const integral or enumeration type, b) the expression can be evaluated at compile-time, and c) there is still a definition somewhere that doesn't violate the one definition rule:

file: gizmo.cpp

#include "gizmo.h"

const int Gizmo::count;
2 of 5
13

In C++ since the beginning of times the presence of an initializer was an exclusive attribute of object definition, i.e. a declaration with an initializer is always a definition (almost always).

As you must know, each external object used in C++ program has to be defined once and only once in only one translation unit. Allowing in-class initializers for static objects would immediately go against this convention: the initializers would go into header files (where class definitions usually reside) and thus generate multiple definitions of the same static object (one for each translation unit that includes the header file). This is, of course, unacceptable. For this reason, the declaration approach for static class members is left perfectly "traditional": you only declare it in the header file (i.e. no initializer allowed), and then you define it in a translation unit of your choice (possibly with an initializer).

One exception from this rule was made for const static class members of integral or enum types, because such entries can for Integral Constant Expressions (ICEs). The main idea of ICEs is that they are evaluated at compile time and thus do no depend on definitions of the objects involved. Which is why this exception was possible for integral or enum types. But for other types it would just contradict the basic declaration/definition principles of C++.

🌐
IBM
ibm.com › docs › en › zos › 2.4.0
Initialization and storage classes
We cannot provide a description for this page right now
🌐
pablo arias
pabloariasal.github.io › 2020 › 01 › 02 › static-variable-initialization
C++ - Initialization of Static Variables | pablo arias
January 2, 2020 - There is, however, a category of variables that can (and should) be initialized before the program starts: static variables. Global (namespace) variables or static class members1 live for the entire execution of the program: they must be initialized before main() is run and destroyed after ...
🌐
Cppreference
en.cppreference.com › w › cpp › language › constant_initialization.html
Constant initialization - cppreference.com
May 13, 2025 - Constant initialization usually happens when the program loads into memory, as part of initializing the program's runtime environment. ... #include <iostream> #include <array> struct S { static const int c; }; const int d = 10 * S::c; // not a constant expression: S::c has no preceding // ...
🌐
Quora
quora.com › How-do-you-initialize-private-static-members-in-C
How to initialize private static members in C++ - Quora
February 7, 2025 - Answer: A lot of students find this one tricky. In your header file, you’ll have the static member DECLARATION… something like this: [code]class MyClass { private: static float s_myStaticFloat; void MyFunction(); }; [/code]That DECLARES your static variable.