A string literal is an unnamed string constant in the source code. E.g. "abc" is a string literal.
If you do something like char str[] = "abc";, then you could say that str is initialized with a literal. str itself is not a literal, since it's not unnamed.
A string (or C-string, rather) is a contiguous sequence of bytes, terminated with a null byte.
A char array is not necessarily a C-string, since it might lack a terminating null byte.
Answer from HolyBlackCat on Stack OverflowWhat is a literal string & char array in C? - Stack Overflow
New to C. If C does not have strings, then what exactly is printf doing?
Can someone please tell me how to declare a string in C and read them with scanf()? Tutorials point/geekforgeeks/other internet resources are total garbage and hard to follow for beginners.
Does c have strings
Videos
My friends are spilt down the middle on this. Half of us think since a c doesn’t have built in strings and only arrays of characters that they don’t. While the other half think that the array of characters would be considered string.
A string literal is an unnamed string constant in the source code. E.g. "abc" is a string literal.
If you do something like char str[] = "abc";, then you could say that str is initialized with a literal. str itself is not a literal, since it's not unnamed.
A string (or C-string, rather) is a contiguous sequence of bytes, terminated with a null byte.
A char array is not necessarily a C-string, since it might lack a terminating null byte.
What is a literal string & char array in C?
C has 2 kinds of literals: string literals and compound literals. Both are unnamed and both can have their address taken. string literals can have more than 1 null character in them.
In the C library, a string is characters up to and including the first null character. So a string always has one and only one null character, else it is not a string. A string may be char, signed char, unsigned char.
Copy// v---v string literal 6 char long
char *s1 = "hello";
char *s2 = "hello\0world";
// ^----------^ string literal 12 char long
char **s3 = &"hello"; // valid
// v------------v compound literal
int *p1 = (int []){2, 4};
int **p2 = &(int []){2, 4}; // vlaid
C specifies the following as constants, not literals, like 123, 'x' and 456.7. These constants can not have their address taken.
Copyint *p3 = &7; // not valid
C++ and C differ in many of these regards.
A chararray is an array of char. An array may consist of many null characters.
Copychar a1[3]; // `a1` is a char array size 3
char a2[3] = "123"; // `a2` is a char array size 3 with 0 null characters
char a3[4] = "456"; // `a3` is a char array size 4
char a4[] = "789"; // `a4` is a char array size 4
char a5[4] = { 0 }; // `a5` is a char array size 4, all null characters
The following t* are not char arrays, but pointers to char.
Copychar *t1;
char *t2 = "123";
int *t3 = (char){'x'};