What is the "{0}" in C?

What does it mean char buf[MAXDATASIZE] = { 0 }; {0}?

tried to print it, but didn't print anything.

#include <stdio.h>

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

This is just an initialization list for an array. So this is very similar to the usual syntax:

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

However, the C standard states that if you do not provide a sufficient number of elements in the list of initializers, it will by default initialize the rest. This way, in your code, all elements bufwill be initialized to 0.

printfIt doesn’t display anything, because it bufis a string of zero length.

+12
source

You assign an array to the buffer.

, ASCII 0, .

, , "Hello world" ,

char buf[100] = {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0};

char buf[100] = "Hello world";

, , , .

+2

Source: https://habr.com/ru/post/1661852/


All Articles