Computers / Programming / Hello Program Examples

This is a list of example programs in all the programming languages that I feel comfortable using. That’s not to say I’m an expert in all these programs but simply that I feel like I could make a decent sized programs in these languages if I had to.

Console Programs

These are compiled programs which use the console for output

C

CHello.c
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("Hello\n");
return 0;
}
1
2
3
4
5
6
7

C++

CppHello.cpp
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "Hello" << std::endl;
return 0;
}
1
2
3
4
5
6
7

C#

CsHello.cs
using System;
public class CsHello
{
public static int Main(string[] args)
{
Console.WriteLine("Hello");
return 0;
}
}
1
2
3
4
5
6
7
8
9
10

Visual Basic .NET

VbHello.vb
Imports System
Public Module VbHello
Public Function Main(args As String()) As Integer
Console.WriteLine("Hello")
Return 0
End Function
End Module
1
2
3
4
5
6
7
8

Java

JHello.java
public class JHello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
1
2
3
4
5

Shell Scripts

These are scripts meant to be ran by an OS Shell

Windows Command Shell

CmdHello.cmd
@ECHO Hello
1

GUI Programs

These are compiled programs which use a graphical user interface for output

Win32

Win32Hello.c
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
static TCHAR szAppName[] = TEXT("Win32Hello");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Class registration failed"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Win32 Hello"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL,
hInstance, NULL);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rc;
switch (message)
{
case WM_CREATE:
GetClientRect(hwnd, &rc);
CreateWindow(TEXT("static"), TEXT("Hello"),
WS_CHILD | WS_VISIBLE | SS_LEFT,
0, 0, rc.right, rc.bottom,
hwnd, NULL,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

WinForms

WinFormsHello.cs
using System.Windows.Forms;
namespace WinFormsHello
{
public partial class WinFormsHello : Form
{
public WinFormsHello()
{
InitializeComponent();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
WinFormsHello.Designer.cs
namespace WinFormsHello
{
partial class WinFormsHello
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Margin = new System.Windows.Forms.Padding(0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(31, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Hello";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

WinForms (Using Model-View-Presenter pattern)

ITextView.vb
Public Interface ITextView
WriteOnly Property Text As String
End Interface
1
2
3
WinFormsHelloView.vb
Imports WinFormsMvpHello
Public Class WinFormsHelloView
Implements ITextView
Private presenter As WinFormsHelloPresenter
Public Sub New()
InitializeComponent()
presenter = New WinFormsHelloPresenter(Me)
End Sub
Private WriteOnly Property IMainForm_Text As String Implements ITextView.Text
Set(value As String)
m_textLabel.Text = value
End Set
End Property
End Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
WinFormsHelloView.Designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class WinFormsHelloView
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.m_textLabel = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'm_textLabel
'
Me.m_textLabel.AutoSize = True
Me.m_textLabel.Location = New System.Drawing.Point(0, 0)
Me.m_textLabel.Margin = New System.Windows.Forms.Padding(0)
Me.m_textLabel.Name = "m_textLabel"
Me.m_textLabel.Size = New System.Drawing.Size(0, 13)
Me.m_textLabel.TabIndex = 0
'
'MainForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 261)
Me.Controls.Add(Me.m_textLabel)
Me.Name = "MainForm"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents m_textLabel As Label
End Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
IGetTextModel.vb
Public Interface IGetTextModel
Function GetText() As string
End Interface
1
2
3
WinFormsHelloModel.vb
Public Class WinFormsHelloModel
Implements IGetTextModel
Public Function GetText() As String Implements IGetTextModel.GetText
Return "Hello"
End Function
End Class
1
2
3
4
5
6
7
WinFormsHelloPresenter.vb
Public Class WinFormsHelloPresenter
Private m_model As IGetTextModel
Private m_view As ITextView
Public Sub New(view As ITextView)
Me.New(view, New WinFormsHelloModel())
End Sub
Public Sub New(view As ITextView, model As IGetTextModel)
m_view = view
m_model = model
m_view.Text = m_model.GetText()
End Sub
End Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

WPF

WpfHello.xaml
<Window x:Class="WpfHello"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfHello"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Text="Hello" />
</Grid>
</Window>
1
2
3
4
5
6
7
8
9
10
11
12
WpfHello.xaml.vb
Class WpfHello
Public Sub New()
InitializeComponent()
End Sub
End Class
1
2
3
4
5

WPF (Using Model-View-ViewModel pattern)

WpfHelloView.xaml
<Window x:Class="WpfMvvmHello.WpfHelloView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfMvvmHello"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Text="{Binding Text}" />
</Grid>
</Window>
1
2
3
4
5
6
7
8
9
10
11
12
WpfHelloView.xaml.cs
using System.Windows;
namespace WpfMvvmHello
{
public partial class WpfHelloView : Window
{
public WpfHelloView()
{
InitializeComponent();
DataContext = new WpfHelloViewModel();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
IGetTextModel.cs
namespace WpfMvvmHello
{
interface IGetTextModel
{
string GetText();
}
}
1
2
3
4
5
6
7
WpfHelloModel.cs
namespace WpfMvvmHello
{
class WpfHelloModel : IGetTextModel
{
public string GetText()
{
return "Hello";
}
}
}
1
2
3
4
5
6
7
8
9
10
WpfHelloViewModel.cs
using System.ComponentModel;
namespace WpfMvvmHello
{
class WpfHelloViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private IGetTextModel m_model;
public WpfHelloViewModel()
:this(new WpfHelloModel())
{
}
public WpfHelloViewModel(IGetTextModel model)
{
m_model = model;
Text = m_model.GetText();
}
private string m_text;
public string Text
{
get
{
return m_text;
}
set
{
if (m_text != value)
{
m_text = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Text)));
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

Web

These are web pages and scripts ran by a web server and/or internet browser

HTML

HtmlHello.html
<!DOCTYPE html>
<html>
<body>
<p>Hello</p>
</body>
</html>
1
2
3
4
5
6

CSS

CssHello.css
p::after{
content:"Hello";
}
1
2
3
CssHello.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="CssHello.css">
</head>
<body>
<p></p>
</body>
</html>
1
2
3
4
5
6
7
8
9

JavaScript

JsHello.js
function SetPContent(id, content) {
var element = document.getElementById(id);
if (element != null && element.nodeName == "P") {
element.innerHTML = content
}
}
1
2
3
4
5
6
7
JsHello.html
<!DOCTYPE html>
<html>
<head>
<script src="JsHello.js"></script>
</head>
<body onload="SetPContent('PBlock', 'Hello');">
<p id="PBlock"></p>
</body>
</html>
1
2
3
4
5
6
7
8
9

TypeScript

TsHello.ts
function SetPContent(id:string, content:string) {
var element:any = document.getElementById(id);
if (element != null && typeof (element) == typeof (HTMLParagraphElement)) {
var pElement = <HTMLParagraphElement>element;
pElement.innerHTML = content
}
}
1
2
3
4
5
6
7
8
TsHello.html
<!DOCTYPE html>
<html>
<head>
<script src="TsHello.js"></script>
</head>
<body onload="SetPContent('PBlock', 'Hello');">
<p id="PBlock"></p>
</body>
</html>
1
2
3
4
5
6
7
8
9

PHP

PhpHello.php
<!DOCTYPE html>
<html>
<body>
<p><?= "Hello"?></p>
</body>
</html>
1
2
3
4
5
6

Database

These are queries ran by a database engine

Transact-SQL

TSqlHello.sql
SELECT 'Hello'
1

Assembly

These are compiled programs coded in a language very simular to the native instruction set of the machine/virtual machine they run on

x86 Assembly (MS-DOS MASM)

AsmHello2.asm
title Hello Program
; this program displays hello
dosseg
.model small
.stack 100h
.data
hello_message db 'Hello',0dh,0ah,'$'
.code
main proc
mov ax, @data
mov ds, ax
mov ah, 9
mov dx, offset hello_message
int 21h
mov ax, 4C00h
int 21h
main endp
end main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

8080 Assembly (Altair 8800 using 2SIO board)

8080Hello.asm
ORG 0
MVI A, 03H ; 0000 3E 03 Reset 2SIO port
OUT 10H ; 0002 D3 10
MVI A, 15H ; 0004 3E 15 Set 2SIO port to 8n1
OUT 10H ; 0006 D3 10
LXI H, MSG ; 0008 21 FE 00 Load message address
CHECK: IN 10H ; 000B DB 10 Check 2SIO output ready
ANI 02H ; 000D E6 02
JZ CHECK ; 000F CA 0B 00
MOV A, M ; 0012 7E Load character
CPI 00H ; 0013 FE 00 Check if null found
JZ FINISH ; 0015 CA 1E 00
OUT 11H ; 0018 D3 11 Output character
INX H ; 001A 23 Move to next character
JMP CHECK ; 001B C3 0B 00
FINISH: HLT ; 001E 76
MSG: "Hello\r\n\0"
; 00FE 48 65 6C 6C 6F 0D 0A 00
END
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Common Intermediate Language (CIL)

Cilhello.il
.assembly extern mscorlib {}
.assembly Test
{
.ver 1:0:1:0
}
.module test.exe
.method static void main() cil managed
{
.maxstack 1
.entrypoint
ldstr "Hello"
call void [mscorlib]System.Console::WriteLine(string)
ret
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18