This article discusses the C++ puts() function that outputs a string to standard output. The standard output stream is called stdout. It serves as the application’s default output location. It often defaults to being directed to the text console in the majority of systems (generally, on the screen). Any function that accepts an argument of type FILE* anticipating an output stream, such as fputs or fprintf, can accept stdout as an argument. Read this article to learn more about the puts() function’s features and operation.
The string that was previously read using some functions like gets() or scanf() is printed on the console using the puts() method. This is provided in the stdio.h header file. The strings’ input and output operations utilize the puts() function.
Syntax of puts() Function
Below you can find the syntax of the puts() function:
The str input passed to the puts() function is a null-terminated string written to stdout. The string is written without the null character “0” at the end, but a newline character “n” is added afterward.
Let’s move on to several examples that will clarify the fundamental ideas behind the puts() function. Please note that we are executing these programs using Dev-C++. A free, feature-rich integrated development environment (IDE) for C and C++ programming is called Dev-C++. Any other IDE that you are comfortable working on can also be used.
Let’s start with the first example of this article to understand the working of the puts() function. Here, we will print the text “Welcome !!!” on the screen three times. In the code, you can see that we have included the cstdio header file by using the keyword “include”. To start the execution, we have used the main() method, as seen in the second line of the code.
After that, a string is initialized with the name “new_str”. It contains the text “Welcome!!!” printed on the screen three times in the following line of code. It is done with the “puts(new_str)” code. Here, you can see that the puts() function is used to achieve that. You can see “return 0” on the code’s final line. Returning 0 from the main() method shows that the program ran successfully, and returning 1 indicates that there was an error and the program did not run correctly.
char new_str = "Welcome !!! ";
Now, let’s save and run this code to see the output. In Dev C++, you can press the option “Execute” in the tool menu to execute the code.
This is the output you get after executing the code. Here, you can see the text “Welcome !!!” is printed three times.
As mentioned, the puts() method returns a non-negative integer value upon successful execution. Otherwise, any error returns EOF. The put() function’s return value is seen in the following example. Here is another illustration that clarifies this idea for your benefit. For the complete steps, see the following:
Here, we have added the “stdio.h” header file first. It is done by using the “include” keyword. After that, the main() method starts the execution. The string is initialized with the text “This is the puts() function of C++”, as you can notice in the code. The integer value is initialized where the puts() function is used in which we have passed the string previously mentioned. After that, the returned value is printed by using the printf() method. In the last line, return 0 is written to show the successful execution of the program.
char str_one = “This is the puts() function of C++”;
int value = puts(str_one);
printf(“Here is the Returned Value = %d”, value);
Click on the “Execute” option or press the “F11” key if you are using Dev C++ to run the code. You will see the following output. Here, the first line is the text showing the usage of the puts() function, and the second line shows its returned value. See the following output:
This is the final example of this article that will discuss the concept one more time. Here, we will also explain the fflush() method. In C++, any buffered data is flushed to the appropriate device using the fflush() function. Buffered data is temporary or application-specific data kept in the computer’s physical memory up to a certain date and time. The cstdio header file contains a definition for the fflush() function. See the code provided below.
In the code, we have included the stdio.h and iostream header files. These files are important for the execution of this code. After that, “using namespace std” is written. The phrase “using namespace” indicates its presence in the scope. It makes everything in the std namespace accessible without requiring the std:: prefix before any of it. After that, the main() method is launched. Inside the main() method, we have called the puts() function in which the text passed that we want to display on the screen. The next line of the code shows the use of the fflush() method. Lastly, return 0 is written.
This code’s purpose is to display the message and provide context for using the fflush() method. The output buffer is flushed using the fflush(stdout) method since it is not always necessary to display data correctly in real-time (when data is printed often).
using namespace std;
puts(“This is the third example program. \n”);
Here is the following output:
This article concentrated on the C++ puts() function. Every character of the null-terminated string referenced by str and one additional newline character (“n”) is written to the output stream stdout by the puts() function. This idea is covered in depth with a number of examples.