puts() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The puts function is handy in the C programming language to print strings. This function writes strings or lines to stdout, i.e., the output stream. The passed string to the puts function in C is printed along with a newline, and an integer value is returned, and this return value is dependent on the success of writing the string passed.

Syntax of puts() Function in C

The syntax of the puts() function in C is simple. The function declaration is given below:

Where the string is the string you want to pass to stdout, this 'const' parameter helps guard against accidentally modifying the char array.

Parameters of puts() Function in C

The only parameter required to use the puts function in C is the string you want to print to the screen.

Parameters:

Return Value of puts() Function in C

Return type: int

The puts() function in C returns a non-negative integer value that denotes successful execution. If there is any error in the execution of the puts() function, it returns EOF error.

Example

Let's now look at a very simple example of the puts() function in C:

Output of the above code:

As we can see in this example, the puts() function has printed the input string to stdout.

The puts() function is preferred for printing strings to stdout because it is less expensive, i.e., implementing the puts function in C is simpler than that of printf(). If your input string contains formatting characters such as %, the printf() function will yield unexpected results.

More Examples

Let's look at a couple more examples of the puts() function in C.

Printing Multiple Strings Using puts() Function in C

Output:

The natural behavior of the puts() function is always to print the string in a new line. It essentially appends the new line character - \n at the end of every string while writing it to stdout.

So in this function, we initialized two strings - string1 and string2. When we used the puts() function, a newline character was appended at the end of string1, making string2 print in the next line. If there were another string, it would be printed in the line after string2.

Showcasing the Return Value of the puts() Function in C:

Output:

The value that is returned from the puts() function in C is the length of the string. If there is any error, EOF will be returned.

A Similar Function: fputs()

In the C programming language, there is a function similar to puts(), which is - fputs(). The difference between the two functions is that the fputs() function is used to write a stream or a file. Adding to that, the fputs() function does not add a new line character at the end of the stream or file like puts().

Conclusion

  • The puts() function in C is used to print ‘strings’ specifically.
  • The syntax of the puts() function in C is:
    • int puts(const char* string)
  • puts() function takes as parameters just the string required to be printed to stdout.
  • A non-negative integer value is returned by the function, which is usually the length of the string.
  • After the end of every string, the puts() function adds the new line character, thereby printing every string into a new line.