zaro

How to Code Hello World?

Published in Programming Basics 3 mins read

The simplest way to code "Hello World" depends on the programming language you're using, but here's a classic example in C:

C - Hello World

This is one of the most fundamental examples used to introduce programming languages.

Steps:

  1. Create a file: Use a text editor to create a new file. Name it hello.c.

  2. Enter the code: Paste the following C code into the hello.c file:

    #include <stdio.h>
    
    int main(void) {
        printf("Hello World!\n");
        return 0;
    }
  3. Save the file: Save the file as hello.c.

  4. Compile the program: Open a terminal or command prompt. Navigate to the directory where you saved hello.c. Use a C compiler (like GCC) to compile the code:

    gcc hello.c -o hello

    This command tells the compiler to create an executable file named hello.

  5. Run the program: In the terminal, execute the compiled program:

    ./hello

    This will print "Hello World!" to the console.

Explanation:

  • #include <stdio.h>: This line includes the standard input/output library, which provides functions like printf.
  • int main(void): This is the main function where the program execution begins.
  • printf("Hello World!\n");: This line uses the printf function to print the string "Hello World!" to the console. \n adds a newline character, moving the cursor to the next line.
  • return 0;: This indicates that the program executed successfully.

Python - Hello World

A much simpler example, using Python:

Steps:

  1. Create a file: Create a new file, hello.py.

  2. Enter the code: Paste the following Python code:

    print("Hello World!")
  3. Save the file: Save the file as hello.py.

  4. Run the program: Open a terminal or command prompt. Navigate to the directory where you saved hello.py, and run the program using the Python interpreter:

    python hello.py

Explanation:

  • print("Hello World!"): This line uses the built-in print function to display the string "Hello World!" on the console.

Java - Hello World

Here's "Hello World" in Java:

Steps:

  1. Create a file: Create a new file named HelloWorld.java (note the capital H and W; class names in Java generally start with a capital letter).

  2. Enter the code: Paste the following Java code into the file:

     public class HelloWorld {
         public static void main(String[] args) {
             System.out.println("Hello World!");
         }
     }
  3. Save the file: Save the file as HelloWorld.java.

  4. Compile the program: Open a terminal or command prompt, navigate to the directory where you saved HelloWorld.java, and compile the code using the Java compiler:

     javac HelloWorld.java

    This will create a file named HelloWorld.class.

  5. Run the program: In the terminal, execute the compiled Java program:

     java HelloWorld

Explanation:

  • public class HelloWorld: Defines a public class named HelloWorld. The filename must match the class name (case-sensitive).
  • public static void main(String[] args): This is the main method, where execution begins.
  • System.out.println("Hello World!");: This line uses System.out.println to print "Hello World!" to the console.

These are just a few examples, but the basic principle of creating a "Hello World" program remains the same across many programming languages: write code to output the phrase "Hello World!" to the console.