如果在字符数组中遇到空字符(位于数组中间),c++ 的 cout 函数会停止打印吗?
Firstly, std::cout
isn't a function. It's an object (specifically an output stream object).
Its <<
operator is overloaded. The overloads which are particularly relevant are those which accept strings.
If we pass a std::string
, then <<
will output every character in the string.
If we pass a C-style string ( char*
), then <<
will output every character up to, but not including, the first NUL character encountered. Note that for malformed input (containing no NUL), this can lead to undefined behaviour, due to reading beyond the bounds of the array.
The other (unformatted) output operation is write()
, which accepts a character array and a length. That outputs exactly the number of characters specified, including any NULs encountered.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.