Q:

How to put binary array into binary file

I have an array: 
 int a[5] = { 1, 0, 0, 1, 1 };

When using a loop to put every element into a text file, the size of the file will be 5 bytes. So I want to put the five elements into a binary file so that the size is 5 bits. How can I do that?

A:

If you want to write 5 bytes to the file, you should use the type unsigned char and open the output file as binary:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    unsigned char a[5] = { 1, 0, 0, 1, 1 };
    FILE *fp = fopen("filename", "wb");
    if (fp == NULL) {
        fprintf(stderr, "cannot open filename: %s\n", strerror(errno));
        exit(1);
    }

    if (fwrite(a, 1, 5, fp) != 5) {
        fprintf(stderr, "cannot write to filename: %s\n", strerror(errno));
        fclose(fp);
        exit(1);
    }

    fclose(fp);
    return 0;
}

Your question is a litte confusing: 

So I want to put the five elements into a binary file so that the size is 5 bits

On most systems, you cannot write individual bits to a file, and file sizes are not expressed in bits but in bytes, which are usually 8 bit wide.  You can set 5 bits of a byte to the values specified, but you must decide how you number bits in a byte...
If bits are numbered from the least significant to the most significant, here is how you can do this:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

/* assuming array `a` contains bit values */
int a[5] = { 1, 0, 0, 1, 1 };

int main(void) {
    FILE *fp = fopen("filename", "wb");
    unsigned char b;
    if (fp == NULL) {
        fprintf(stderr, "cannot open filename: %s\n", strerror(errno));
        exit(1);
    }

    for (int i = 0; i < 5; i++) {
        b |= a[i] << i;
    }
    fputc(b, fp);
    fclose(fp);

    return 0;
}

