简体   繁体   English

如果在字符数组中遇到空字符(位于数组中间),c++ 的 cout 函数是否会停止打印

[英]Will cout function of c++ stop printing if it comes across a null character(positioned in middle of array) in character array

如果在字符数组中遇到空字符(位于数组中间),c++ 的 cout 函数会停止打印吗?

Firstly, std::cout isn't a function.首先, std::cout不是函数。 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.如果我们传递一个std::string ,那么<<将输出字符串中的每个字符。

  • If we pass a C-style string ( char* ), then << will output every character up to, but not including, the first NUL character encountered.如果我们传递一个 C 风格的字符串 ( char* ),那么<<将输出每个字符,直到遇到的第一个 NUL 字符,但不包括。 Note that for malformed input (containing no NUL), this can lead to undefined behaviour, due to reading beyond the bounds of the array.请注意,对于格式错误的输入(不包含 NUL),这可能会导致未定义的行为,因为读取超出了数组的边界。

The other (unformatted) output operation is write() , which accepts a character array and a length.另一个(未格式化的)输出操作是write() ,它接受一个字符数组和一个长度。 That outputs exactly the number of characters specified, including any NULs encountered.这将准确输出指定的字符数,包括遇到的任何 NUL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM