How to get the total RAM memory size on windows PC in C Win32 API

2 Answers

0 votes
#include <stdio.h>
#include <windows.h> 

int main(void)
{
    long long TotalMemoryInKilobytes;

    GetPhysicallyInstalledSystemMemory(&TotalMemoryInKilobytes);

    printf("%llu KB %.2f GB\n", TotalMemoryInKilobytes, TotalMemoryInKilobytes / (1024.0 * 1024.0));

    return 0;
}




/*
run:

33554432 KB 32.00 GB

*/

 



answered Nov 4, 2021 by avibootz
edited Nov 5, 2021 by avibootz
0 votes
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#define BYTES_TO_KB 1024

int main(void) {

    MEMORYSTATUSEX statex;

    statex.dwLength = sizeof(statex);

    GlobalMemoryStatusEx(&statex);

    _tprintf(TEXT("%I64d KB\n"), statex.ullTotalPhys / BYTES_TO_KB);
    _tprintf(TEXT("%.2f GB\n"), (statex.ullTotalPhys / BYTES_TO_KB) / (1024.0 * 1024.0));

    printf("%lu KB\n", statex.ullTotalPhys / BYTES_TO_KB);
    printf("%.2f GB\n", (statex.ullTotalPhys / BYTES_TO_KB) / (1024.0 * 1024.0));

    return 0;
}



/*
run:

33485960 KB
31.93 GB
33485960 KB
31.93 GB

*/

 



answered Nov 12, 2021 by avibootz
edited Nov 12, 2021 by avibootz
...