I am learning C, and I finally solved the following exercise, after couple of hours of struggle:

"Write a program that merges lines alternately from two files and writes the results
to stdout. If one file has less lines than the other, the remaining lines from the
larger file should simply be copied to stdout."

However, I am not satisfied with the code. I feel that I over complicated it, and that there's
a simpler solution.

How do I improve this code?

```
#include <stdio.h>
#include <stdbool.h>

int main (void)
{
    char file1[11], file2[11];
    FILE *input1, *input2;
    int c, d, i = 0;
    bool end_of_file1 = false, end_of_file2 = false;
    bool file1_newline = false, file2_newline = false;

    printf ("Enter the name of the two files to be merged,\
 separated by space: ");
    scanf ("%10s %10s", file1, file2);

    input1 = fopen (file1, "r");
    input2 = fopen (file2, "r");

    while ( end_of_file1 == false ) {
    if ( file1_newline == false )
        c = getc (input1);

    if ( end_of_file2 == true && end_of_file1 == false
                                         && i == 0 ) {
        putc ('\n', stdout);
        i = 1;
    }

    if ( c == '\n' && end_of_file2 == true )
        i = 0;

    if ( (c == '\n' && file1_newline == false) ||
               (c == EOF && file1_newline == false) ) {
        file1_newline = true;
        putc (' ', stdout);
    }

    if ( file1_newline == false )
        putc (c, stdout);

    if ( file1_newline == true )
        d = getc (input2);
    if ( d == EOF ) {
        end_of_file2 = true;
        if ( c == EOF )
        end_of_file1 = true;
    }
    if ( file1_newline == true && end_of_file2 == false )
        putc (d, stdout);
    if ( (d == '\n' && c != EOF) || end_of_file2 == true )
        file1_newline = false;
    }

    fclose (input1);
    fclose (input2);

    return 0;
}

```