r/ProgrammerTIL • u/khrushchev007 • Apr 18 '18
C [C] TIL double quotes and single quotes are different
"Use double quotes around strings" and 's'ingle quotes around characters
... While working with strings I had to add a NULL ('\0') character at the end, and adding "\0" at the end messed it up
31
Upvotes
32
u/King_Joffreys_Tits Apr 18 '18
In C style languages, a char and a string are different. They (or most) won’t even let you create a char with more than one character in single quotes. A string is an array of chars, with a null terminator at the end of the string.
There’s some “fun” assignments I’ve done in school to create your own string classes using only char pointers. It’s something else.
7
u/SeequelistaTaco Apr 18 '18
I know you're post is related to C, but to go on a tangent, I would like it if languages like Python and Javascript which don't have the character type still enforced just one type of quote for strings.
It's not a fundamental issue by any means but I don't enjoy the look of code that has a mixture of single and double quotes. It could be enforced with an in-house coding style of course but I've not seen that done for quotes at the places I've worked.
It does give the benefit of not having to escape the internal quote, be it double or single, though. In Javascript I could do...
But in C# I'd have to escape the quotes.
2
u/[deleted] Apr 18 '18
C doesn't really have strings, just pointers to char arrays. Thinking about them any other way invites heartache.