Q:

How to get total size of free memory in C in linux?

In java it's simply:
Runtime.getRuntime().freeMemory()

How to do it in C?

A:

You can get the virtual memory limit for a process under linux using getrlimit() with the RLIMIT_AS parameter.

A:

Your comments seem to indicate what you really want to know is the most memory that malloc can allocate in a single block.  That would approximately the maximum allowed VM size minus the current VM size.  The following code returns that:
#include <sys/resource.h>
#include <sys/time.h>
#include <stdint.h>

uint64_t get_total_free_mem()
{
    uint64_t vm_size = 0;
    FILE *statm = fopen("/proc/self/statm", "r");
    if (!statm)
        return 0;
    if (fscanf("%ld", &vm_size) != 1)
    {
        flcose(statm);
        return 0;
    }
    vm_size = (vm_size + 1) * 1024;

    rlimit lim;
    if (getrlimit(RLIMIT_AS, &lim) != 0)
        return 0;
    if (lim.rlim_cur <= vm_size)
        return 0;
    if (lim.rlim_cur >= 0xC000000000000000ull) // most systems cannot address more than 48 bits
        lim.rlim_cur  = 0xBFFFFFFFFFFFFFFFull;
    return lim.rlim_cur - vm_size;
}

The only exception is that in some cases getrlimit could return 0xFFFFFFFFFFFFFFFF however most 64-bit systems cannot address more than 48 bits of address to be used, no matter what.  I've accounted for that, but there may be some other edge cases I have missed.  For example, 32-bit applications cannot typically allocate more than 3GB of memory, though that depends on how the kernel was built.
The real answer here is why you would want to do this.  Usually the maximum amount that malloc can allocate is vastly larger than the amount the system can actually handle.  When you call malloc the system will happily allocate any amount you ask for (up to the AS limit which is usually ulimited) even if no pnhysical or swap space is available.  Until your program tries write to the memory (including writing 0s) with the memory is not actually allocated from physical memory or swap.  When you do write to it, that's when the system will work on finding out where to get the memory from, and thats when you might run into problems.
Your best bet is to use one of the other answers that tells you how much physical memory is available, and never allocate more than that.  Usually less, as you will want to leave some physical memory available for other processes and the kernel itself.

