✓ Link copied to clipboard
C 📅 July 13, 2026 ⏱ 5 min read

C Program to Write a Sentence to a File

C Program to Write a Sentence to a File

Working with files is a must-have skill in C. This time, we’ll write a sentence to a file. Imagine needing to save user input directly to disk for later use. Let’s check out how to do that with just a few lines of code.

Prerequisites for the C Program to Write a Sentence to a File

Before diving into the code, ensure you know the following:

  • File handling functions in C like fopen, fprintf, and fclose.
  • Basic knowledge of stdio.h and stdlib.h headers.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Write a Sentence to a File

Here's the complete code to write a sentence to a file. It opens a file, writes input data, and then closes the file.

write_sentence_to_file.cC▶ Run
#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *file = fopen("output.txt", "w");
    // Open file in write mode
    if (file == NULL) {
        printf("Error opening file!\n");
        exit(1);
    }

    char sentence[100];
    printf("Enter the sentence to write to the file: ");
    fgets(sentence, 100, stdin);

    fprintf(file, "%s", sentence);
    printf("Sentence written to file successfully.\n");

    fclose(file);
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Write a Sentence to a File Program

When you run the program, it prompts for a sentence and writes it to a file. Here's what it looks like:

Output
Enter the sentence to write to the file: Hello, EduSeekho!
Sentence written to file successfully.
Output of C Program to Write a Sentence to a File
Output of C Program to Write a Sentence to a File

Algorithm to Write a Sentence to a File in C

This program writes a sentence to a file using basic file handling operations in C. Let's break down the steps involved.

  1. Open the file output.txt in write mode.
  2. Check if the file is opened successfully. If not, print an error message and exit.
  3. Prompt the user to enter a sentence.
  4. Write the entered sentence to the file.
  5. Close the file.
flowchart TD
  A([Start]) --> B[Open file in write mode]
  B --> C[Check if file is NULL]
  C -->|No| D[Prompt user for input]
  D --> E[Write sentence to file]
  E --> F[Close the file]
  F --> G([End])
  C -->|Yes| H[Print error message]
  H --> G
Flowchart: the algorithm step by step

How the C Program to Write a Sentence to a File Works

Let's examine each part of the code to understand how it works.

  • #include <stdio.h> and #include <stdlib.h>: These headers are necessary for file operations and standard functions.
  • FILE *file = fopen("output.txt", "w");: Opens the file in write mode. If the file doesn't exist, it creates one.
  • if (file == NULL): Checks if the file was opened successfully. If not, it prints an error message.
  • fgets(sentence, 100, stdin);: Reads a sentence from the user input.
  • fprintf(file, "%s", sentence);: Writes the sentence to the file.
  • fclose(file);: Closes the file to save resources.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What function is used to open a file in C?

🎉Correct! The correct function to open a file in C is fopen().
🤔Not quite. Think of the standard C library functions starting with 'f'.

Time and Space Complexity of the Write a Sentence to a File Program

Let's look at how this program performs in terms of time and space complexity.

Time Complexity: O(n). Here, n is the number of characters in the input sentence. The operation involves reading character by character until a newline is found or the limit is reached. For example, writing a 50-character sentence takes 50 operations.

Space Complexity: O(1). The program uses fixed memory for variables, like the sentence array to store input, and the space does not grow with input size.

⚠️

Common mistake: Forgetting to close the file with fclose(file); can lead to memory leaks and file corruption.

Key takeaway: This program is a simple yet powerful demonstration of file operations in C. Always make sure to close the file properly to dodge issues.

FAQs About C Program to Write a Sentence to a File

Here are some common questions students ask about this program.

The program checks for this condition. If the file can’t be opened, it shows an error message and exits.
Yeah, this program will overwrite an existing file with the same name.
You gotta include stdio.h for file stuff and stdlib.h for standard functions.
The limit is set by the size of the sentence array. You can make this size bigger if you want.
You can change the filename by editing the name in the fopen function call.

If you found this program helpful, check out these related programs:

Conclusion

Writing to files is super important for many apps. This program lays the groundwork for more advanced file operations. Try changing the filename or sentence limit to see how it works.

Practice your skills with more C programs in our C language quiz or check out other examples in the C online compiler.