Comparing two large binary files in C I'm trying to read two sufficiently large binary files, comparing them and printing the offset at which they differ. I'm using fread to read the binary files and memcmp to compare them.

#include&lt;stdio.h&gt;
#include&lt;time.h&gt;

clock_t start, end;
int main(int argc, char *argv[])
{
    double cpu_time_taken;
    FILE *fp1, *fp2;
    printf("\nArgument count:  %d", argc);
    printf("\nFile 1 is: %s", argv[1]);
    printf("\nFile 2 is: %s\n", argv[2]);
    if (argc &lt; 3)
    {
        printf("\nInsufficient Arguments: \n");
        printf("\nHelp:./executable &lt;filename1&gt; &lt;filename2&gt;\n");
        return 0;
    }
    else
    {
        fp1 = fopen(argv[1],  "rb");
        if (fp1 == NULL)
        {
            printf("\nError in opening file %s", argv[1]);
            return 0;
        }

        fp2 = fopen(argv[2], "rb");

        if (fp2 == NULL)
        {
            printf("\nError in opening file %s", argv[2]);
            return 0;
        }

        if ((fp1 != NULL) &amp;&amp; (fp2 != NULL))
        {
            start = clock();
            compare_two_binary_files(fp1, fp2);
            end = clock();
            cpu_time_taken = ((double) (end - start)) / CLOCKS_PER_SEC;
            printf("\nTime taken to compare: %f", cpu_time_taken*1000);
        }
    }
}


int compare_two_binary_files(FILE *fp1, FILE *fp2)
{
    char tmp1[16], tmp2[16];

    size_t bytes = 0, readsz = sizeof tmp1;
    int count = 0;
    while (!feof(fp1) || !feof(fp2)){
        fread (tmp1, sizeof *tmp1, readsz, fp1);
        fread (tmp2, sizeof *tmp2, readsz, fp2);
        count += 16;
        if(memcmp(tmp1, tmp2, readsz)){
            for(int i=0; i &lt; readsz; i++){
                printf ("%d: 0x%02x ",i, tmp1[i]);
            }
            printf("\n%x", count);
            return 0;
        }
    }
}
fread (tmp1, sizeof *tmp1, readsz, fp1);
fread (tmp2, sizeof *tmp2, readsz, fp2);
count += 16;
if(memcmp(tmp1, tmp2, readsz)){
    …
}



You are discarding the return values of the fread() calls, blindly assuming that they both successfully read 16 bytes.

It is unclear what the return value of compare_two_binary_files(…, …) means.  In fact, you sometimes don't return a value at all.  Your compiler should have warned you about that problem.

File I/O should be done a block at a time: 512 bytes, 1024 bytes, or 2048 bytes would be a more reasonable chunk size than 16.

Technically, most of the time is spent waiting for I/O.  cpu_time_taken is a misnomer: what you're measuring is called "wall clock time".