Structure of a C Program, Identifiers, and Basic Data Types

Every C program, no matter how large, is built from the same basic skeleton. Once you recognize this structure, reading (and writing) any C program becomes far less intimidating. This is Post 2 of the Unit I series — Post 1 covered algorithms and flowcharts.

The Structure of a C Program

#include <stdio.h>      // 1. Preprocessor directives

#define PI 3.14159       // 2. (Optional) Macro definitions

int globalVar = 10;       // 3. (Optional) Global declarations

int square(int);          // 4. (Optional) Function prototypes

int main() {               // 5. main() function — every C program needs exactly one
    int localVar = 5;      // 6. Local variable declarations
    printf("%d\n", square(localVar));   // 7. Statements
    return 0;               // 8. Return statement
}

int square(int n) {          // 9. Function definitions
    return n * n;
}
The one non-negotiable part: Every executable C program must have exactly one function named main(). This is where program execution always begins, no matter how many other functions or files exist.

Breaking Down Each Part

  • Preprocessor directives (lines starting with #) run before compilation. #include <stdio.h> pulls in the standard I/O library so functions like printf/scanf are available.
  • Global declarations — variables declared outside any function, visible throughout the file.
  • Function prototypes — declarations telling the compiler about functions defined later in the file (see the Functions series for a full explanation).
  • main() — the entry point. Execution starts at the first statement inside its { } and ends when it hits return or the closing brace.
  • Statements — the actual instructions (assignments, function calls, control flow).

Tokens: The Smallest Building Blocks

A C program is broken down by the compiler into tokens — the smallest individual units that still carry meaning. There are six kinds:

  1. Keywords — reserved words with fixed meaning: int, if, while, return, void, etc. (32 in standard C). You cannot use these as variable names.
  2. Identifiers — names you create for variables, functions, arrays, etc.
  3. Constants — fixed values like 10, 3.14, 'A'.
  4. Strings — sequences of characters in double quotes, like "Hello".
  5. Operators — symbols like +, -, ==, &&.
  6. Special symbols — { } ( ) ; , and others that structure the code.

Identifiers: Naming Rules

An identifier names a variable, function, array, or other user-defined item. The rules:

  • Must start with a letter (A-Z, a-z) or an underscore _ — never a digit.
  • Can contain letters, digits, and underscores after the first character.
  • Cannot be a reserved keyword.
  • Case-sensitive: total, Total, and TOTAL are three different identifiers.
  • No spaces or special characters (@, -, %, etc.) allowed.
int age;        // valid
int _count;     // valid
int total_1;    // valid
int 1total;     // INVALID — starts with a digit
int my-var;     // INVALID — hyphen not allowed
int int;        // INVALID — 'int' is a reserved keyword
Naming convention: While C only enforces the rules above, using descriptive, lowercase, underscore-separated names (snake_case, e.g. student_marks) makes code dramatically easier to read than terse names like x1, a, temp2 — a habit worth building early.

Basic Data Types

Every variable in C has a type, which tells the compiler how much memory to allocate and how to interpret the bits stored there.

Type Typical Size Used For Format Specifier
int 4 bytes Whole numbers %d
float 4 bytes Decimal numbers %f
double 8 bytes Higher-precision decimals %lf
char 1 byte A single character %c

(Exact byte sizes are compiler/platform-dependent, but the ones above are the standard values on most modern systems.) C also provides modifiers — short, long, signed, unsigned — to adjust range and sign handling, e.g. unsigned int, long double.

Example: Declaring and Printing Each Type

#include <stdio.h>

int main() {
    int age = 20;
    float price = 49.99;
    double pi = 3.14159265358979;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Price: %.2f\n", price);
    printf("Pi: %.10lf\n", pi);
    printf("Grade: %c\n", grade);

    return 0;
}

Sample Output

Age: 20
Price: 49.99
Pi: 3.1415926536
Grade: A
Common mistake: Using the wrong format specifier (e.g. %d for a float) doesn’t cause a compile error in most compilers — it just silently prints garbage, because printf() has no way to know the actual type you passed; it trusts the specifier you gave it. Always double-check that your specifiers match your variable types.

Summary

  • Every C program follows the same skeleton: directives, declarations, main(), statements, function definitions.
  • main() is mandatory and is always where execution begins.
  • A program is made of six kinds of tokens: keywords, identifiers, constants, strings, operators, special symbols.
  • Identifiers must start with a letter/underscore, contain only letters/digits/underscores, and can’t be a keyword.
  • The four basic data types are int, float, double, and char, each with its own size and format specifier.

Further Reading

Next in this series: variables, constants, and input/output statements.

Leave a Comment