c++ - 结果 cout << "Hello"+ 1<<endl; (C++)

标签 c++ cout

<分区>

你能帮我解释一下下面代码的结果吗

cout<< "Hello" + 1<< endl;

为什么结果显示为“ello”,我知道要打印出 Hello1 然后我应该使用: cout<< "你好"<< 1<< endl; 但是谁能帮我解释一下上面代码的顺序: 太感谢了。

最佳答案

你的例子大致等同于:

// `p` points to the first character 'H' in an array of 6 characters
// {'H', 'e', 'l', 'l', 'o', '\0'} forming the string literal.
const char* p = "Hello";
// `q` holds the result of advancing `p` by one element.
// That is, it points to character 'e' in the same array.
const char* q = p + 1;
// The sequence of characters, starting with that pointed by `q`
// and ending with NUL character, is sent to the standard output.
// That would be "ello"
std::cout << q << std::endl;

关于c++ - 结果 cout << "Hello"+ 1<<endl; (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44298443/

相关文章:

c++ - 带有 BOOST 的简单代理程序

android - wcstombs() 在 Android 上有无效的输出

c++ - 最小和完美转发

C++函数cout重定向到文件

c++ - 使用结构标识符来表示 POD 类型和 C 结构

c++ - 线程安全游戏引擎 : Multi-Threading Best Practices?

c++11 - ios_base::sync_with_stdio(false) 会影响 <fstream> 吗?

C++ cout操作数错误,类型错误ostream,字符串

c++ - std::cout << cstring;打印 cstring 元素的值,而不是 cstring 十六进制地址。为什么?

C++ cin 在没有我告诉它这样做的情况下打印变量