#include <stdio.h>
int main(){
char s[] = {"hello"};
printf("%s", s);
return 0;
}
This code can be compiled and executed successfully,but how can one dimension array(char[] s) be assigned as the two dimension array({"hello"})?
#include <stdio.h>
int main(){
char s[] = {"hello"};
printf("%s", s);
return 0;
}
This code can be compiled and executed successfully,but how can one dimension array(char[] s) be assigned as the two dimension array({"hello"})?
C 2018 6.7.9 14 says:
An array of character type may be initialized by a character string literal or UTF–8 string literal, optionally enclosed in braces…
{"hello"}
is a string literal enclosed in braces.
From the C Standard (6.7.9 Initialization)
14 An array of character type may be initialized by a character string literal or UTF–8 string literal, optionally enclosed in braces. Successive bytes of the 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.
Thus these two declarations of a character array
char s[] = {"hello"};
and
char s[] = "hello";
are equivalent.
Moreover you may also enclose in braces an initializing expression for a scalar object
11 The initializer for a scalar shall be a single expression, optionally enclosed in braces. The initial value of the object is that of the expression (after conversion); the same type constraints and conversions as for simple assignment apply, taking the type of the scalar to be the unqualified version of its declared type.
So you may write for example either
char *s = "hello";
or
char *s = { "hello" };
or similarly
int x = 10;
or
int x = { 10 };
Note: in these declarations
char *s = "hello";
and
char *s = { "hello" };
the string literal having array type char[6]
is implicitly converted to a pointer to its first element of the type char *
. Pointers are scalar types.
And one more remark. According to the C grammar you may append a comma to an initalizing list.
initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }
^^^
That is you may write for example
char s[] = { "Hello " "Word", };
int x = { 10, };
Here is a demonstration program:
#include <stdio.h>
int main( void )
{
char s[] = { "Hello " "Word", };
int x = { 10, };
printf( "s = %s\n", s );
printf( "x = %d\n", x );
}
The program output is
s = Hello Word
x = 10
Relative to this declaration
char s[] = { "Hello " "Word", };
pay attention to that according to the C Standard (5.1.1.2 Translation phases)
- Adjacent string literal tokens are concatenated.
That is the above declaration is equivalent to
char s[] = { "Hello Word", };
Bear in mind that there is an essential difference between C and C++ in the initialization of a character array with a string literal. In C you may write for example
char s[5] = "hello";
In this case the array s
will not contain a string because it has no room for storing the terminating zero character '\0'
of the string literal.
In C++ such a declaration is invalid and will not compile.
It only works when there is only one string in the list. For example, this code fails to compile:
The compiler ignores the list when
a
is one-dimensional (your case) and there is only one string in the list. It's pretty much like this:In fact, it doesn't have to be a string. These are also valid:
If you want to make it two-dimensional (one dimension for strings and one dimension for the list itself):
Or if you know your string size is fixed: