C Programming – What is ‘{0}’?

c++

What does char buf[MAXDATASIZE] = { 0 };'s {0} means?

tried to print it out but it print nothing.

#include <stdio.h>

int main(void)
{
        char buf[100] = { 0 };
        printf("%s",buf);
        return 0;
}

Best Answer

This is just an initializer list for an array. So it's very like the normal syntax:

char buf[5] = { 1, 2, 3, 4, 5 };

However, the C standard states that if you don't provide enough elements in your initializer list, it will default-initialize the rest of them. So in your code, all elements of buf will end up initialized to 0.

printf doesn't display anything because buf is effectively a zero-length string.