#Difference between and ' in C++?

10 messages · Page 1 of 1 (latest)

stuck crag
#

So i was learning about strings and i made the following:
char str[6] = { "H", "i", "\0" };
which displayed the following error:
C++ a value of type cannot be used to initialize an entity of type
but then i later found out i needed to use ' instead of ", why? what is the difference?

night steppeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

hot swift
#

Char uses ‘ ‘ single quotes

cold trench
#

The things you put inside your array are c strings, of type const char[]

#

But the array is supposed to hold characters

stuck crag
#

thanks! another thing:

int main() {
    // \0 is a NULL charecter to mark the end of the string
    char str[6] = { 'H', 'e', 'l', 'l', 'o', '\0' };

    std::cout << str;
    std::cout << str;
}

the \0 marks the end of the string but if i remove it, seemingly nothing changes? i printed the string twice to see if it would maybe change what lines they print on but it stays the same

cold trench
#

Normally you would get weird characters on your screen without the null character

#

Since cout interprets your char array as a string and keeps printing until it finds a '\0' char

stuck crag
#

good to know!

#

!solved