C Programming

Puts function in C

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.

$ sudo apt install gcc

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.

$ nano puts.c

“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.

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.

$ gcc puts.c
$ ./a.out

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.

$ nano puts2.c

“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”

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.

$ gcc puts2.c
$ ./a.out

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

Kalsoom Akhtar

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