#include <stdio.h>int main(void)
{
printf("Hello, world!\n");
return 0;
}#include <stdio.h>int main(void)
{
printf("안녕 세상아!\n");
return 0;
}Hello, world!는 프로그래밍을 처음 배울 때 가장 널리 사용되는 예제 문장이다. 일반적으로 화면, 콘솔, 터미널, 로그 등에 짧은 문자열을 출력하는 프로그램을 작성함으로써 개발 환경이 정상적으로 설치되었는지, 소스 코드가 올바르게 실행되는지, 기본 입출력 문법을 이해했는지 확인하는 데 사용된다.
많은 프로그래밍 입문서와 튜토리얼은 첫 예제로 Hello, world!를 사용한다. 이 관례는 브라이언 커니핸(Brian W. Kernighan)이 작성한 B 언어 튜토리얼의 예제와, 브라이언 커니핸과 데니스 리치(Dennis Ritchie)가 함께 쓴 《The C Programming Language》의 C 언어 예제를 통해 널리 알려졌다.[1][2]
개요[편집 / 원본 편집]
Hello, world! 프로그램은 보통 다음 목적을 가진다.
- 개발 환경이 정상적으로 설치되었는지 확인한다.
- 컴파일러, 인터프리터, 런타임, 셸, 브라우저 등의 실행 흐름을 확인한다.
- 문자열 출력, 함수 호출, 프로그램 진입점 등 가장 기본적인 문법을 익힌다.
- 새 언어의 코드 구조를 짧은 예제로 비교한다.
다만 언어마다 실행 방식은 다르다. 어떤 언어는 컴파일이 필요하고, 어떤 언어는 인터프리터로 바로 실행되며, 어떤 언어는 브라우저나 특정 런타임, 하드웨어 시뮬레이터가 필요하다.
역사[편집 / 원본 편집]
Hello, world!라는 문구는 C 언어의 대표적인 첫 예제로 널리 알려져 있다. 다만 그 기원은 C 언어 책 하나로만 설명하기보다는, 벨 연구소(Bell Laboratories)에서 작성된 초기 언어 튜토리얼 문맥까지 함께 보는 편이 정확하다.
브라이언 커니핸은 B 언어 튜토리얼에서 외부 변수와 문자 출력을 설명하기 위해 비슷한 형태의 예제를 사용하였다. 이후 《The C Programming Language》에서 C 언어의 첫 예제로 간단한 문자열 출력 프로그램이 제시되면서, Hello, world!는 새 프로그래밍 언어를 배울 때 처음 작성하는 상징적인 예제가 되었다.
표현 차이[편집 / 원본 편집]
언어마다 Hello, world!를 출력하는 방식은 다음과 같은 차이를 보인다.
각 언어로 표현한 Hello, world![편집 / 원본 편집]
Ada[편집 / 원본 편집]
with Ada.Text_IO;
use Ada.Text_IO;
procedure Hello_World is
begin
Put_Line("Hello, world!");
end Hello_World;ActionScript[편집 / 원본 편집]
package {
import flash.display.Sprite;
public class HelloWorld extends Sprite {
public function HelloWorld() {
trace("Hello, world!");
}
}
}Agda[편집 / 원본 편집]
open import IO
main = run (putStr "Hello, world!")Assembly[편집 / 원본 편집]
아래 예제는 32비트 리눅스 x86 환경에서 시스템 호출을 사용해 문자열을 출력하는 예이다.
section .data
hello: db 'Hello, world!', 0Ah
helloLen: equ $ - hellosection .text
global _start
_start:
mov edx, helloLen
mov ecx, hello
mov ebx, 1
mov eax, 4
int 80h
mov eax, 1
xor ebx, ebx
int 80h=== AutoIt ===
MsgBox(0, "Message", "Hello, world!")Bash[편집 / 원본 편집]
#!/bin/bash
echo "Hello, world!"
printf "Hello, world!\n"Batch[편집 / 원본 편집]
@echo off
echo Hello, world!B[편집 / 원본 편집]
초기 B 언어 튜토리얼에서는 다음과 같이 여러 외부 변수에 문자열 조각을 나누어 저장한 뒤 출력하는 예제가 사용되었다.
main()
{
extrn a, b, c;putchar(a);
putchar(b);
putchar(c);
putchar('!*n');
}
a 'hell';
b 'o, w';
c 'orld';Brainfuck[편집 / 원본 편집]
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++++++++++++++.<hr>.<<+++++++++++++++.>.+++.<hr>.<hr>.>+.C[편집 / 원본 편집]
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}C++[편집 / 원본 편집]
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}C#[편집 / 원본 편집]
using System;
namespace HelloWorld
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
}
}
}Common Lisp[편집 / 원본 편집]
(format t "Hello, world!~%")CSS[편집 / 원본 편집]
CSS는 일반적인 프로그래밍 언어라기보다는 스타일시트 언어이다. 아래 예제는 문서의 body 앞에 문자열을 표시한다.
body::before {
content: "Hello, world!";
}D[편집 / 원본 편집]
import std.stdio;
void main()
{
writeln("Hello, world!");
}Dart[편집 / 원본 편집]
void main() {
print("Hello, world!");
}Elixir[편집 / 원본 편집]
IO.puts("Hello, world!")Erlang[편집 / 원본 편집]
-module(hello).
-export([main/0]).
main() ->
io:format("Hello, world!~n").F#[편집 / 원본 편집]
printfn "Hello, world!"Fortran[편집 / 원본 편집]
program HelloWorld
print *, "Hello, world!"
end program HelloWorldGo[편집 / 원본 편집]
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}Groovy[편집 / 원본 편집]
println "Hello, world!"Haskell[편집 / 원본 편집]
main :: IO ()
main = putStrLn "Hello, world!"HTML[편집 / 원본 편집]
HTML은 프로그래밍 언어라기보다는 마크업 언어이다. 아래 예제는 웹 문서에 문자열을 표시한다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Hello, world!</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>=== Java ===
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}JavaScript[편집 / 원본 편집]
console.log("Hello, world!");
document.body.textContent = "Hello, world!";Kotlin[편집 / 원본 편집]
fun main() {
println("Hello, world!")
}Lua[편집 / 원본 편집]
print("Hello, world!")Objective-C[편집 / 원본 편집]
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"Hello, world!");
}
return 0;
}OCaml[편집 / 원본 편집]
print_endline "Hello, world!"Pascal[편집 / 원본 편집]
program HelloWorld;
begin
writeln('Hello, world!');
end.Perl[편집 / 원본 편집]
use strict;
use warnings;
print "Hello, world!\n";PHP[편집 / 원본 편집]
<?php
echo "Hello, world!\n";PowerShell[편집 / 원본 편집]
Write-Output "Hello, world!"Python[편집 / 원본 편집]
print("Hello, world!")R[편집 / 원본 편집]
cat("Hello, world!\n")Raku[편집 / 원본 편집]
say "Hello, world!";Ruby[편집 / 원본 편집]
puts "Hello, world!"
print "Hello, world!\n"Rust[편집 / 원본 편집]
fn main() {
println!("Hello, world!");
}Scala[편집 / 원본 편집]
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}Scheme[편집 / 원본 편집]
(display "Hello, world!")
(newline)SQL[편집 / 원본 편집]
SQL은 일반적인 프로그래밍 언어라기보다는 데이터베이스 질의 언어이다. 아래 예제는 문자열 값을 하나의 결과 행으로 조회한다.
SELECT 'Hello, world!' AS greeting;Swift[편집 / 원본 편집]
print("Hello, world!")TypeScript[편집 / 원본 편집]
const message: string = "Hello, world!";
console.log(message);Verilog[편집 / 원본 편집]
module main;
initial begin
$display("Hello, world!");
$finish;
end
endmoduleVHDL[편집 / 원본 편집]
use std.textio.all;
entity main is
end main;
architecture behaviour of main is
begin
process
variable hello : line;
begin
write(hello, String'("Hello, world!"));
writeline(output, hello);
wait;
end process;
end behaviour;Visual Basic .NET[편집 / 원본 편집]
Module HelloWorld
Sub Main()
Console.WriteLine("Hello, world!")
End Sub
End ModuleZig[편집 / 원본 편집]
const std = @import("std");
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("Hello, world!\n", .{});
}난해한 프로그래밍 언어의 예시[편집 / 원본 편집]
일부 난해한 프로그래밍 언어는 실용성보다는 표현 방식, 장난성, 언어 설계 실험 자체에 의미가 있다. 따라서 아래 예제들은 일반적인 입문용 예제라기보다는 Hello, world!가 여러 형태로 변형될 수 있음을 보여 주는 사례에 가깝다.
몰랭(Mollang)[편집 / 원본 편집]
몰????.??????????? 모올????.????.?? 모오올몰모올
아모오올!!!!루 모오올모올 아모오올!!!!!!!루 아모오올루 아모오올루
아모오올???루 아몰루 아모올루 몰모올 아몰???????????????????????????????????????????루 아모오올???루
아몰모올??????루 아모오올루 아모오올!!!!!!!!루 아모올?루엄랭[편집 / 원본 편집]
어떻게
엄
어엄
어어엄
어어어엄
어어어어엄
어어어어어엄
어어어어어어엄
어어어어어어어엄
엄어..........
동탄어?준..... .....
어엄어어.......
어어엄어어어..........
어어어엄어어어어...
어어어어엄어어어어어.
엄어,
준.............
어엄어어..
식어어ㅋ
어어엄어어어.
식어어어ㅋ
어어엄어어어.......
식어어어ㅋ
식어어어ㅋ
어어엄어어어...
식어어어ㅋ
어어어엄어어어어..............
식어어어어ㅋ
어어어엄어어어어<sub></sub><sub></sub><sub></sub>
식어어어어ㅋ
어엄어어...............
식어어ㅋ
식어어어ㅋ
어어엄어어어...
식어어어ㅋ
어어엄어어어<sub></sub><sub>
식어어어ㅋ
어엄어어어</sub><sub></sub>,,
식어어어ㅋ
어어어엄어어어어.
식어어어어ㅋ
화이팅!.,
이 사람이름이냐ㅋㅋ활용[편집 / 원본 편집]
Hello, world!는 단순한 예제이지만 다음과 같은 상황에서 자주 사용된다.
- 새 프로그래밍 언어를 처음 배울 때
- 컴파일러 또는 인터프리터 설치를 확인할 때
- 개발 환경, 빌드 도구, 패키지 관리자 설정을 검증할 때
- 웹 서버, 런타임, 컨테이너, 배포 환경의 기본 동작을 확인할 때
- 임베디드 장치나 하드웨어 기술 언어에서 출력 또는 시뮬레이션 동작을 확인할 때
주의점[편집 / 원본 편집]
Hello, world! 예제는 언어의 전체 특징을 보여 주지는 않는다. 예를 들어 메모리 관리, 오류 처리, 모듈 시스템, 비동기 처리, 객체 지향, 함수형 프로그래밍, 타입 시스템 같은 핵심 개념은 이 예제만으로 알기 어렵다.
또한 일부 예제는 실행 환경에 따라 수정이 필요할 수 있다. 예를 들어 Assembly 예제는 운영체제와 CPU 아키텍처에 따라 완전히 달라질 수 있으며, Verilog와 VHDL은 일반적인 콘솔 프로그램이 아니라 하드웨어 기술 언어의 시뮬레이션 예제로 보아야 한다.