What is the difference between 'a' and “a”?

前端 未结 6 836
离开以前
离开以前 2020-12-05 20:40

I am learning C++ and have got a question that I cannot find the answer to.

What is the difference between a char constant (using single quotes) and a s

6条回答
  •  庸人自扰
    2020-12-05 20:48

    "a" is an array of characters that just happens to only contain one character, or two if you count the \0 at the end. 'a' is one character. They're not the same thing. For example:

    #include 
    
    void test(char c) {
        printf("Got character %c\n", c);
    }
    
    void test(const char* c) {
        printf("Got string %s\n", c);
    }
    
    int main() {
        test('c');
        test("c");
    }
    

    This will use two different overloads; see http://codepad.org/okl0UcCN for a demo.

提交回复
热议问题