C++ puts()

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.

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";
    
    puts(str1);
    /*  Printed on new line since '/n' is added */
    puts(str2);
    
    return 0;
}

When you run the program, the output will be:

Happy New Year
Happy Birthday

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges