Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/c/puts"

From cppreference.com
< cpp‎ | io‎ | c
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/sidebar}}
+
{{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 a newline to {{tt|stdout}}
+
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===
{{param list begin}}
+
{{par begin}}
{{param list item | str | character string to be written}}
+
{{par|str|character string to be written}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
 +
On success, returns a non-negative value
  
non-negative number on success or {{c|EOF}} otherwise
+
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===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/io/c/dcl list fputs}}
+
{{dsc inc|cpp/io/c/dsc fputs}}
{{dcl list template | cpp/io/c/dcl list fprintf}}
+
{{dsc inc|cpp/io/c/dsc fprintf}}
{{dcl list end}}
+
{{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

 
 
 
C-style I/O
Types and objects
Functions
File access
Direct input/output
Unformatted input/output
Formatted input
(C++11)(C++11)(C++11)    
(C++11)(C++11)(C++11)    
 
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) [edit]
prints formatted output to stdout, a file stream or a buffer
(function) [edit]