Join our newsletter for the latest updates.

C++ puts()

The puts() function in C++ writes a string to stdout.

puts() prototype

int puts(const char *str);

The puts() function takes a null terminated string str as its argument and writes it to stdout. The terminating null character '\0' is not written but it adds a newline character '\n' after writing the string.

A call to puts() is same as calling fputc() repeatedly.

The main difference between fputs() and puts() is the puts() function appends a newline character to the output, while fputs() function does not.

It is defined in <cstdio> header file.

puts() Parameters

str: The string to be written.

puts() Return value

On success, the puts() function returns a non-negative integer. On failure it returns EOF and sets the error indicator on stdout.

Example: How puts() function works

#include <cstdio>

int main()
    char str1[] = "Happy New Year";
    char str2[] = "Happy Birthday";
    /*  Printed on new line since '/n' is added */
    return 0;

When you run the program, the output will be:

Happy New Year
Happy Birthday