C fputs() Function Tutorial

In this section, we will learn what the fputs() function is and how to use it in C.

fputs() Function in C

The `fputs` function is used to send a character-string to the output stream.

fputs() Function Syntax

Here’s the prototype of this function:

int fputs(const char *str, FILE *stream)

fputs() Function Parameters

  1. The first argument of this function is the address of the character string that we want to send its content to the output stream.
  2. This function also takes a second argument, indicating the file to which to write. We can use `stdout` (for standard output), which is defined in stdio.h as the second argument of this function.

This function does not write the null-character to the output.

fputs() Function Return Value

Also, the return value of this function is a non-negative value on a successful operation and EOF or -1 in case of an error.

Note: Unlike the puts function, the `fputs` does not automatically append a newline to the output.

Example: using fputs() function in C

#include <stdio.h>
int main() {

    char array[30];

    fputs("Please enter something:\n ",stdout);
    fgets(array, 30, stdin);

    fputs("This is what you've entered: \n",stdout);
    fputs(array,stdout);

    return 0;
}

Output:

Please enter something:

John Doe is here 🙂

This is what you've entered:

John Doe is here 🙂
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies