I have 16 GB RAM on my computer at school running Ubuntu 13.10. I wrote a simple C program to get the total RAM but it shows non-precise results, instead of 16 it prints out 15. How to make it to be precise, even for smaller RAM sizes? At home I have a computer with 768 MB RAM and my program shows there wrong results too :(
#include <sys/sysctl.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
void getMemoryInfo()
{
FILE *meminfo = fopen("/proc/meminfo", "r");
int totalMemory = 0;
if(meminfo == NULL)
{
exit(-1);
}
char buff[256];
while(fgets(buff, sizeof(buff), meminfo))
{
int ramKB;
if(sscanf(buff, "MemTotal: %d kB", &ramKB) == 1)
{
totalMemory = ramKB/1024.0;
totalMemory = totalMemory/1024.0;
}
}
if(fclose(meminfo) != 0)
{
exit(-1);
}
printf("%d\n", totalMemory);
}
int main()
{
getMemoryInfo();
return 0;
}