C Syntax: Anatomy of a C Program with a Worked Compile

Read a complete C file line by line, trace its integer values, and see how preprocessing, compilation, linking, and execution produce the final output.

KnowledgeGate Team

Exam prep & CS education

Updated 9 Nov 20265 min read
C Syntax: Anatomy of a C Program with a Worked Compile

A short C file can look like punctuation wrapped around main. The file becomes predictable when directives, declarations, a prototype, blocks, statements, expressions, compilation, linking, and running are separated, then its output is traced.

C program anatomy: read the source file from top to bottom

Save this program as anatomy.c:

c
#include <stdio.h>

#define TAX_RATE 18

int add_tax(int price);

int main(void)
{
    int base = 250;
    int tax = base * TAX_RATE / 100;
    int total = add_tax(base);

    printf("Base=%d Tax=%d Total=%d\n", base, tax, total);
    return 0;
}

int add_tax(int price)
{
    return price + price * TAX_RATE / 100;
}

Read it in five regions. First, #include <stdio.h> supplies the declaration of printf, not its executable body. Second, #define TAX_RATE 18 creates an object-like macro with the replacement list 18. Third, int add_tax(int price); declares the helper before its call.

Fourth, main is the entry function in a hosted C implementation. Fifth, the last block defines add_tax. The prototype ends with ; and each function definition has a compound statement in braces.Coding & DSA Courses for Placements category lets you compare programming tracks.

Five labeled regions of a C file, with the corrected prototype and printf newline shown exactly.

C syntax building blocks: tokens, identifiers and punctuators

Tokenise int tax = base * TAX_RATE / 100;. int is a keyword. Before preprocessing, tax, base, and TAX_RATE are identifiers. The symbols =, *, and / are operators, 100 is an integer constant, and ; ends the declaration. Preprocessing replaces TAX_RATE with 18 before parsing.

In the printf line, printf is an identifier, parentheses enclose arguments, and commas separate them. The string literal contains three %d conversions and \n, a newline character. They are not C operators.

An identifier begins with a letter or underscore. Later characters may also be digits, but a keyword cannot be used as an identifier. Names are case-sensitive, so tax, Tax, and TAX_RATE are different. Whitespace and comments normally separate tokens, while spaces inside the string literal are printed data.

C declarations, scope and function signatures

#define TAX_RATE 18 is a preprocessor macro. The prototype int add_tax(int price); tells the compiler that the helper takes one int and returns one int. The later definition, int add_tax(int price) { ... }, supplies the executable code that will be linked into the program.

base, tax, and total are block-scope objects inside main. price is local to each call of add_tax. None is available in the other function. TAX_RATE is not a block-scope int; its macro remains active to the file's end unless undefined.

int main(void) returns an int and declares no parameters. Its return 0; reports successful completion. The helper's return statement sends the computed total back to its caller.

C expressions and integer evaluation: trace every worked value

After macro replacement, the tax expression is base * 18 / 100. Multiplication and division have equal precedence and associate from left to right. With base = 250, the evaluation is:

Code
(250 * 18) / 100
= 4500 / 100
= 45

Using integer arithmetic, add_tax(base) passes 250, so price = 250 inside the helper. There, price * 18 / 100 = 45, and the return expression gives 250 + 45 = 295. main stores 295 in total.

The three %d conversions consume base, tax, and total in that order. The exact output is:

Code
Base=250 Tax=45 Total=295
Numbered flow diagram traces TAX_RATE replacement, base 250, tax 45, add_tax result 295, printf arguments, output, and return 0.

Compile and run a C program: source, object code and executable

With GCC installed, compile and run it like this:

bash
gcc -std=c17 -Wall -Wextra -pedantic anatomy.c -o anatomy
./anatomy

It prints Base=250 Tax=45 Total=295. Executable names and run syntax can differ by operating system, but the computed values do not.

Stage

What happens

Preprocessing

Handles #include and expands both uses of TAX_RATE to 18.

Compilation

Parses and type-checks the resulting translation unit, then produces object code.

Linking

Combines the object code with the library implementation needed for printf.

Loading and runtime

Loads the executable, begins execution through the C runtime, and calls main.

The compiler does not execute the program, and the operating system does not necessarily call main directly. Each stage has a distinct job.

C syntax errors: compiler, linker and runtime traps

Small changes fail at different stages:

Change

Where it fails

Why

Fix

Remove ; after int base = 250

Compiler parse diagnostic

The declaration lacks its terminator.

Restore ;.

Write TAX_Rate in the tax expression

Compiler diagnostic

The identifier is undeclared because case matters.

Use TAX_RATE.

Remove the prototype but leave the helper below main

C17 compiler diagnostic

The call has no prior declaration.

Restore the prototype or move the full definition above main.

Keep the prototype but delete the helper definition

Linker error

add_tax cannot be resolved.

Supply the definition.

Another trap is keeping three %d conversions but passing only base, tax. -Wall should warn about the mismatch. If executed, the missing argument causes undefined behaviour. Pass three matching int arguments in conversion order.

Compile after small changes. Read the first diagnostic before its cascade, then inspect its line and the previous one. Keep warnings enabled. Indentation helps readers; tools interpret braces, semicolons, token boundaries, strings, names, and case.

C syntax questions in exams and interviews

Durable questions ask you to predict output, find an invalid token or missing terminator, distinguish declarations from definitions, trace integer arithmetic, explain macro replacement, or separate compiler and linker failures. C Programming for GATE: Tested Areas and Prep Order places these skills in the broader subject.

Try one checkpoint before reading the next sentence: change only base from 250 to 251 and predict the output. The answer is tax = (251 * 18) / 100 = 4518 / 100 = 45 because integer division drops the fractional part. Then add_tax(251) returns 251 + 45 = 296, so the output becomes Base=251 Tax=45 Total=296.

For focused output and concept practice, continue with C Language MCQs: 12 Solved Output and Concept Questions.

C program anatomy: the short version and next step

Keep five points clear:

  1. Directives are handled before C compilation.

  2. Declarations introduce names and types.

  3. Function bodies are brace-delimited compound statements.

  4. Semicolons terminate this example's declarations and simple statements.

  5. A successful build still requires value tracing to predict the output.

Retype the program, compile with warnings, predict its output, then run it. Change base to 251 and check the truncation. For a full sequence of concepts and practice, use the C Language Course: Concepts, MCQs and Coding. Once you can explain this file line by line, move to Arrays and Pointers in C: Memory, Arithmetic, Traps.

Your mastery test is concrete: identify every token category in the tax declaration, explain every semicolon and brace, and derive 45 and 295 without running the code.