Difference between revisions of "cpp/io/c/puts"
m (Text replace - "{{cpp|" to "{{c|") |
m (fmt) |
||
(9 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/title| puts}} | + | {{cpp/title|puts}} |
− | {{cpp/io/c/ | + | {{cpp/io/c/navbar}} |
− | {{ddcl | header=cstdio | | + | {{ddcl|header=cstdio| |
− | int puts( char *str ); | + | int puts( const char *str ); |
}} | }} | ||
− | Writes character string {{tt|str}} and | + | Writes every character from the null-terminated string {{tt|str}} and one additional newline character {{c|'\n'}} to the output stream {{lc|stdout}}, as if by repeatedly executing {{lc|std::fputc}}. |
+ | |||
+ | The terminating null character from {{tt|str}} is not written. | ||
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|str|character string to be written}} |
− | {{ | + | {{par end}} |
===Return value=== | ===Return value=== | ||
+ | On success, returns a non-negative value | ||
− | non-negative number | + | On failure, returns {{lc|EOF}} and sets the ''error'' indicator (see {{lc|std::ferror}}) on {{tt|stdout}}. |
+ | |||
+ | ===Notes=== | ||
+ | The {{tt|std::puts}} function appends the newline character to the output, while {{lc|std::fputs}} function does not. | ||
+ | |||
+ | Different implementations return different non-negative numbers: some return the last character written, some return the number of characters written (or {{lc|INT_MAX}} if the string was longer than that), some simply return a non-negative constant. | ||
+ | |||
+ | A typical cause of failure for {{tt|std::puts}} is running out of space on the file system, when {{tt|stdout}} is redirected to a file. | ||
+ | |||
+ | ===Example=== | ||
+ | {{example|code= | ||
+ | #include <cstdio> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int rc = std::puts("Hello World"); | ||
+ | |||
+ | if (rc == EOF) | ||
+ | std::perror("puts()"); // POSIX requires that errno is set | ||
+ | } | ||
+ | |output= | ||
+ | Hello World | ||
+ | }} | ||
===See also=== | ===See also=== | ||
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc inc|cpp/io/c/dsc fputs}} |
− | {{ | + | {{dsc inc|cpp/io/c/dsc fprintf}} |
− | {{ | + | {{dsc see c|c/io/puts}} |
+ | {{dsc end}} | ||
+ | |||
+ | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 13:18, 29 November 2022
Defined in header <cstdio>
|
||
int puts( const char *str ); |
||
Writes every character from the null-terminated string str
and one additional newline character '\n' to the output stream stdout, as if by repeatedly executing std::fputc.
The terminating null character from str
is not written.
Contents |
[edit] Parameters
str | - | character string to be written |
[edit] Return value
On success, returns a non-negative value
On failure, returns EOF and sets the error indicator (see std::ferror) on stdout
.
[edit] Notes
The std::puts
function appends the newline character to the output, while std::fputs function does not.
Different implementations return different non-negative numbers: some return the last character written, some return the number of characters written (or INT_MAX if the string was longer than that), some simply return a non-negative constant.
A typical cause of failure for std::puts
is running out of space on the file system, when stdout
is redirected to a file.
[edit] Example
#include <cstdio> int main() { int rc = std::puts("Hello World"); if (rc == EOF) std::perror("puts()"); // POSIX requires that errno is set }
Output:
Hello World
[edit] See also
writes a character string to a file stream (function) | |
(C++11) |
prints formatted output to stdout, a file stream or a buffer (function) |
C documentation for puts
|