Displaying the output on the screen is an important component of every programming language. The puts function in C is used to perform this. The puts () call in C is a file handling method that is utilized to print a line to the output screen. The puts () function looks a lot like the printf() function. The puts () function is being used to display the string that was read previously using the gets () or scanf() functions on the terminal. The integer value returned by the puts () function represents the number of characters written on the terminal.

If the string contains formatting characters such as ‘% s,’ printf() will provide unpredictable results in the puts() function. Also, if “str” is a user-supplied string, using printf() could compromise security.

Return value of Puts () function in C

The puts function adds a newline character to the end of the specified argument and publishes it to the output stream.

The method returns a non-negative integer if the processing is effective; else, it outputs an EOF (End-of-File) if there is a problem.

Syntax of Puts () function in C

The syntax of the puts () function is displayed in the below line.

$ int puts(const char *string)

The string is a pointer toward an array of “chars” containing the C string.

Implementation of Puts () function in C

After a basic understanding of the puts () function in C, we will now implement a few examples. For example, implementation, and understanding, we are using Ubuntu 20.04 operating system. We have a GCC compiler for code execution and compilation in it. You can have it by running the following listed command in the console.

Example 1

To implement an illustration of the Puts () function in C, open the command prompt by pressing “Ctrl Alt T” or by finding the operating system’s applications. Once you find it, create a file with the nano command so that your file will appear in the GNU editor. Execute the following affixed instruction in the shell.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Puts-function-C-01.png" data-lazy- height="108" src="data:image/svg xml,” width=”974″>

“nano” represents the command, puts the file’s name, and .c is the file extension showing that you will have c type code. The title of the file may differ reliant on your need. After execution, the GNU nano editor will appear as shown in the succeeding attached image. We are implementing the simplest example. Add the subsequent code to your created file.

In this example, we have declared a string as “Hi I am Kalsoom”, we are going to print it on the screen with the help of the puts () function. The comments along with each line of code have been added for user understanding.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Puts-function-C-02.png" data-lazy- height="800" src="data:image/svg xml,” width=”974″>

Once you write the whole code, save the file and exit it by pressing “Ctrl X”. You will be back to your terminal screen. Now execute and compile the code by following the listed instructions.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Puts-function-C-03.png" data-lazy- height="85" src="data:image/svg xml,” width=”974″>

The output string has been displayed, as you can check it on the above-attached output screen.

Example 2

Now moving towards the next example, create a file with the nano command so that your file will appear in GNU editor. Execute the following affixed instruction in the shell.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Puts-function-C-04.png" data-lazy- height="97" src="data:image/svg xml,” width=”974″>

“nano” represents the command, “puts2” is the file’s name, and .c is the file extension showing that you will have c type code. The title of the file may differ reliant on your need. After execution, the GNU nano editor will appear as shown in the succeeding attached image. Add the subsequent code to your created file.

In this illustration, we have declared two strings with a string size of 100. The first string will print “I love to write for linuxhint”; however, the second string will print “Linuxhint is best”. Both strings will be printed on two separate lines without the addition of “/n”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Puts-function-C-05.png" data-lazy- height="647" src="data:image/svg xml,” width=”974″>

Once you write the whole code, save the file and exit it by pressing “Ctrl X”. You will be back to your terminal screen. Now execute and compile the code by following the listed instructions.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Puts-function-C-06.png" data-lazy- height="95" src="data:image/svg xml,” width=”974″>

The output string has been demonstrated, as you can check it on the above-attached output screen.

Conclusion

This article was about the puts () function used in the C programming language. We have explained the puts () function’s basic concept and its return value and syntax. Two examples have been implemented for user understanding. Now by looking into this article with concentration will help you to understand this concept easily, and after that, you will be able to implement the code as per your work requirements.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content