There is no such thing as a "string" in C. In C, strings are one-dimensional array of char, terminated by a null character \0. Since you can't assign arrays in C, you can't assign strings either. The literal "hello" is syntactic sugar for const char x[] = {'h','e','l','l','o','\0'};
When initializing an array, C allows you to fill it with values. So
char s[100] = "abcd";
is basically the same as
int s[3] = { 1, 2, 3 };
but it doesn't allow you to do the assignment since s is an array and not a free pointer. The meaning of
s = "abcd"
is to assign the pointer value of abcd to s but you can't change s since then nothing will be pointing to the array.
This can and does work if s is a char* - a pointer that can point to anything.
In the example you provided, s is actually initialized at line 1, not line 2. Even though you didn't assign it a value explicitly at this point, the compiler did.
At line 2, you're performing an assignment operation, and you cannot assign one array of characters to another array of characters like this. You'll have to use strcpy() or some kind of loop to assign each element of the array.
Initialization and assignment are two distinct operations that happen to use the same operator ("=") here.
Think of it like this:
Imagine that there are 2 functions, called InitializeObject, and AssignObject. When the compiler sees thing = value, it looks at the context and calls one InitializeObject if you're making a new thing. If you're not, it instead calls AssignObject.
Normally this is fine as InitializeObject and AssignObject usually behave the same way. Except when dealing with char arrays (and a few other edge cases) in which case they behave differently. Why do this? Well that's a whole other post involving the stack vs the heap and so on and so forth.
PS: As an aside, thinking of it in this way will also help you understand copy constructors and other such things if you ever venture into C++
I know that this has already been answered, but I wanted to share an answer that I gave to someone who asked a very similar question on a C/C++ Facebook group.
Arrays don't have assignment operator functions*. This means that you cannot simply assign a char array to a string literal. Why? Because the array itself doesn't have any assignment operator. (*It's a const pointer which can't be changed.)
To copy a string literal (such as "Hello world" or "abcd") to your char array, you must manually copy all char elements of the string literal onto the array.
char s[100]; This will initialize an empty array of length 100.
Now to copy your string literal onto this array, use strcpy
strcpy(s, "abcd"); This will copy the contents from the string literal "abcd" and copy it to the s[100] array.
Here's a great example of what it's doing:
int i = 0; //start at 0
do {
s[i] = ("Hello World")[i]; //assign s[i] to the string literal index i
} while(s[i++]); //continue the loop until the last char is null
You should obviously use strcpy instead of this custom string literal copier, but it's a good example that explains how strcpy fundamentally works.
But I guess it just doesn't work like that. Anyways, here was my workaround:
char string[10] = {0};
sprintf(string,"hello");
its almost as good, because it is short.
Strangely enough, another thing which has worked for me (though a little off-topic perhaps) is when you declare arrays of structs, you can initialize them with the good-ole double quotes like this:
Incidentally, this method of initialization is known as a "compound literal"
Id love to see if anyone could explain why this works to use double quotes and not the string = "hello"; way...
This method is great if you have a lot of strings by the way, because it allows you to write code like: