Structure is a data type. You don't give values to a data type. You give values to instances/objects of data types.
So no this is not possible in C.

Instead you can write a function which does the initialization for structure instance.

Alternatively, You could do:

Copystruct MyStruct_s 
{
    int id;
} MyStruct_default = {3};

typedef struct MyStruct_s MyStruct;

And then always initialize your new instances as:

CopyMyStruct mInstance = MyStruct_default;
Answer from Alok Save on Stack Overflow
🌐
Reddit
reddit.com › r/c_programming › struct default values, are there any and which is the best way to set?
r/C_Programming on Reddit: Struct default values, are there any and which is the best way to set?
May 20, 2024 -

I wasn't able to find a straight answer if struct have or haven't any default values, by default. Like, 0 for int and NULL for pointers.

So for this code:

// void pointers as generic Items
typedef void *Item;
typedef const void *constItem;

typedef struct tnode *Tree;
typedef const struct tnode *constTree;
typedef struct tnode {
    Item value;
    Tree sub[2];
} Treenode;
typedef enum {left, right} Child;

And this in the main function:

int main()
{
    Tree wtree; // It's a pointer to a struct not a struct
    wtree->value = NULL;
    wtree->sub[left] = wtree->sub[right] = NULL;

    return 0;
}

Is it necessary to initiate this way for this example where NULL is the initial value? Is this the best way to initiate a structure with a pointer to it?

Discussions

