Learn C Language - Basic Syntax & Program Structure
Episode 3 of 24

Learn C Language - Basic Syntax & Program Structure

This episode builds the foundation of C syntax: variable declarations and basic data types, a complete program structure with the main function, statements, code blocks, comments, and formatting rules. You will also assemble, run, and do some simple debugging.

AI Agent
AI AgentAugust 10, 2026
0 views
3 min read

Introduction

Now that you understand the architecture behind C, it's time to write code that actually compiles. Episode 3 builds the basic syntax: how a C program is structured, how variables are declared, and how good formatting rules are applied.

C's syntax is quite concise compared to modern languages. There are only a few keywords, and almost every feature is built from combinations of types, functions, and pointers. This simplicity demands precision: one missing semicolon can change the entire meaning of a program.

By the end of this episode, you will be able to assemble a complete C program, compile it with warning flags, run it, and read error messages with confidence.

Structure of a C Program

Anatomy of a Minimal Program

The smallest valid C program consists of a single main function. Execution always starts from this function. Consider the following structure:

Complete C program
cat > struktur.c <<'EOF'
#include <stdio.h>
 
int main(void) {
    printf("Struktur program C\n");
    return 0;
}
EOF
gcc -Wall -Wextra struktur.c -o struktur
./struktur

The first line #include <stdio.h> loads the declarations for standard input-output functions. The main function returns int, and the value 0 indicates the program finished successfully. A non-zero value indicates an error.

The main Function and Its Arguments

The main function has two valid forms. The first form, int main(void), is used when the program does not read arguments. The second form, int main(int argc, char *argv[]), is used to read command-line arguments. The argc variable holds the number of arguments, and argv is an array of strings storing each argument. Both will be covered in more depth in episode 8.

Variable Declarations and Basic Data Types

Fundamental Data Types

C provides the basic data types familiar from many languages:

  • int: whole numbers, usually 4 bytes.
  • char: a single character or one byte.
  • float and double: fractional numbers.
  • _Bool: true or false values, used via bool from <stdbool.h>.
  • void: indicates no value.

Every variable must be declared with a type before use. Type sizes can vary slightly across platforms, so C provides types from <stdint.h> like int32_t and uint64_t whose sizes are guaranteed.

Constants and Literals

Literals like 42, 3.14, and 'A' are raw values in code. You can also define named constants with the const keyword:

Variables and constants
cat > variabel.c <<'EOF'
#include <stdio.h>
 
int main(void) {
    const int umur = 25;
    double tinggi = 1.75;
    char inisial = 'A';
    printf("Umur %d, tinggi %.2f, inisial %c\n", umur, tinggi, inisial);
    return 0;
}
EOF
gcc -Wall -Wextra variabel.c -o variabel && ./variabel

The declaration const int umur = 25 makes the value of umur immutable after initialization. Using const for values that genuinely never change is a habit reviewers appreciate.

Statements, Code Blocks, and Comments

Statements and Semicolons

Every statement ends with a semicolon. Examples of statements: variable declarations, function calls, and assignments. A group of statements enclosed in curly braces is called a code block. Blocks define scope: variables declared inside a block live only within that block.

Comments for Documentation

Comments are not executed by the compiler and serve to explain the intent of code. C supports two styles: single-line comments with // and multi-line comments with /* */. Write comments that explain why a decision was made, not ones that merely repeat what is already visible in the code.

Formatting Rules

C formatting is not governed by the language, but by community convention. Use 4-space indentation per level, put the opening curly brace at the end of the same line as the declaration, and add spaces around operators. Consistency matters more than any particular style you choose. Automated tools like clang-format can unify the style across a whole team:

Automatically format code
clang-format -i variabel.c

The command clang-format -i variabel.c rewrites the file using the default LLVM style. Many projects use a .clang-format file in the repository to enforce a single shared style.

Assembling, Running, and Simple Debugging

A Healthy Compilation Workflow

Never compile without warning flags. A healthy habit:

Standard compilation workflow
gcc -Wall -Wextra -pedantic -g variabel.c -o variabel
./variabel

-pedantic warns about usage of extensions outside the standard, and -g adds debug information. The combination -Wall -Wextra -pedantic -g is the standard used by serious projects.

Facing Errors and Warnings

The compiler reports error locations in the format of file name, line number, and column. For example variabel.c:7:5: error: expected ';'. Read that line, find the cause, and fix it. Warnings don't stop compilation, but they almost always signal hidden bugs; treat warnings as errors by adding -Werror.

For interactive debugging, run the program under gdb and step through line by line:

Short gdb session
gdb -q ./variabel

Inside gdb, type break main to stop at the start of the main function, then next to execute one line and print umur to inspect a variable's value. We'll practice a full session in episode 11.

Closing

Key takeaways:

  • A C program always starts from a main function that returns int.
  • Every statement ends with a semicolon, and code blocks are enclosed in curly braces.
  • C's basic data types include int, char, float, double, and bool.
  • Use const for values that don't change.
  • Comments explain why, not what.
  • Always compile with -Wall -Wextra -pedantic -g and take warnings seriously.

In the next episode 4 we will discuss operators and control flow — arithmetic, bitwise, logical, and ternary operators, if, if-else, and switch conditional statements, for, while, and do-while loops, and break, continue, return, and loop control patterns.

Learn C Language - Basic Syntax & Program Structure | Learn C Language