How to get total amount of RAM in C

GlobalMemoryStatusEx – retrieves information about the system’s current usage of both physical and virtual memory.

MEMORYSTATUSEX – contains information about the current state of both physical and virtual memory, including extended memory.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
 
int GetTotalRamInGB(){
  MEMORYSTATUSEX mstatusex;
  mstatusex.dwLength = sizeof (mstatusex);
  GlobalMemoryStatusEx (&mstatusex);
  return floor(mstatusex.ullTotalPhys/1024.0/1024.0/1024.0 + 0.5);
}
 
int main(void) {
	printf("Total amount of RAM: %dGB.\n", GetTotalRamInGB());
	return EXIT_SUCCESS;
}
Categories: C Tags: , , ,
%d bloggers like this: