Core C Programming Concepts
C Program Compilation Process
Compilation Stages
The compilation of a C program involves four main phases:
- Preprocessing: Expands macros, includes header files, processes conditional compilation directives, and removes comments without syntax checking
- Compilation: Performs syntax validation and translates preprocessed code into assembly language
- Assembly: Converts assembly code into machine-readable object files
- Linking: Combines object files with required libraries to produce the final executable
GCC Compilation Example
Step-by-step compilation using GCC:
Preprocessing: gcc -E program.c -o program.i
Compilation: gcc -S program.i -o program.s
Assembly: gcc -c program.s -o program.o
Linking: gcc program.o -o program
| Option | Purpose |
|---|---|
| -E | Preprocessing only |
| -S | Preprocessing and compilation |
| -c | Preprocessing, compilation, and assembly |
| -o <filename> | Specifies output filename |
Simplified compilation:
gcc program.c -o program
Assembly Language Operations
| Operator | Function |
|---|---|
| mov | Move data |
| add | Addition operation |
| push | Push to stack |
| pop | Pop from stack |
| call | Function call |
eax represents a 32-bit register.
C Language Keywords
Data Type Keywords (12)
- unsigned, signed
- short, int, long, float, double, char
- struct, union, enum
- void
Storage Class Keywords (5)
- auto, static, register, extern, const
Control Flow Keywords (12)
- if, else, switch, case, default
- for, while, do
- break, continue
- goto, return
Other Keywords (3)
- sizeof
- typedef
- volatile
Data Types
- Basic Types:
- Integer: short, int, long
- Character: char
- Floating-point: float, double
- Composite Types:
- Array types
- Structure: struct
- Union: union
- Enumeration: enum
- Pointer Types
Constants
Two methods for defining constants:
Macro Definition
#define IDENTIFIER replacement_list
Macros are expanded during preprocessing and don't occupy memory.
Const Variables
const data_type variable_name;
Const variables maintain their values throughout program execution and cannot be reassigned.
Const vs Define Comparison
- Define directives are processed during preprocessing, while const variables are handled during compilation
- Macros don't occupy memory, whereas const variables have allocated storage
- Const variables support type safety checking
Use const variables instead of macros when possible for better type safety.
Pointer Types
- Wild Pointers: Point to undefined memory locations
- Null Pointers: Point to memory address 0
- Void Pointers: Can hold addresses of any data type
Pointers and Strings
char text[] = "sample text"; // Stack-allocated string
char* text_ptr = "sample text"; // Data section constant string
// text[2] = 'x'; // Allowed
// text_ptr[2] = 'x'; // Error - attempt to modify constant
Variable Scope and Lifetime
| Variable Type | Scope | Lifetime | Storage |
|---|---|---|---|
| Local Variable | Function scope | Function duration | Stack |
| Global Variable | Project scope | Program duration | Data section |
| Static Local | Function scope | Program duration | Data section |
| Static Global | File scope | Program duration | Data section |
Function Types
- Global functions are accessible throughout the program
- Static functions are restricted to their defining file
Memory Segments
- Code Section: Contains executable instructions (read-only)
- Data Section:
- Uninitialized data (BSS)
- Initialized data
- Constant data
- Stack: Stores local variables, function parameters, and return addresses (typically 1MB, expandable)
- Heap: Dynamic memory allocation using malloc/free
Process Memory Layout
High Address ↓
- Stack (grows downward)
- Heap (grows upward)
- Uninitialized global data (BSS)
- Initialized global/static data
- Executable code
Low Address ↑
Common Memory Issues
- Array boundary violations
- Incorrect heap allocation sizes
- Multiple free operations on the same memory
- Lost heap pointers
Union Types
- Unions occupy memory sufficient for their largest member
- Only one union member is active at any time
File Pointer Structure
typedef struct {
short buffer_level;
unsigned status_flags;
char file_descriptor;
unsigned char hold_char;
short buffer_size;
unsigned char* data_buffer;
unsigned current_position;
unsigned temp_indicator;
short validation_token;
} FILE;