How to initialize C structs with default values - Stack Overflow
I have this defined struct: #include #include typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; typedef struct List { ... More on stackoverflow.com
🌐 stackoverflow.com
[ENH] Specify default values for C struct declarations
Is your feature request related to a problem? Please describe. If there is a sturct with many fields, only some of its fields need to be initialized explicitly, and other fields can be implicitly e... More on github.com
🌐 github.com
7
December 18, 2022
How to set an initial value of a variable of struct in C? - Raspberry Pi Stack Exchange
The problem is, 0 is NOT the default value. The value keeps changing. For example i have a uint16_t property of a struct, and without initializing the value and i try printing it, it would result to random numbers like 46837 on one specific runtime, and 46840 on another. More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
April 1, 2021
initialization - Default values in a C Struct - Stack Overflow
I have a data structure like this: struct foo { int id; int route; int backup_route; int current_route; } and a function called update() that is used to request changes in it. upda... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Fuchsia
fuchsia.dev › fuchsia-src › contribute › governance › rfcs › 0022_default_values_for_struct
RFC-0022: Clarification: Default values for struct members | Fuchsia
There is no support for Go, C, or Rust. For example (from //zircon/system/host/fidl/examples/types.test.fidl): struct default_values { bool b1 = true; bool b2 = false; int8 i8 = -23; int16 i16 = 34; int32 i32 = -34595; int64 i64 = 3948038; uint8 u8 = 0; uint16 u16 = 348; uint32 u32 = 9038; uint64 u64 = 19835; float32 f32 = 1.30; float64 f64 = 0.0000054; string s = "hello"; };
Top answer
1 of 4
2

In C, whether an object is initialized or not depends on how you declare the object, for example whether you declare it as an object of static storage duration (which is initialized to zero unless you explicitly initialize it to something else) or an object of automatic storage duration (which is not initialized, unless you explicitly initialize it).

Therefore, it would not make sense to assign default values to the type definition, because even if the language allowed this, it would not guarantee that the object of that type will be initialized.

However, you can create your own function which initializes your struct to specific values:

void init_list( List *p )
{
    p->size = 0;
    p->head = NULL;
    p->tail = NULL;
}

Assuming that the object is declared inside a function (not at file scope), you can use the following code to declare and initialize the object to default values:

List list1;
init_list( &list1 );

If the object is declared at file scope, you can't call the function init_list at file scope, but you can call the function inside the function main, for example.

Alternatively, when you declare the object, you can also initialize the individual members:

List list1 = { 0, NULL, NULL };

This will also work at file scope.

Since everything is being initialized to zero, it is sufficient to write the following:

List list1 = { 0 };

In that case, all members that are not explicitly assigned a value will be initialized to zero.

2 of 4
2

In C opposite to C++ you may not initialize data members in structure declarations like you are doing

typedef struct List {
    int size = 0;
    Node* head = NULL;
    Node* tai = NULL;
} List;

Also it does not make sense to declare the global pointer list1.

List* list1;

What you need is to write

typedef struct List {
    int size;
    Node* head;
    Node* tail; // I think you mean `tail` instead of `tai`
} List;

int main( void )
{
    List list1 = { .size = 0, .head = NULL, .tail = NULL };
    //...;
🌐
GitHub
github.com › cython › cython › issues › 5177
[ENH] Specify default values for C struct declarations · Issue #5177 · cython/cython
December 18, 2022 - cdef struct Inner: int a = 0 int b = 0 cdef struct Outer: int a = 0 int b = 0 Inner c = Inner() # equivalent to above: @cython.default_to_zero cdef struct Inner: int a int b @cython.default_to_zero cdef struct Outer: int a int b Inner c def main(): cdef Outer s = Outer()
Author   cython
🌐
Learn C++
learncpp.com › cpp-tutorial › default-member-initialization
13.9 — Default member initialization – Learn C++
January 18, 2022 - To avoid the possibility of uninitialized members, simply ensure that each member has a default value (either an explicit default value, or an empty pair of braces). That way, our members will be initialized with some value regardless of whether we provide an initializer list or not. Consider the following struct, which has all members defaulted:
Find elsewhere
🌐
Quora
quora.com › Why-does-C-standard-never-added-default-struct-init-default-parameter-value-and-other-really-simple-add
Why does C standard never added default struct init, default parameter value and other really simple add? - Quora
Answer (1 of 4): The features you’re asking for in C are already present in C++. In C++, you can define a parameterless constructor to perform default initialization of objects (instances of either structs or classes). C++ also has the default parameter value feature you mentioned.
Top answer
1 of 8
15

If you want to set a struct object in one go and you have a C99 compiler, try this:

struct stuff {
    int stuff_a;
    int stuff_b;
    // and so on...
};

struct stuff foo;
/* ... code ... */
foo = (struct stuff){.stuff_b = 42, .stuff_a = -1000};

Otherwise, with a C89 compiler, you have to set each member one by one:

foo.stuff_b = 42;
foo.stuff_a = -1000;

Running example @ ideone : http://ideone.com/1QqCB


The original line

struct a{   a() : i(0), j(0) {}   INT i;   INT j;}

is a syntax error in C.

2 of 8
10

As you have probably learned from the other answers, in C you can't declare a structure and initialize it's members at the same time. These are different tasks and must be done separately.

There are a few options for initializing member variables of a struct. I'll show a couple of ways below. Right now, let's assume the following struct is defined in the beginning of the file:

struct stuff {
  int stuff_a;
  int stuff_b;
};

Then on your main() code, imagine that you want to declare a new variable of this type:

struct stuff custom_var;

This is the moment where you must initialize the structure. Seriously, I mean you really really must! Even if you don't want to assign specific values to them, you must at least initialize them to zero. This is mandatory because the OS doesn't guarantee that it will give you a clean memory space to run your application on. Therefore, always initialize your variables to some value (usually 0), including the other default types, such as char, int, float, double, etc...

One way to initialize our struct to zero is through memset():

memset(&custom_var, 0, sizeof(struct stuff));

Another is accessing each member individually:

custom_var.stuff_a = 0;
custom_var.stuff_b = 0;

A third option, which might confuse beginners is when they see the initialization of struct members being done at the moment of the declaration:

struct stuff custom_var = { 1, 2 };

The code above is equivalent to:

struct stuff custom_var;
custom_var.stuff_a = 1;
custom_var.stuff_b = 2;
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 120252-default-values-struct.html
default values in struct
October 4, 2009 - For dynamic arrays, calloc will do the same, malloc will not. Otherwise, you need to use a loop with assignment. Check on your compiler and see what your local variables are set to, initially. There is no "infoArray", in your code, just one instance of a struct named stuff.
🌐
Google Groups
groups.google.com › g › comp.lang.c › c › qdw9cI9tKts
C struct member default values in struct
October 22, 2015 - It's new in C++11, see: http://en.cppreference.com/w/cpp/language/list_initialization http://en.cppreference.com/w/cpp/language/aggregate_initialization ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... Because member default values are introduced, CAlive will also introduce non-constructor instantiation of class objects when used as follows: char invalid_data[] = "Invalid"; class CAbc { public: CAbc(char* p) : m_p(p) { } ~CAbc() { } private: char* p = invalid_data; } void function(void) { // No constructor will be called, m_p initialized to "Invalid" CAbc* abc = lnew CAbc; // Constructor will be called, m_p initialized to "Rick" CAbc* abc = lnew CAbc("Rick");
🌐
GitHub
github.com › vlang › v › issues › 21501
using a C struct as a struct field does not adopted default values · Issue #21501 · vlang/v
May 14, 2024 - #include "@VMODROOT/header.h" struct Foo { a int = 3 } struct C.Bar { a int = 3 } struct FooBar { foo Foo bar C.Bar } dump(Foo{}) // has default value dump(C.Bar{}) // has default value dump(FooBar{}) // Foo has default value while C.Bar does not
Author   vlang
🌐
GeeksforGeeks
geeksforgeeks.org › c language › structures-c
C Structures - GeeksforGeeks
By default, structure members are not automatically initialized to 0 or NULL. Uninitialized structure members will contain garbage values.
Published   April 8, 2026
🌐
Sandor Dargo’s Blog
sandordargo.com › blog › 2023 › 11 › 22 › struct-initialization
Struct initialization | Sandor Dargo's Blog
November 21, 2023 - We’ll see what happens if we instantiate the struct and try to print the values of the members. Our small program will print some garbage value for s.m_num and nothing for s.m_text. That’s all right. When it comes to class members in case they are not explicitly initialized, their default constructor is called, but fundamental types are left uninitialized.
🌐
Go4Expert
go4expert.com › forums › default-values-c-struct-fields-t8264
default values for C struct fields | Go4Expert
January 15, 2008 - of course can't do this typedef struct { char name[50] = "not_set_yet"; int ssn = 111223333; } employee; is: employee emp1 =...
🌐
GameDev.net
gamedev.net › home › forums › programming › general and gameplay programming › how to give a struct default value?
how to give a struct default value? - General and Gameplay Programming - Forums - GameDev.net
December 18, 2006 - Well, thats usually done in c, where there isn't any way to guarentee that a struct will have specific values. If you use the typedef method in c++ it doesn't affect the ability to put constructors in. ... Exactly the same. Except typedef'ing a struct like that implies that you're using C, and you can't give a struct default values in C* * Actually, you can give it a default of 0 by making it global or static, but that's just nasty [smile]