C Hello World

Lesson 1.3: Your First C Program ("Hello, World!")


It's time to write your first C program!  We'll start with the classic "Hello, World!" program, a simple program that displays the message "Hello, World!" on the screen.


Writing the Code


Create a New File:


Open your code editor (e.g., Visual Studio Code) and create a new file.

Save it with a .c extension (e.g., hello.c).

Type (or Copy/Paste) the Following Code:


C

#include <stdio.h> // Include the standard input/output library


int main() {         // The main function - the starting point of your program

    printf("Hello, World!\n");  // Print the message

    return 0;       // Indicate successful program execution

}


Understanding the Code


#include <stdio.h>: This line tells the compiler to include the stdio.h (standard input/output) header file. This file contains the definition of the printf function, which we use to display output.


int main(): This is the main function, which is the heart of every C program. Execution always starts here.


printf("Hello, World!\n");: This line:


Calls the printf function.

Passes the string "Hello, World!\n" as an argument. The \n is a special character called a newline, which tells the computer to move the cursor to the next line after printing the message.

return 0;:  This line ends the main function and indicates that the program has finished successfully. The value 0 is a standard return code for successful program termination.


Compiling the Code


Open a Terminal or Command Prompt: Navigate to the directory where you saved your hello.c file.

Compile with GCC: Type the following command and press Enter:

Bash or ZSH

gcc hello.c -o hello


This command does two things:

It compiles your C code (hello.c) into machine code.

It creates an executable file named hello (or hello.exe on Windows) that you can run.

Running the Program


In VS Code (with Code Runner): Press Ctrl+Alt+N to run the code.


From the Command Line: Type the following command and press Enter:


Linux/macOS: ./hello

Windows: hello or hello.exe


The Output


You should see the message "Hello, World!" printed in the terminal or command prompt window. Congratulations! You've just compiled and run your first C program.


Key Points to Remember


Every C program must have a main function.

#include <stdio.h> is needed to use input/output functions like printf.

printf is used to display output to the console.

You compile C code using a C compiler like GCC.

The compiled code is an executable file that you can run.

Course Syllabus