Skip to main content
C Programming Basics
CHAPTER 03 Beginner

C Syntax and First Program

Updated: May 17, 2026
5 min read

# CHAPTER 3

C Syntax and First Program

1. Introduction

Every language has grammar rules — in programming, we call this syntax. Just as English sentences end with a period, C statements end with a semicolon. In this chapter, we will dissect the classic "Hello World" program line by line to understand how C is structured.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the structure of a C program.
  • Explain the purpose of #include and header files.
  • Understand the role of the main() function.
  • Use printf() to output text.
  • Write single and multi-line comments.

3. Anatomy of a C Program

Let's look at the most famous program in computer science:

c
123456
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Let's break it down line by line:

ComponentMeaning
#include <stdio.h>A preprocessor directive. It tells the compiler to include the Standard Input/Output library. Without this, printf() won't work.
int main() {The entry point of every C program. int means it returns an integer. { marks the beginning of the function body.
printf("Hello, World!\n");Prints text to the screen. \n creates a new line. The ; signifies the end of the statement.
return 0;Exits the main() function. Returning 0 tells the operating system that the program ran successfully without errors.
}Marks the end of the main() function.

4. Semicolons and Curly Braces

  • Semicolons (;): Every executable statement in C must end with a semicolon. Missing a semicolon is the #1 cause of beginner errors.
  • Curly Braces ({ }): Used to group multiple statements together into a block (like defining the body of a function).

5. Comments in C

Comments are text ignored by the compiler. They are used to explain code to humans.
c
123456
// This is a single-line comment.

/* 
   This is a 
   multi-line comment.
*/

6. The printf() Function

printf() is used for output. It stands for "print formatted".
c
1234567
#include <stdio.h>

int main() {
    printf("Line 1\n"); // \n moves the cursor to the next line
    printf("Line 2\tTabbed\n"); // \t creates a horizontal tab
    return 0;
}

7. Mini Project: Simple Calculator (Hardcoded)

Let's use our basic syntax to create a program that does simple math.
c
123456789101112131415
#include <stdio.h>

int main() {
    int a = 15;
    int b = 5;

    printf("==== SIMPLE CALCULATOR ====\n");
    printf("Addition: %d\n", a + b);
    printf("Subtraction: %d\n", a - b);
    printf("Multiplication: %d\n", a * b);
    printf("Division: %d\n", a / b);
    printf("===========================\n");

    return 0;
}

*(Note: %d is a format specifier telling printf to insert an integer here. We will cover this deeply in the next chapter).*

8. Memory-Level Explanation

When the OS runs your program, it looks for the memory address associated with main().
12
Execution Flow:
OS -> Calls main() -> Executes printf() -> Returns 0 to OS -> Process terminated.

If you omit return 0;, the C99 standard guarantees it will implicitly return 0, but writing it is best practice.

9. Common Mistakes

  • Forgetting the semicolon: printf("Hi") instead of printf("Hi");.
  • Misspelling stdio.h: Using <studio.h> (very common beginner typo).
  • Case Sensitivity: Main() or Printf() will cause errors. C is strictly case-sensitive.

10. Best Practices

  • Always indent code inside curly braces for readability.
  • Add comments explaining *why* you did something complex.
  • Keep the main function at the bottom of the file (or declare functions at the top).

11. Exercises

  1. 1. Write a C program that prints your name, age, and country on three separate lines.
  1. 2. Intentionally remove a semicolon from your program and try compiling it. Read the error message.
  1. 3. Write a program using multi-line comments at the top containing your name and the date.

12. MCQ Quiz with Answers

Question 1

What is the entry point of a C program?

Question 2

Which symbol marks the end of a statement in C?

Question 3

What does #include <stdio.h> do?

Question 4

Which of these is a correct single-line comment?

# Comment Answer: c) // Comment
Question 5

What does return 0; indicate in main()?

Question 6

What escape sequence represents a new line?

Q7. Is C a case-sensitive language? a) Yes b) No Answer: a) Yes
Question 8

What happens if you type <studio.h> instead of <stdio.h>?

Question 9

The { and } symbols are used to define a:

Question 10

printf() is a:

13. Interview Questions

  • Q: Explain the significance of the main() function in C.
  • Q: What is a preprocessor directive?
  • Q: Why is C case-sensitive, and how does it affect naming?

14. FAQs

Q: Does every C program need a main() function? A: Yes, every executable C program requires exactly one main() function.

Q: Can I use single quotes for strings? A: No. In C, single quotes ('A') are for single characters, while double quotes ("Text") are for strings.

15. Summary

The structure of a C program relies on the #include directive to load libraries, the main() function as the starting point, and {} to define code blocks. Semicolons must terminate statements, and printf is used for console output.

16. Next Chapter Recommendation

Now that you know how a program is structured, in Chapter 4: Variables and Data Types, we will learn how to store and manipulate data in memory.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·