By accident I found that the line char s[] = {"Hello World"};
is properly compiled and seems to be treated the same as char s[] = "Hello World";
. Isn't the first ({"Hello World"}
) an array containing one element that is an array of char, so the declaration for s should read char *s[]
? In fact if I change it to char *s[] = {"Hello World"};
the compiler accepts it as well, as expected.
Searching for an answer, the only place I found which mentioned this is this one but there is no citing of the standard.
So my question is, why the line char s[] = {"Hello World"};
is compiled although the left side is of type array of char
and the right side is of type array of array of char
?
Following is a working program:
#include<stdio.h> int main() { char s[] = {"Hello World"}; printf("%s", s); // Same output if line above is char s[] = "Hello World"; return 0; }
Thanks for any clarifications.
P.S. My compiler is gcc-4.3.4.
Use { { }} Double Curly Braces to Initialize 2D char Array in C The curly braced list can also be utilized to initialize two-dimensional char arrays. In this case, we declare a 5x5 char array and include five braced strings inside the outer curly braces. Note that each string literal in this example initializes the five-element rows of the matrix.
Curly braced list notation is one of the available methods to initialize the char array with constant values. It’s possible to specify only the portion of the elements in the curly braces as the remainder of chars is implicitly initialized with a null byte value. It can be useful if the char array needs to be printed as a character string.
It’s possible to specify only the portion of the elements in the curly braces as the remainder of chars is implicitly initialized with a null byte value. It can be useful if the char array needs to be printed as a character string.
Since there’s a null byte character guaranteed to be stored at the end of valid characters, then the printf function can be efficiently utilized with the %s format string specifier to output the array’s content. Another useful method to initialize a char array is to assign a string value in the declaration statement.
It's allowed because the standard says so: C99 section 6.7.8, §14:
An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
What this means is that both
char s[] = { "Hello World" };
and
char s[] = "Hello World";
are nothing more than syntactic sugar for
char s[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', 0 };
On a related note (same section, §11), C also allows braces around scalar initializers like
int foo = { 42 };
which, incidentally, fits nicely with the syntax for compound literals
(int){ 42 }
The braces are optional, and the expression is equivalent to just an array of char.
You can also write this:
int a = {100}; //ok
Demo : http://ideone.com/z0psd
In fact, C++11
generalizes this very syntax, to initialize non-arrays as well as arrays, uniformly. So in C++11
, you can have these:
int a{}; //a is initialized to zero, and it is NOT an array int b[]{1,2,3,4}; //b is an array of size 4 containing elements 1,2,3,4 int c[10]{}; //all 10 elements are initialized to zero int *d{}; //pointer initialized to nullptr std::vector<int> v{1,2,3,4,5}; //vector is initialized uniformly as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With