いろいろなプログラミング言語でHello World(コンソール編)
いろいろなプログラミング言語に触れてみる。(随時更新)
ソースコードはこちらからもご覧いただけます。
https://github.com/FlyingHighAgain/HelloWorld
Ada
ソース(hello.adb)
-- hello.adb with Ada.Text_IO; use Ada.Text_IO; procedure Hello is begin Put_Line("Hello, World!"); end Hello;
コンパイルと実行結果
$ gnatls -v GNATLS 4.6 Copyright (C) 1997-2010, Free Software Foundation, Inc. $ gcc -c hello.adb $ gnatbind hello $ gnatlink hello $ ./hello Hello, World!
Assembler(x86)
ソース(hello.asm)
; hello.asm section .text global _start _start: ; entry point mov edx,len mov ecx,msg mov ebx,1 ; file descriptor(stdout) mov eax,4 ; system call number (sys_write) int 0x80; ; system call mov eax,1 ; system call number (sys_exit) int 0x80 ; system call section .data msg db 'Hello, World!',0xa len equ $ - msg
コンパイルと実行結果
$ nasm -v NASM version 2.09.10 compiled on Oct 17 2011 $ nasm -f elf hello.asm $ ld -s -o hello hello.o $ ./hello Hello, World!
C
ソース(hello.c)
/* hello.c */ #include <stdio.h> int main() { printf("Hello, World\n"); return 0; }
コンパイルと実行結果
$ gcc --version gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -o hello hello.c $ ./hello Hello, World
C#
ソース(hello.cs)
// hello.cs using System; class HelloWorld { [STAThread] static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }
コンパイルと実行結果
$ mcs --version Mono C# compiler version 2.10.8.1 $ mono --version Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-1ubuntu2.2) Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com $ mcs hello.cs $ mono hello.exe Hello, World!
C++
ソース(hello.cpp)
// hello.cpp #include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
コンパイルと実行結果
$ g++ --version g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ g++ hello.cpp -o hello $ ./hello Hello, World!
COBOL
ソース(hello.cob)
123456*8901234567890123456789012345678901234567890 * hello.cob IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. PROCEDURE DIVISION. DISPLAY "Hello, World!". STOP RUN.
コンパイルと実行結果
$ cobc -version cobc (OpenCOBOL) 1.0.0 Copyright (C) 2001-2007 Keisuke Nishida Copyright (C) 2007 Roger While $ cobc -x hello.cob $ ./hello Hello, World!
CoffeeScript
ソース(hello.coffee)
# hello.coffee hello = -> console.log("Hello, World!") hello()
コンパイルと実行結果
$ coffee -version CoffeeScript version 1.2.0 $ coffee -c hello.coffee $ node hello.js Hello, World!
D
ソース(hello.d)
// hello.d private import std.stdio; void main() { writeln("Hello, World!"); }
コンパイルと実行結果
$dmd DMD32 D Compiler v2.060 Copyright (c) 1999-2012 by Digital Mars written by Walter Bright Documentation: http://www.dlang.org/index.html ... $ dmd hello.d $ ./hello Hello, World!
Erlang
ソース(hello.erl)
%% hello.erl -module(hello). -export([start/0]). start() -> io:fwrite("Hello, World!\n").
コンパイルと実行結果
$ erl -version Erlang (SMP,ASYNC_THREADS) (BEAM) emulator version 5.8.5 $ erlc hello.erl $ erl -noshell -s hello start -s init stop Hello, World!
Fortran
ソース(hello.f90)
! hello.f90 PROGRAM HELLO PRINT *, 'Hello, World!' END PROGRAM
コンパイルと実行結果
$ gfortran --version GNU Fortran (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 Copyright (C) 2011 Free Software Foundation, Inc. GNU Fortran comes with NO WARRANTY, to the extent permitted by law. You may redistribute copies of GNU Fortran under the terms of the GNU General Public License. For more information about these matters, see the file named COPYING $ gfortran -o hello hello.f90 $ ./hello Hello, World!
Go
ソース(hello.go)
// hello.go package main import "fmt" func main() { fmt.Print("Hello, World!\n") }
コンパイルと実行結果
$ go version go version go1 $ go build hello.go $ ./hello Hello, World!
Haskell
ソース(hello.hs)
-- hello.hs main=putStrLn "Hello, World!"
コンパイルと実行結果
$ ghc --version The Glorious Glasgow Haskell Compilation System, version 7.4.1 $ ghc -o hello hello.hs [1 of 1] Compiling Main ( hello.hs, hello.o ) Linking hello ... $ ./hello Hello, World!
Io
ソース(hello.io)
// hello.io "Hello, World" println
実行結果
$ io_static --version Io Programming Language, v. 20110905 $ io_static hello.io Hello, World
Java
ソース(Hello.java)
// Hello.java public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); } }
コンパイルと実行
$ javac -version javac 1.8.0 $ javac Hello.java $ java Hello Hello, World!
JavaScript
ソース(hello.js)
// hello.js hello = function() { if (typeof console === "object") { console.log("Hello, World!"); } else { print("Hello, World!"); } }; hello();
実行結果
$ rhino hello.js Hello, World! $ node hello.js Hello, World!
OCaml
ソース(hello.ml)
(* hello.ml *) print_string "Hello, World\n"
コンパイルと実行結果
$ ocamlc -v The Objective Caml compiler, version 3.12.1 Standard library directory: /usr/lib/ocaml $ ocamlc -o hello hello.ml $ ./hello Hello, World
Pascal
ソース(hello.pas)
{ hello.pas } program hello; begin WriteLn('Hello, World!'); end.
コンパイルと実行結果
$ fpc hello.pas Free Pascal Compiler version 2.4.4-3.1 [2012/01/04] for i386 Copyright (c) 1993-2010 by Florian Klaempfl Target OS: Linux for i386 Compiling hello.pas Linking hello /usr/bin/ld: warning: link.res contains output sections; did you forget -T? 7 lines compiled, 0.0 sec $ ./hello Hello, World!
Prolog
ソース(hello.pl)
/* hello.pl */ :- initialization(main). main :- write('Hello, World!'), nl, halt.
コンパイルと実行結果
$ gplc --version Prolog compiler (GNU Prolog) 1.3.0 By Daniel Diaz Copyright (C) 1999-2007 Daniel Diaz GNU Prolog comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of GNU Prolog under the terms of the GNU General Public License. For more information about these matters, see the files named COPYING. $ gplc hello.pl $ ./hello Hello, World!
R
ソース(hello.r)
# hello.r print("Hello, World!")
実行結果
$ R --version R version 2.14.1 (2011-12-22) Copyright (C) 2011 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i686-pc-linux-gnu (32-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under the terms of the GNU General Public License version 2. For more information about these matters see http://www.gnu.org/licenses/. $ R -q -f hello.r > # hello.r > > print("Hello, World!") [1] "Hello, World!" > >
Ruby
ソース(hello.rb)
# hello.rb puts "Hello, World!"
実行結果
$ ruby --version ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux] $ ruby hello.rb Hello, World!
Scala
ソース(hello.scala)
// hello.scala object HelloWorld { def main(args: Array[String]) { println("Hello, World!") } }
コンパイルと実行結果
$ scalac -version Scala compiler version 2.10.4 -- Copyright 2002-2013, LAMP/EPFL $ scalac hello.scala $ scala HelloWorld Hello, World!
Scheme
ソース(hello.scm)
;hello.scm (display "Hello, World!") (newline)
実行結果
$ gosh -V Gauche scheme shell, version 0.9.1 [utf-8,pthreads], i686-pc-linux-gnu $ gosh hello.scm Hello, World!
Smalltalk
ソース(hello.st)
"hello.st" Transcript show: 'Hello, World!'; cr
実行結果
$ gst --versionGNU Smalltalk version 3.2.4 Copyright 2009 Free Software Foundation, Inc. Written by Steve Byrne (sbb@gnu.org) and Paolo Bonzini (bonzini@gnu.org) GNU Smalltalk comes with NO WARRANTY, to the extent permitted by law. You may redistribute copies of GNU Smalltalk under the terms of the GNU General Public License. For more information, see the file named COPYING. Using default kernel path: /usr/share/gnu-smalltalk/kernel Using default image path: /usr/lib/gnu-smalltalk $ gst hello.st Hello, World!
Visual Basic.net
ソース(hello.vb)
' hello.vb Imports System Module HelloWorld Sub Main() Console.WriteLine("Hello, World!") End Sub End Module
コンパイルと実行結果
$ vbnc hello.vb Visual Basic.Net Compiler version 0.0.0.5943 Copyright (C) 2004-2010 Rolf Bjarne Kvinge. All rights reserved. Assembly 'hello, Version=0.0, Culture=neutral, PublicKeyToken=null' saved successfully to '/HelloWorld/VisualBasic.NET/hello.exe'. Compilation successful Compilation took 00:00:01.0953170 $ mono hello.exe Hello, World!