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.
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.
You are assigning an array to the buffer.
In the particular case of string, usually, the character whose ASCII value is 0 terminates the string.
For example, if you wanted to put a string that reads 'Hello world' inside the string you could have done
char buf[100] = {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0};
or
char buf[100] = "Hello world";
Anyway, your code prints nothing because you are trying to print a string with length zero, that is an empty string.
Videos
What is the significance of '0' in C++ programming?
In what scenarios is '0' commonly used in C++ arrays?
Why is nullptr preferred over '0' for null pointers in modern C++ code?
The null character '\0' (also null terminator), abbreviated NUL, is a control character with the value zero. Its the same in C and objective C
The character has much more significance in C and it serves as a reserved character used to signify the end of a string,often called a null-terminated string
The length of a C string (an array containing the characters and terminated with a '\0' character) is found by searching for the (first) NUL byte.
In C, \0 denotes a character with value zero. The following are identical:
char a = 0;
char b = '\0';
The utility of this escape sequence is greater inside string literals, which are arrays of characters:
char arr[] = "abc\0def\0ghi\0";
(Note that this array has two zero characters at the end, since string literals include a hidden, implicit terminal zero.)
0 being an int like other integers, sizeof(0) will yield 4 bytes.
sizeof(NULL) will yield 8 bytes. In binary system, it is 8x8=64 bits, all bits with 0.
Pointers have 8 bytes allocated against characters with 1 bytes and integers 4 bytes. Is 8 bytes the maximum bytes for any datatype? I believe so as NULL is set to 8 bytes apparently for that reason to take care NULL denotes 0 for all datatypes.
Edit: I think I understand it better now. Thaks to all of those whom where able to explain it in a way I could understand.
I tried searching about it on the internet and I read a few answers in stack overflow and some here on reddit and I still don`t understand what it does. Not even the book I'm using now teaches it properly. People, and the book, say that it returns a value to indicate that the program has ran sucessfuly. But I don't see any 0 being printed. Where does it go?
If it is "hidden", how can I visualize it? What is the purpose of that if, when there is an error, the compiler already warns us about it?
Boolean/logical operators in C are required to yield either 0 or 1.
From section 6.5.3.3/5 of the ISO C99 standard:
The result of the logical negation operator
!is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0.
In fact, !!x is a common idiom for forcing a value to be either 0 or 1 (I personally prefer x != 0, though).
Also see Q9.2 from the comp.lang.c FAQ.
§6.5.3.3/5: "The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int."
The other logical operators (e.g., &&, ||) always produce either 0 or 1 as well.