puts() — Write a string

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3
Language Environment

both  

Format

#include <stdio.h>

int puts(const char *string);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>

int puts_unlocked(const char *string);

General description

Writes the string pointed to by string to the stream pointed to by stdout, and appends the newline character to the output. The terminating NULL character is not written.

If stdout points to the text stream, and the output string is longer than the length of the stream's record, the output is wrapped. That is, the record is filled with the output characters, the last character of the record is set to a newline character, and the remaining output characters are written to the next record. Such wrapping is repeated until the remaining output characters fit into the record. Please note that the newline character is appended to the last portion of the output string. If the output string is shorter than the record, the remaining characters of the record are filled with blanks—if stdout is opened in a text mode—or with NULL characters if the stdout is opened in binary mode.

The puts() function is not supported for files opened with type=record or type=blocked.

puts() has the same restriction as any write operation for a read immediately following a write or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must also be an intervening flush or reposition unless an EOF has been reached.

puts_unlocked() is functionally equivalent to puts() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

Returned value

If successful, puts() returns the number of bytes written. However, newline characters used to wrap the data are not counted.

If unsuccessful, puts() returns EOF.

If a system write-error occurs, the write stops at the point of failure.

After truncation, puts() does not count the truncated characters, but returns the actual number of bytes written.

Example

CELEBP55
⁄* cCELEBP55                    
                 
   This example writes "Hello World" to stdout.                                 
                                                                                
 *⁄                                                                             
#include <stdio.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
  if ( puts("Hello World") == EOF )                                             
    printf( "Error in puts\n" );                                                
}                                                                               
Output:
Hello World

Related information