LearnContact
Lesson 410 min read

Your First C Program

Every programmer begins with a simple program. In this lesson, you will write your first C program (Hello World) and understand the purpose of every line of code.

Introduction

Traditionally, the first program written in almost every programming language displays the message "Hello, World!" on the screen. Although this program is very small, it introduces the basic structure of a C program and helps you understand how a program is written, compiled, and executed.

Fun Fact

The "Hello, World!" program was introduced in the book "The C Programming Language" by Brian Kernighan and Dennis Ritchie in 1978.

What is the First Program?

The first program is a simple C program that prints a message on the screen. It is commonly called the Hello World Program. Programmers around the world use this program as their first step when learning a new programming language.

Why Do We Start with Hello World?

The Hello World program helps you:

Verify Setup

Confirm that your compiler is working correctly.

Understand Structure

Learn the basic structure of a C program.

Learn Compilation

Understand how to compile and execute a program.

Build Confidence

Gain confidence before moving to advanced topics.

Writing Your First Program

Create a new file named hello.c and write the following program:

hello.c
#include <stdio.h>

int main()
{
    printf("Hello, World!");

    return 0;
}

Save the file before compiling it.

Expected Output
Hello, World!

Understanding the Program

Let's understand each part of the program line by line.

Line 1: #include <stdio.h>

This is called a Preprocessor Directive. It tells the compiler to include the Standard Input Output Library, which contains functions like printf() and scanf().

Without this header file, the compiler would not recognize the printf() function. Think of it as importing a toolbox before starting work.

Line 2: int main()

This defines the main function. Every C program starts execution from the main() function. When the program begins running, the operating system automatically calls this function.

There can be only one main() function in a C program.

Line 3: {

The opening curly brace marks the beginning of the main() function. Everything inside these braces belongs to the function.

Line 4: printf("Hello, World!");

The printf() function displays text on the screen. In this example, it prints: Hello, World!

The text inside double quotation marks is called a string.

Line 5: return 0;

This statement tells the operating system that the program finished successfully. A return value of 0 usually indicates successful execution.

Line 6: }

The closing curly brace marks the end of the main() function.

Program Execution Flow

The execution of the program follows this order:

1. Program Starts
2. Read Header Files
3. Enter main()
4. Execute printf()
5. Display Output
6. Execute return 0
7. Program Ends

How to Compile & Run the Program

How to Compile

After saving the source file, the compiler converts it into machine code. The compiler checks:

  • Syntax
  • Keywords
  • Function names
  • Errors

How to Run

After successful compilation:

  • Execute the generated program
  • The operating system loads it into memory
  • The main() function begins execution
  • The message is displayed
  • The program terminates

Real-World Analogy

Imagine a television. When you press the Power Button, the TV starts.

Computer
main()
Program Starts

The main() function is like the power button of a C program. Without it, the program cannot start.

Important Components of the First Program

ComponentPurpose
#includeIncludes required library files
main()Starting point of the program
{ }Defines the function body
printf()Displays output on the screen
return 0Ends the program successfully

Rules to Remember

Important Rules
  • Every C program must have one main() function.
  • Statements usually end with a semicolon (;).
  • Curly braces define the beginning and end of a block.
  • C is case-sensitive. printf is different from PRINTF.
  • Save C source files with the .c extension.

Common Beginner Mistakes

Missing Semicolon
// Incorrect:
printf("Hello")

// Correct:
printf("Hello");
Missing Curly Braces
// Incorrect:
int main()
printf("Hello");

// Correct:
int main()
{
    printf("Hello");
}
Incorrect Header File
// Incorrect:
#include <studio.h>

// Correct:
#include <stdio.h>
Forgetting Double Quotes
// Incorrect:
printf(Hello);

// Correct:
printf("Hello");
Wrong File Extension
// Incorrect:
hello.txt

// Correct:
hello.c

Best Practices

  • Save your program before compiling.
  • Use meaningful file names.
  • Write one statement per line.
  • Maintain proper indentation.
  • Always include the required header files.
  • Read compiler error messages carefully.

Frequently Asked Questions

Why is main() required?

The operating system needs a starting point to execute the program. The main() function provides that starting point.

Can I change the name of main()?

No. The compiler expects the program to begin with the main() function.

Why do we write return 0;?

It indicates that the program completed successfully without errors.

What happens if I remove printf()?

The program will execute, but nothing will be displayed on the screen.

Can a program have multiple main() functions?

No. A C program can have only one main() function.

Key Takeaways

  • Every C program starts execution from the main() function.
  • The printf() function displays output on the screen.
  • #include <stdio.h> provides access to standard input and output functions.
  • Curly braces define the beginning and end of a function.
  • return 0; indicates successful program termination.

Summary

The Hello World program is the first step in learning C programming. Although it is simple, it introduces the essential building blocks of every C program: the header file, the main() function, the printf() function, and the return statement. Understanding these components will make it easier to learn more advanced concepts in the following lessons.

Next Lesson →

Program Structure