不同编程语言打印当前操作系统信息和当前语言标准
目录
C++
Linux系统(尝试使用POSIX uname函数)
#include <iostream>
#include <unistd.h>
#include <sys/utsname.h>
int main()
{
struct utsname unameData;
if (uname(&unameData) != 0) {
std::cerr << "Failed to get system information." << std::endl;
return 1;
}
std::cout << "Current C++ Standard: " << __cplusplus << std::endl;
std::cout << "Operating system: " << unameData.sysname << std::endl;
std::cout << "Node name: " << unameData.nodename << std::endl;
std::cout << "Release: " << unameData.release << std::endl;
std::cout << "Version: " << unameData.version << std::endl;
std::cout << "Machine: " << unameData.machine << std::endl;
return 0;
}
Current C++ Standard: 201402
Operating system: Linux
Node name: tio2
Release: 5.2.11-100.fc29.x86_64
Version: #1 SMP Thu Aug 29 12:52:22 UTC 2019
Machine: x86_64
Windows系统(尝试使用Windows API函数)
#include <iostream>
#include <windows.h>
int main()
{
OSVERSIONINFOEX osvi;
SYSTEM_INFO si;
BOOL bOsVersionInfoEx;
DWORDLONG dwlConditionMask = 0;
ZeroMemory(&si, sizeof(SYSTEM_INFO));
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi);
if (!bOsVersionInfoEx) {
std::cerr << "Failed to get OS version information." << std::endl;
return 1;
}
GetSystemInfo(&si);
std::cout << "Current C++ Standard: " << __cplusplus << std::endl;
std::cout << "Operating system: " << "Windows" << std::endl; // Windows API does not provide a direct equivalent of unameData.sysname
std::cout << "Major version: " << osvi.dwMajorVersion << std::endl;
std::cout << "Minor version: " << osvi.dwMinorVersion << std::endl;
std::cout << "Build number: " << osvi.dwBuildNumber << std::endl;
std::cout << "Platform: " << osvi.dwPlatformId << std::endl;
std::cout << "Service pack: " << (osvi.szCSDVersion[0] ? osvi.szCSDVersion : "None") << std::endl;
switch (si.wProcessorArchitecture) {
case PROCESSOR_ARCHITECTURE_AMD64:
std::cout << "Machine: " << "AMD64" << std::endl;
break;
case PROCESSOR_ARCHITECTURE_INTEL:
std::cout << "Machine: " << "x86" << std::endl;
break;
case PROCESSOR_ARCHITECTURE_ARM:
std::cout << "Machine: " << "ARM" << std::endl;
break;
case PROCESSOR_ARCHITECTURE_IA64:
std::cout << "Machine: " << "IA64" << std::endl;
break;
case PROCESSOR_ARCHITECTURE_ARM64:
std::cout << "Machine: " << "ARM64" << std::endl;
break;
default:
std::cout << "Machine: " << "Unknown architecture" << std::endl;
break;
}
return 0;
}
Current C++ Standard: 201703
Operating system: Windows
Major version: 10
Minor version: 0
Build number: 19045
Platform: 2
Service pack: None
Machine: AMD64
Scala
object Main {
def main(args: Array[String]): Unit = {
println(s"Scala version: ${util.Properties.versionString}")
println(s"OS name: ${System.getProperty("os.name")}")
println(s"OS version: ${System.getProperty("os.version")}")
println(s"OS architecture: ${System.getProperty("os.arch")}")
}
}
Scala version: version 2.12.10
OS name: Windows 10
OS version: 10.0
OS architecture: amd64
Python
import platform
import sys
# Python version
print(f"Python version: {sys.version}")
# OS Information
print(f"OS: {platform.system()} {platform.release()}")
Python version: 3.8.0b4 (default, Sep 2 2019, 08:08:37)
[GCC 8.3.1 20190223 (Red Hat 8.3.1-2)]
OS: Linux 5.2.11-100.fc29.x86_64
Java
public class Main {
public static void main(String[] args) {
// Java version
String javaVersion = System.getProperty("java.version");
System.out.println("Java version: " + javaVersion);
// Operating system information
String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
String osArch = System.getProperty("os.arch");
System.out.println("OS Name: " + osName);
System.out.println("OS Version: " + osVersion);
System.out.println("OS Architecture: " + osArch);
}
}
Java version: 1.8.0_222
OS Name: Linux
OS Version: 5.2.11-100.fc29.x86_64
OS Architecture: amd64
Erlang(escript)
#!/usr/bin/env escript
%% -*- erlang -*-
main(_) ->
%% Erlang version
ErlangVersion = erlang:system_info(otp_release),
io:format("Erlang version: ~s~n", [ErlangVersion]),
%% OS Information
{OSFamily, OSName} = os:type(),
io:format("OS: ~s ~s~n", [OSFamily, OSName]),
%% OS Version (Only works on UNIX systems, will return undefined on others)
OSVersion = os:version(),
io:format("OS Version: ~p~n", [OSVersion]).
Erlang version: 20
OS: unix linux
OS Version: {5,2,11}
Go
package main
import (
"fmt"
"runtime"
)
func main() {
// Go version
fmt.Println("Go version: " + runtime.Version())
// OS Information
fmt.Println("OS: " + runtime.GOOS)
fmt.Println("Architecture: " + runtime.GOARCH)
}
Go version: go1.11.13
OS: linux
Architecture: amd64
OCaml
let () =
(* OCaml version *)
let ocaml_version = Sys.ocaml_version in
Printf.printf "OCaml version: %s\n" ocaml_version;
(* OS Information *)
let os_type = Sys.os_type in
Printf.printf "OS Type: %s\n" os_type
OCaml version: 4.07.0
OS Type: Unix
Racket
#lang racket
(define racket-version (version))
(define os-info (system-type))
(printf "Racket version: ~a\n" racket-version)
(printf "OS information: ~a\n" os-info)
Racket version: 6.8
OS information: unix
C#
using System;
using System.Runtime.InteropServices;
class Program
{
static void Main()
{
// .NET version
var dotnetVersion = Environment.Version;
Console.WriteLine($"C# / .NET version: {dotnetVersion}"); //你可以使用.NET的内置方法和属性来获取C#(更准确的说是.NET的版本)
// Operating system information
var osDescription = RuntimeInformation.OSDescription;
var osArchitecture = RuntimeInformation.OSArchitecture;
Console.WriteLine($"OS: {osDescription}");
Console.WriteLine($"Architecture: {osArchitecture}");
}
}
C# / .NET version: 4.0.30319.42000
OS: Linux 5.2.11-100.fc29.x86_64 #1 SMP Thu Aug 29 12:52:22 UTC 2019
Architecture: X64
Ruby
# Ruby version
puts "Ruby version: #{RUBY_VERSION}"
# OS Information
require 'rbconfig'
os = RbConfig::CONFIG['host_os']
puts "OS: #{os}"
Ruby version: 2.5.5
OS: linux-gnu
Elixir
# Elixir version
IO.puts "Elixir version: #{System.version()}"
# Erlang/OTP version
# {:ok, otp_version} = :otp_release.erlang()
# IO.puts "Erlang/OTP version: #{otp_version}"
# OS Information
os_type = :os.type()
IO.puts "OS: #{inspect(os_type)}"
Elixir version: 1.7.2
OS: {:unix, :linux}
Mathematica
Print["Mathematica version: ", $Version];
Print["Operating system: ", $OperatingSystem];
Mathematica version: 12.0.1 for Linux x86 (64-bit) (October 16, 2019)
Operating system: Unix
Swift(存疑)
import Foundation
// Swift version is typically determined by the compiler and isn't available at runtime.
// However, you can use the Swift compiler directive to print it during compilation.
#if swift(>=5.5)
print("Swift version: 5.5 or later")
#elseif swift(>=5.4)
print("Swift version: 5.4")
#elseif swift(>=5.3)
print("Swift version: 5.3")
// add more checks if needed
#else
print("Swift version: unknown")
#endif
// Operating system information
let os = ProcessInfo.processInfo.operatingSystemVersion
print("OS: \(os)")
Swift version: unknown
OS: OperatingSystemVersion(majorVersion: -1, minorVersion: 0, patchVersion: 0)
PHP
<?php
// PHP version
$phpVersion = phpversion();
echo "PHP version: " . $phpVersion . "\n";
// Operating system information
$osInfo = php_uname();
echo "OS information: " . $osInfo . "\n";
?>
PHP version: 7.4.0-dev
OS information: Linux tio2 5.2.11-100.fc29.x86_64 #1 SMP Thu Aug 29 12:52:22 UTC 2019 x86_64
Dart
import 'dart:io';
void main() {
// Dart version
var dartVersion = Platform.version;
print('Dart version: $dartVersion');
// Operating system information
var osInfo = Platform.operatingSystem;
print('Operating system: $osInfo');
}
Dart version: 2.4.1 (Wed Aug 7 13:15:56 2019 +0200) on "linux_x64"
Operating system: linux
Kotlin
fun main() {
// Kotlin version
val kotlinVersion = KotlinVersion.CURRENT
println("Kotlin version: $kotlinVersion")
// Operating system information
val osName = System.getProperty("os.name")
println("Operating system: $osName")
}
Kotlin version: 1.3.31
Operating system: Linux
R
# R version
cat("R version:", R.version.string, "\n")
# Operating system information
sys_info <- Sys.info()
cat("Operating system:", sys_info["sysname"], "\n")
cat("Version:", sys_info["release"], "\n")
R version: R version 3.5.2 (2018-12-20)
Operating system: Linux
Version: 5.2.11-100.fc29.x86_64
Julia
# Julia version
println("Julia version: ", VERSION)
# Operating system information
println("Operating system: ", Sys.KERNEL)
Julia version: 1.0.0
Operating system: Linux
Pari/GP(存疑)
/* PARI/GP version */
print("PARI/GP version: ", version())
/* Operating system information */
print("Operating system: ", getenv("OS"))
PARI/GP version: [2, 11, 1]
Operating system: 0
Octave
% Octave version
disp(['Octave version: ' version])
% Operating system information
disp(['Operating system: ' computer])
Octave version: 4.2.2
Operating system: x86_64-redhat-linux-gnu
Zig
const std = @import("std");
const builtin = @import("builtin");
pub fn main() !void {
std.log.info("Zig version: {}", .{ builtin.zig_version });
}
info: Zig version: 0.11.0