1 / 15

Session 3: Program Structure & Syntax

Understanding the Anatomy of C Programs

Today we dive deeper into how C programs work and master the language rules

From basic Hello World to understanding every detail

🎯 Today's Learning Goals

🏗️ Understand the complete anatomy of a C program
📚 Master header files and the #include directive
🚪 Learn why main() is the "front door" of every program
📜 Understand C syntax rules and why they matter
✨ Master escape sequences for beautiful output
🎨 Create programs with professional formatting

📚 Quick Review: Your First Program

#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }

You successfully wrote this! Today we'll understand every single character.

🏗️ Anatomy of a C Program

1. Preprocessor Directives

Instructions that run BEFORE compilation

#include <stdio.h>

2. Function Declarations

Tell compiler about functions we'll use

int main();

3. Main Function

The heart - where execution begins

int main() { ... }

4. Statements

Instructions that DO something

printf("Hello!");

📚 Header Files: Your Program's Library

#include <stdio.h>

What is stdio.h?

STanDard Input Output - contains functions for printing and reading

Why Do We Need It?

Without it, printf() is undefined - computer doesn't know what it means!

🔧 Syntax Rules:

• Must be at the TOP of your program

• Use < > for system headers, " " for your own files

• No semicolon needed after #include

🚪 main() - The Front Door

int main() { // Your program starts here! return 0; }

int

Return type - main() gives back an integer to the operating system

main

Special function name - OS looks for this to start your program

()

Parameter list - empty means no input needed

return 0;

Success code - 0 means "everything went fine!"

📜 C Language Syntax Rules

🔹 Semicolon Rule

Every statement MUST end with ;

printf("Hello");

🔹 Case Sensitive

PRINTF ≠ printf ≠ Printf

printfPrintf

🔹 Braces Must Match

Every { needs a closing }

{ ... }

🔹 String Quotes

Use straight quotes, not curly

"Hello""Hello"

💬 Comments: Notes for Humans

// Single line comment - everything after // is ignored /* Multi-line comment You can write multiple lines here Computer completely ignores all of this */ #include <stdio.h> // This loads input/output functions int main() { printf("Hello!"); // Print message to screen return 0; // Program finished successfully }

✅ Good Practice:

Always comment your code! Future you will thank present you.

🖨️ printf() - Your Output Superpower

printf("format string");

printf() Breakdown:

printf - Function name (print formatted)
( ) - Contains the parameters we pass to function
"text" - String literal in double quotes
; - Statement terminator (like period in English)

✨ Escape Sequences: Special Characters

\n

Newline - like pressing Enter

\t

Tab - 8 spaces of indentation

\"

Literal quote mark

\\

Literal backslash

Code:
printf("Line 1\nLine 2\n"); printf("\tIndented text\n"); printf("He said \"Hello!\"\n");
Output:
Line 1
Line 2
        Indented text
He said "Hello!"

🎮 Interactive Challenge!

Fix the Broken Code
#include <stdio.h> int main() { printf("Welcome to my program!\n") printf("Today is a great day to learn C\n"); Return 0; }

Can you spot the 2 errors in this code?

Missing #include
Missing semicolon after first printf
"Return" should be lowercase "return"
Wrong quotes in printf

🚨 Common Beginner Mistakes

❌ Forgetting Semicolons

printf("Hello")
Error: Expected ';' before '}'

❌ Case Sensitivity

Printf("Hello");
Error: 'Printf' undeclared

❌ Unmatched Braces

int main() { printf("Hi"); // Missing }
Error: Expected '}' at end of input

❌ Wrong Quote Types

printf("Hello");
Error: Stray characters

Remember: Errors are learning opportunities!

⭐ Professional Coding Style

✅ Proper Indentation

int main() { printf("Indented properly\n"); return 0; }

✅ Meaningful Comments

// Display welcome message printf("Welcome!\n");

✅ Consistent Spacing

#include <stdio.h> int main() { // Space between functions }

✅ Clear Variable Names

We'll learn this next week!

studentAgex

🎯 Your Practice Mission

Create a Personal Info Program

Write a program that displays:

Your name and age
Your favorite subject in school
What you want to build with programming
Use proper indentation and comments
Use \n and \t for nice formatting
// Example structure: #include <stdio.h> int main() { printf("=== About Me ===\n"); printf("Name: [Your Name]\n"); // Add more lines here! return 0; }

🏠 Homework & What's Next

📝 This Week's Practice:

Complete the "About Me" program
Create 3 different programs with creative output
Practice using \n, \t, \", and \\ in your programs
Try to break your code on purpose, then fix it!
Add meaningful comments to all your programs

🚀 Next Week: Session 4

Variables and Data Types

We'll learn how to store and manipulate information in our programs!

🎉 Excellent work! You're mastering C programming structure!