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.

2nd Jun 2019, 12:47 PM
A_Singh7
6 Antworten
+ 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.
2nd Jun 2019, 1:04 PM
Farry
Farry - avatar
+ 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
2nd Jun 2019, 12:56 PM
Farry
Farry - avatar
+ 2
char
2nd Jun 2019, 1:06 PM
Farry
Farry - avatar
+ 1
Ok Thanks. That solved my question.
2nd Jun 2019, 1:06 PM
A_Singh7
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.
2nd Jun 2019, 1:01 PM
A_Singh7
0
Ok. So this means that if you have to work with a single character, which is better using char or string ?
2nd Jun 2019, 1:05 PM
A_Singh7