The puts
function in C is used to write a line or string to the output stream (stdout
) that is up to, but does not include, the null
character. The puts
function also appends a newline character to the output and returns an integer.
To use the puts
function, you need to include the <stdio.h>
library in the program. This is shown below:
#include <stdio.h>
The prototype of the puts
function is shown below:
int puts(const char* str);
The puts
function takes a single mandatory parameter, i.e., a null-terminated character array.
The puts
function writes the provided argument to the output stream and appends a newline character at the end.
If the execution is successful, the function returns a non-negative integer; otherwise, it returns an EOF
(End-of-File) for any error.
The code below shows how the puts
function works in C:
#include <stdio.h>#include <string.h>int main() {// initializing stringschar str1[] = "Hello World";char str2[] = "Using puts in C";// writing to stdoutputs(str1);puts(str2);return 0;}
First, two character arrays (str1
and str2
) are initialized. These arrays are then provided as arguments to the puts
function called on Lines 11 and 12.
The puts
function proceeds to write the contents of the strings to stdout
and appends a newline character to each string. As a result, each string is printed on a separate line.
Free Resources