使用单引号或双引号的c风格字符串?

时间:2015-09-15 19:41:10

标签: c++ quotes

我正在本网站上学习c风格的字符串教程: http://www.learncpp.com/cpp-tutorial/66-c-style-strings/ 并且有一个代码片段:

func rotated()
{
    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
    {            
        println("landscape")
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
    {
        println("Portrait")
    }

}

结果应该是: 字符串有7个字符。 115 116 114 105 110 103 0

但是,当我使用Xcode运行它时,它显示: *字符串有71920151050115 116 114 105 110 103 0 *

并且还收到了'字符'的警告。\ n'。警告说“字符太长,其类型,多字符字符常数”。

因此我将其更改为“characters。\ n”(替换为“with”),结果与预期一致。

我的问题是这个问题是由于代码本身还是由于我的编译器造成的?是书的错还是我的错?

1 个答案:

答案 0 :(得分:1)

您的代码中存在拼写错误:在"字符周围使用双引号。\ n":

std::cout << mystring << " has " << sizeof(mystring) << "characters.\n";

Live Example

您使用sinqle引号看到的输出是一个多字符文字,其类型为int,并且是一个实现定义的值。请参阅this Q&A