Chapter 02Beginner

Basic Syntax in C

Learn the fundamental building blocks of C programs. Understand the main() function, how to write comments, and why proper formatting makes code readable.

12 min readUpdated 2024-12-16
syntaxmain functioncommentsindentationprogram structuresemicolon

What You Will Learn

  • Understand the structure of every C program
  • Know why main() is special and what return 0 means
  • Write comments to explain your code
  • Use proper indentation and formatting

01Introduction

Welcome back! 🎓 Now that you've written your first "Hello, World!" program, it's time to understand why that code worked. In this tutorial, we'll take a deep dive into the fundamental rules and structure of C programming.

🤔 What is Syntax?

Syntax is like the grammar of a programming language. Just as English has rules about sentence structure, C has rules about how code must be written. If you break these rules, the compiler won't understand your code and will show an error.

✓ Correct Syntax

printf("Hello");

✗ Wrong Syntax

printf("Hello")

Missing semicolon!

In this tutorial, you'll learn:

  • The anatomy of a C program — what parts make up a complete program
  • The special role of the main() function — where every program begins
  • How to write comments — notes for yourself and other programmers
  • Proper indentation — making your code readable and professional

💡 Why Syntax Matters

C is a strict language — a single misplaced character (like a missing semicolon or wrong bracket) can cause errors. The good news? Once you understand the rules, they become second nature. Understanding proper syntax from the start will save you hours of debugging later!

02Prerequisites

03Structure of a C Program

Let's start with something you already know — the Hello World program from the previous tutorial. We'll use this simple example to understand how every C program is built:

hello.c
C
1#include <stdio.h>
2
3int main() {
4 printf("Hello, World!\n");
5 return 0;
6}

This tiny program has all the essential parts of any C program. Let's break it down into three simple sections:

1

The #include Line

Tells C what tools you need

#include <stdio.h>

What it means: "I want to use the Standard Input/Output tools."

Think of it like this: Before you can cook, you need to get your pots and pans from the cupboard. #include gets the tools (like printf) from C's "cupboard" (the standard library).

2

The main() Function

Where your program starts

int main() {

What it means: "Here's where the program begins running."

Think of it like this: The main() function is like the front door of your house. When someone runs your program, they always enter through main().

Breaking it down:
int = the function will return a number
main = special name C looks for
() = no inputs needed
{ = start of the function body

3

The Statements Inside

The actual instructions

printf("Hello, World!\n"); return 0;

What it means: "Print this text, then tell the computer we finished successfully."

Important rules:
✓ Every statement ends with a semicolon ;
✓ Statements run from top to bottom, one at a time
return 0; means "finished with no errors"

📊 The Simplest C Program Structure

#include <stdio.h>← Get your tools
int main() {← Start here
printf("...");← Do stuff
return 0;← Finish up
}← End of main

💡 Key Takeaway

Every C program you write will follow this same basic pattern: include tools at the top, then put your code inside main(). That's really all there is to it!

04The main() Function

The main() function is the heart of every C program. Think of it as the "start" button — when you run your program, the computer looks for main()and begins executing from there.

🎯 Three Rules About main()

Every C program must have exactly one main() function

Execution always starts from the first line inside main()

The name must be exactly main — lowercase, no variations

The Standard main() Template

Here's the template you'll use for most programs. You can copy this every time you start a new program:

template.c
C
1#include <stdio.h>
2
3int main() {
4 // Your code goes here
5
6 return 0; // Tell the system: "Success!"
7}

What Does "return 0" Mean?

At the end of main(), we write return 0;. This is like giving a thumbs up 👍 to the operating system, saying "Everything worked fine!"

👍
return 0;

"Program finished successfully!"

👎
return 1;

"Something went wrong!"

Pro Tip

Use EXIT_SUCCESS and EXIT_FAILURE from <stdlib.h> for portable code that works across different systems.

05Comments and Indentation

Comments and proper indentation make your code readable and maintainable. They don't affect how your program runs, but they're essential for you and others to understand the code.

Types of Comments

Single-Line Comments

single_line.c
C
// This is a single-line comment
int x = 10; // Comment at end of line
// Use for brief explanations
// Can span multiple lines like this

Use // — everything after is ignored

Multi-Line Comments

multi_line.c
C
/* This is a multi-line comment.
It can span across
multiple lines easily. */
/* Also works on one line */

Use /* */ — everything between is ignored

Comment Best Practices

comment_practices.c
C
1/*
2 * Program: calculator.c
3 * Author: Your Name
4 * Date: December 2024
5 * Description: A simple calculator program
6 */
7
8#include <stdio.h>
9
10// Calculate the sum of two numbers
11// Parameters: a, b - numbers to add
12// Returns: sum of a and b
13int add(int a, int b) {
14 return a + b;
15}
16
17int main() {
18 int num1 = 10;
19 int num2 = 20;
20
21 // Calculate and display the result
22 int result = add(num1, num2);
23 printf("Sum: %d\n", result);
24
25 // TODO: Add subtraction function
26 // FIXME: Handle negative numbers
27
28 return 0;
29}

✓ Good Comments

  • • Explain why, not what
  • • Document function purpose
  • • Mark TODO/FIXME items
  • • Add file headers

✗ Bad Comments

  • i++; // increment i
  • • Stating the obvious
  • • Outdated comments
  • • Commented-out code

Indentation and Formatting

Proper indentation shows the structure of your code visually. While C ignores whitespace, humans need it to read code quickly.

✗ Bad Formatting

bad_format.c
C
#include <stdio.h>
int main(){int x=10;
if(x>5){printf("Big");
}else{printf("Small");}
return 0;}

✓ Good Formatting

good_format.c
C
#include <stdio.h>
int main() {
int x = 10;
if (x > 5) {
printf("Big");
} else {
printf("Small");
}
return 0;
}

Indentation Guidelines

  • Use 4 spaces or 1 tab per indentation level (be consistent!)
  • Indent code inside { } braces
  • Add blank lines between logical sections
  • Put spaces around operators: x = a + b; not x=a+b;
  • Keep lines under 80-100 characters

Warning: Nested Comments

Multi-line comments /* */ cannot be nested! The first */ ends the comment, causing errors.

06Summary

What You Learned:

  • Program Structure: Preprocessor directives → Global declarations → main() → Other functions
  • main() Function: Entry point of every C program, returns 0 for success
  • Comments: Use // for single-line, /* */ for multi-line
  • Indentation: Use consistent spacing (4 spaces or 1 tab) inside braces
  • Best Practices: Write comments that explain why, not what

07Next Steps

Now that you understand C's basic syntax and structure, you're ready to learn about Data Types — how to store numbers, characters, and other data in your programs.