C Syntax and First Program
# 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
#includeand 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:
Let's break it down line by line:
| Component | Meaning |
|---|---|
#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.
6. The printf() Function
printf() is used for output. It stands for "print formatted".
7. Mini Project: Simple Calculator (Hardcoded)
Let's use our basic syntax to create a program that does simple math.*(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 withmain().
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 ofprintf("Hi");.
-
Misspelling
stdio.h: Using<studio.h>(very common beginner typo).
-
Case Sensitivity:
Main()orPrintf()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
mainfunction at the bottom of the file (or declare functions at the top).
11. Exercises
- 1. Write a C program that prints your name, age, and country on three separate lines.
- 2. Intentionally remove a semicolon from your program and try compiling it. Read the error message.
- 3. Write a program using multi-line comments at the top containing your name and the date.
12. MCQ Quiz with Answers
What is the entry point of a C program?
Which symbol marks the end of a statement in C?
What does #include <stdio.h> do?
Which of these is a correct single-line comment?
What does return 0; indicate in main()?
What escape sequence represents a new line?
What happens if you type <studio.h> instead of <stdio.h>?
The { and } symbols are used to define a:
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 onemain() 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.