31

Sorry for the simple question but I'm on vacation reading a book on core audio, and don't have my C or Objective C books with me...

What are the curly braces doing in this variable definition?

MyRecorder recorder = {0};
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
John
  • 2,121
  • 1
  • 23
  • 38
  • 17
    I like to call the `{0}`, the ***universal zero initializer***. It "works", recursively if needed, for every type! ints, doubles, structs, arrays, pointers, pointers to structs, structs with arrays of pointers, ..., ... – pmg Jan 05 '12 at 15:26
  • 2
    @pmg — why didn't you post your comment as an answer? – vikingosegundo Jan 05 '12 at 15:31
  • 4
    My comment really isn't an answer: it doesn't explain anything, it merely points out another use of the construct --- and the name I give that construct. Also explaining how `{0}` is different to `{42}` and how they apply to arrays or nested structs or other "strange" types would require a very long answer. – pmg Jan 05 '12 at 15:38

6 Answers6

26

Assuming that MyRecorder is a struct, this sets every member to their respective representation of zero (0 for integers, NULL for pointers etc.).

Actually this also works on all other datatypes like int, double, pointers, arrays, nested structures, ..., everything you can imagine (thanks to pmg for pointing this out!)

UPDATE: A quote extracted from the website linked above, citing the final draft of C99:

[6.7.8.21] If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, [...] the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

Niklas B.
  • 92,950
  • 18
  • 194
  • 224
  • struct members do not have default values: it may be better to say zero explicitly --- "sets every member to (the right kind of) zero". – pmg Jan 05 '12 at 15:31
9

Its initializing all members of recorder structure to 0 according to C99 standard. It might seem that it initializes every bit of the structure with 0 bits. But thats not true for every compiler.

See this example code,

#include<stdio.h>

struct s {
    int i;
    unsigned long l;
    double d;
};

int main(){
    struct s es = {0};
    printf("%d\n", es.i);
    printf("%lu\n", es.l);
    printf("%f\n", es.d);
    return 0;
}

This is the output.

$ ./a.out 
0
0
0.000000
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • 1
    "Its initializing all members of recorder structure to 0." is in application of the C99 standard. "In fact it initialize every bit of the structure with 0 bits." is only true in practice on conventional architectures, and the C99 standard points out several times that it is not saying that it is true, and that a standard-conforming compiler/target architecture do not have to make it true. – Pascal Cuoq Jan 05 '12 at 17:28
5

It is an initialization of a structure.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    could also be of a scalar: `MyRecorder recorder = {0};` where `MyRecorder` is an alias of `int` for example is valid in C – ouah Jan 05 '12 at 15:34
  • 1
    @ouah Judging from the name of the `struct`, I say it is *highly unlikely* that it is an `int` or in fact anything other than a structure/union. – Sergey Kalinichenko Jan 05 '12 at 15:35
5

Actually, it don't initliaze all the elements of the structure, just the first one. But, the others are automatically initialized with 0 because this is what the C standard ask to do.

If you put: MyRecorder recorder = {3};

The first element will be 3 and the others weill be 0.

Bruno Soares
  • 756
  • 5
  • 6
0

Unlike C++11, in C99 there has to be at least one element in initializer braces.

C++11 struct:

MyRecorder recorder{};

C struct:

MyRecorder recorder = {0};
lumpidu
  • 719
  • 6
  • 13
0

MyRecorder could be one of the following and you are attempting to initialize all the elements of that with zero

typedef struct _MyRecorder1 {
    int i;
    int j;
    int k;
}MyRecorder1;

typedef int MyRecorder2[3];
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98