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;
}
Breaking Down Each Part
- Preprocessor directives (lines starting with
#) run before compilation.#include <stdio.h>pulls in the standard I/O library so functions likeprintf/scanfare 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 hitsreturnor 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:
- Keywords — reserved words with fixed meaning:
int,if,while,return,void, etc. (32 in standard C). You cannot use these as variable names. - Identifiers — names you create for variables, functions, arrays, etc.
- Constants — fixed values like
10,3.14,'A'. - Strings — sequences of characters in double quotes, like
"Hello". - Operators — symbols like
+,-,==,&&. - 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, andTOTALare 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
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
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, andchar, each with its own size and format specifier.
Further Reading
Next in this series: variables, constants, and input/output statements.