Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to get the current OS name and version in C

3 Answers

0 votes
#include <stdio.h>
#include <sys/utsname.h>

int main() {
    struct utsname buffer;

    if (uname(&buffer) == 0) {
        printf("Operating System: %s\n", buffer.sysname);
        printf("Node Name: %s\n", buffer.nodename);
        printf("Release: %s\n", buffer.release);
        printf("Version: %s\n", buffer.version);
        printf("Machine: %s\n", buffer.machine);
    } else {
        perror("uname");
    }

    return 0;
}



/*
run:

Operating System: Linux
Node Name: prod-repl-c-758868f6f5-nn8bj
Release: 6.6.72+
Version: #1 SMP PREEMPT_DYNAMIC Sat Feb  8 10:02:01 UTC 2025
Machine: x86_64


*/

 



answered Mar 27, 2025 by avibootz
0 votes
#include <stdio.h>
#include <sys/utsname.h>

int main() {
    struct utsname buffer;

    if (uname(&buffer) == 0) {
        printf("Operating System: %s\n", buffer.sysname);
        printf("Release: %s\n", buffer.release);
        printf("Version: %s\n", buffer.version);
    } else {
        perror("uname");
    }

    return 0;
}



/*
run:

Operating System: Linux
Release: 6.6.72+
Version: #1 SMP PREEMPT_DYNAMIC Sat Feb  8 10:02:01 UTC 2025


*/

 



answered Mar 27, 2025 by avibootz
0 votes
#include <stdio.h>
#include <windows.h>

typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(OSVERSIONINFOW*);

int main() {
    OSVERSIONINFOW osVersion = { 0 };
    osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);

    HMODULE ntdllHandle = GetModuleHandleW(L"ntdll.dll");
    if (ntdllHandle) {
        RtlGetVersionPtr rtlGetVersion = (RtlGetVersionPtr)GetProcAddress(ntdllHandle, "RtlGetVersion");
        if (rtlGetVersion) {
            NTSTATUS status = rtlGetVersion(&osVersion);
            if (status == 0) {
                printf("Operating System: Windows %lu.%lu\n", osVersion.dwMajorVersion, osVersion.dwMinorVersion);
                printf("Build Number: %lu\n", osVersion.dwBuildNumber);
            }
            else {
                printf("RtlGetVersion failed with status: %ld\n", status);
            }
        }
        else {
            printf("Failed to get RtlGetVersion address.\n");
        }
    }
    else {
        printf("Failed to get ntdll.dll handle.\n");
    }

    return 0;
}



/*
run:

Operating System: Windows 10.0
Build Number: 19045

*/

 



answered Mar 27, 2025 by avibootz

Related questions

1 answer 74 views
3 answers 79 views
1 answer 80 views
1 answer 86 views
1 answer 89 views
1 answer 92 views
1 answer 99 views
...