0
Differences between a string literal and a character literal
Hey guys, I want to ask that what are the differences between a string literal and character literal. For example, I declare char literal = ‘a’; and, string literal = ‘’a’’; What would be the differences between them and between their usages, if any ? Thanks everybody.
6 odpowiedzi
+ 1
of course there is a difference in them and their usage. char can only hold a single character and has 1 byte allocated for it in the RAM. string can hold multiple characters (like words or phrased) and its mem size varies depending on the length of the string.
+ 2
string is an array of chars.
string l="a";
char a[]= ['a','\0'];
there is no string object in c so you have to declares stings as the above syntax. this also works in cpp btw
+ 2
char
+ 1
Ok Thanks.
That solved my question.
0
Farry Thank you so much for your answer.
Even, I know that.
I was asking if there is any difference in their usage.
How is a string literal treated in a progam and how is a character literal treated ?
Using an array of characters as a string is certainly a poor practice in C++, because you can use the std::string which is a lot safer and doesn’t use a null terminator.
0
Ok.
So this means that if you have to work with a single character, which is better using char or string ?