How to Use puts() Function in C Programming Language

puts() Function in C Programming Language

Introduction :

Outputting text to the console screen is a basic operation in any programming language. There are various functions available for this purpose. Generally, the printf() function is used to display the output on the console screen in C programming language. In C programming language, you can also display text on the console screen using puts() function.

In case of puts() function, printing text or string as output on the console is very easy. In this article, I shall show you how to use puts() function using the C programming language. Here, I also show you the syntax and usage of puts() function.

What is puts() function :

In C programming language, the puts() function is used to print strings to the standard output (console screen). This function is part of the standard input output library (stdio.h). It automatically adds a newline character at the end of the string. The puts() function is useful when you need a newline character (\n) at the end of the output.

It is a simple and commonly used function for printing string literals or value of string variables with a newline character. It does not require format specifiers like printf() function. It is working with only string data (character arrays). To display other types of data (integers, floating point numbers), you have to use printf() function.

Syntax of the puts() function :

The following code is the syntax of the puts() function.

The puts() function takes a single argument or parameter, which is a pointer to a null-terminated string (string_array). It returns a positive integer on success and EOF (-1) on error.

About the program :

This C program is an example of basic usage of the puts() function to print strings or text to the console screen. In this program, I call puts() function two times with argument of string literals and string variables. When you run the program on your PC, you see two strings in the console with new lines.

Explanation of the program :

You must include the standard input output header file (stdio.h) at the beginning of your program.
In the main function, you have to declare a character array (string_array) is initialized with a string (“puskarcoding.com”). Then, call the puts() function twice. First one with “string_array” as argument and second one with a string literals (“This is my website.”).

How run the program :

To run the program on your PC, you have to install VS Code. Then, open it and create a new file (pac.c). Then, copy the below code and paste in “pac.c” file. After saving the file, run the program on your PC.

Source code of the program :

You can copy the following code of the program and run on your PC.

Output of the program :

If you run the program, you can see the output of the program on your PC.

output of puts() function in the C programming language

Conclusion :

After reading the above article, you have learned how to use puts() function in the C programming language. Now you can use puts() function to make more complex programs. Thank you for visiting my site.

Scroll to Top