fputs() Function in C

Last Updated : 21 Jul, 2026

fputs() function is a standard library function declared in the <stdio.h> header file. It is used to write a string to a file without automatically appending a newline character.

  • It takes a string and a file pointer as arguments and writes the string to the specified file.
  • It is commonly used to write text data to files line by line.
  • On success, it returns a non-negative value; on failure, it returns EOF.
C
#include <stdio.h>

int main()
{
    FILE *fp = fopen("sample.txt", "w");

    if (fp == NULL)
    {
        printf("Unable to open file.\n");
        return 1;
    }

    fputs("Hello, GeeksforGeeks!\n", fp);
    fputs("Learning C File Handling.", fp);

    fclose(fp);

    return 0;
}

Output

Hello, GeeksforGeeks! 
Learning C File Handling.

Syntax

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

Parameters

  • str: Pointer to the null-terminated string to be written.
  • stream: Pointer to the file where the string will be written.

Return Value

  • Returns a non-negative value on successful write.
  • Returns EOF if an error occurs.

Working

The fputs() function writes the specified string to the given file until it encounters the null character ('\0').

  • Open the file using the fopen() function in write ("w") or append ("a") mode.
  • Pass the string and the file pointer as arguments to the fputs() function.
  • The function writes the entire string to the file starting from the current file position.
  • Finally, close the file using the fclose() function to ensure the data is saved properly.

Advantages

The fputs() function provides a simple and efficient way to write strings to a file.

  • Writes an entire string to a file with a single function call.
  • More efficient than writing characters one by one using fputc().
  • Easy to use for creating and updating text files.

Limitations

Although fputs() is useful for writing strings, it has a few limitations.

  • It can write only null-terminated strings to a file.
  • It does not support formatted output like fprintf().
  • It does not automatically append a newline character after writing.

Difference Between fputs() and puts()

Both fputs() and puts() are used to write strings, but they differ in where they write the output and how they handle newline characters.

fputs()puts()
Writes a string to a file or any output stream.Writes a string to the standard output (stdout).
Requires a FILE* pointer as an argument.Does not require a file pointer.
Does not automatically append a newline character.Automatically appends a newline character after the string.
Declared as fputs(const char *str, FILE *stream).Declared as puts(const char *str).
Commonly used for file handling.Commonly used for console output.
Comment