Search code examples
cclangclang-format

clang-format: designated initializer: One member per line


I have a struct in C:

struct A {
    int a;
    int b;
    int c;
}

When initializing the struct clang-format reformats the code as follows:

struct A name = {.a = 1, .b = 2, .c = 3};

How can I tell clang-format to put any member in its own line? Like so:

struct A name = {
    .a = 1,
    .b = 2,
    .c = 3
};

Solution

  • Adding a trailing comma should fix this problem.

    struct A name = {
        .a = 1,
        .b = 2,
        .c = 3, // <-- this trailing comma is needed
    };