Lesson 6 of 60 – First C Program
10%

First C Program

In this lesson, we will write our first C program and understand each part of the program step by step. We will use the printf() function to display a message on the screen.

Note: Save your C program with the .c extension, for example hello.c.

1. Our First C Program

Here is a simple C program:

#include <stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}

This program displays Hello World on the screen.

2. Create a C Source File

First, create a new file and save it with the .c extension.

hello.c

The .c extension tells us that the file contains C source code.

3. The #include Directive

The first line of our program is:

#include <stdio.h>

This includes the standard input/output header file so that functions such as printf() can be used.

4. What is stdio.h?

stdio.h stands for Standard Input Output Header. It provides declarations for commonly used input and output functions.

For example, printf() is declared through the standard I/O facilities provided by this header.

5. The main() Function

Every hosted C program has a function called main() where program execution begins.

int main()
{
}

6. Why int main()?

The int before main() means that the function returns an integer value to the environment when the program finishes.

int main()

7. Curly Braces

The curly braces { } define the body of the main() function.

int main()
{
    // Program statements
}

Statements written between these braces belong to the function.

8. The printf() Function

The printf() function is used to display formatted output.

printf("Hello World");

The text inside quotation marks is displayed on the screen.

9. Printing Text

We can print different messages using printf().

printf("Welcome to C Programming");

The output will be:

Welcome to C Programming

10. Semicolon in C

Most C statements end with a semicolon ;.

printf("Hello");
return 0;

Forgetting the semicolon can cause a compilation error.

11. The return 0 Statement

The statement:

return 0;

returns the integer value 0 from main(). A zero return value is conventionally used to indicate successful completion.

12. Complete Program

#include <stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}

This is a basic complete C program.

13. Program Output

When the program is compiled and executed, the output will be:

Hello World

14. Printing Your Name

We can use printf() to print our name.

#include <stdio.h>

int main()
{
    printf("My name is Rahul");
    return 0;
}

Replace the name with your own name when practicing.

15. Printing Multiple Messages

We can use multiple printf() statements in one program.

#include <stdio.h>

int main()
{
    printf("Hello\n");
    printf("Welcome to C");
    return 0;
}

16. The Newline Character

The escape sequence \n moves the output to a new line.

printf("Hello\nWorld");

Output:

Hello
World

17. Printing Numbers

Numbers can also be printed using printf().

printf("%d", 100);

Output:

100

18. Printing a Character

The %c format specifier can be used to print a character.

printf("%c", 'A');

Output:

A

19. Printing a Decimal Number

The %f format specifier can be used to print a floating-point value.

printf("%f", 10.5);

The output is displayed as a floating-point value according to the default formatting.

20. Adding Comments

Comments help explain the code and are ignored by the compiler.

#include <stdio.h>

int main()
{
    // Display a message
    printf("Hello World");
    return 0;
}

21. Single-Line Comment

A single-line comment starts with //.

// This is a comment

The compiler does not treat the comment as an executable statement.

22. Multi-Line Comment

A multi-line comment starts with /* and ends with */.

/*
   This is a
   multi-line comment
*/

23. Compile the Program

If the file is named hello.c, GCC can compile it using:

gcc hello.c -o hello

If there are no compilation errors, an executable program will be created.

24. Run the Program

After successful compilation, run the generated program.

Windows:

hello

Linux/macOS:

./hello

25. Program with Two Output Statements

#include <stdio.h>

int main()
{
    printf("My name is Rahul.\n");
    printf("I am learning C.");
    return 0;
}

The \n places the second message on a new line.

26. Common Mistakes

Beginners commonly make these mistakes:

  • Forgetting #include <stdio.h> when using printf().
  • Forgetting semicolons.
  • Using incorrect quotation marks.
  • Misspelling main or printf.
  • Saving the file with the wrong extension.

27. Correct Quotation Marks

Text passed to printf() should normally be written inside double quotation marks.

printf("Hello World");

Do not replace the programming quotation marks with typographic “smart quotes”.

28. Understand the Program Flow

The basic execution flow is:

  1. The program starts from main().
  2. The printf() statement executes.
  3. The message is displayed.
  4. return 0; ends the main() function.

29. Practice Program

Write a program that displays your name, city, and course.

#include <stdio.h>

int main()
{
    printf("Name: Rahul\n");
    printf("City: Patna\n");
    printf("Course: C Programming\n");
    return 0;
}

Change the values and practice the program yourself.

30. Summary of Your First C Program

A basic C program generally contains a header inclusion, the main() function, statements, and a return statement.

#include <stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}

You are now ready to learn the basic syntax rules of the C programming language.

📌 Key Points

  • main() is the starting function of a hosted C program.
  • #include <stdio.h> provides standard input/output declarations.
  • printf() is used to display formatted output.
  • Most C statements end with a semicolon.
  • \n is used to move output to a new line.
  • return 0; returns zero from main().
  • C source files normally use the .c extension.

🧠 Quick Quiz

Question: Which function is the starting point of a basic C program?