Unit 4: Pointers, files and preprocessor directives
Programming in C notes · PTU syllabus (BSIT102/BSBC102)
On this page
Unit summary
Pointers give C its power over memory, files make data permanent, and the preprocessor shapes code before compilation. This unit covers the address operator and pointers, declaring and initialising pointers, assignment through pointers, pointers and arrays, creating, opening, closing and processing data files, macros, conditional preprocessor directives and header files.
After this unit you can
- Declare, initialise and use pointers
- Use pointers with arrays
- Create, open, process and close data files
- Use macros, conditional directives and header files
PTU syllabus topics
- Address operator and pointers
- declaring and initializing pointers
- assignment through pointers
- pointers and arrays
- creating/opening/closing/processing a data file
- macros
- conditional preprocessors
- header files
Topic 1
Pointers: address operator, declaration and initialisation
A pointer is a variable that stores the address of another variable.
int x = 10; int *p = &x;—&xis the address of x;pholds it.*p(dereferencing) gives the value at that address, so*pis 10, and*p = 20;changes x to 20.- Pointers allow call by reference, dynamic memory allocation and efficient array handling.
cvoid swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
/* called as swap(&x, &y); */Example
Value and address: int x = 10; int *p = &x; — printf("%d", *p); prints 10, and *p = 25; changes x to 25. printf("%p", p); prints the address of x.
Exam tip
& gives the address of a variable; * gives the value stored at an address.
Topic 2
Pointers and arrays
- The name of an array is the address of its first element:
ais the same as&a[0], and*(a + i)isa[i]. - Pointer arithmetic:
p + 1moves to the next element (by the size of the data type). - Pointer to a structure uses the arrow operator:
struct student *ptr = &s1; ptr->marksis the same as(*ptr).marks.
Example
Traversing with a pointer: int a[] = {5, 10, 15}; int *p = a; — *(p + 2) is 15, and the loop for (i = 0; i < 3; i++) printf("%d ", *(p + i)); prints 5 10 15.
Exam tip
p + 1 moves to the next element, not the next byte — the step equals the size of the data type.
Topic 3
Data files: creating, opening, processing and closing
File modes include "r" (read), "w" (write — creates or overwrites), "a" (append) and their update versions "r+", "w+" and "a+". Common functions are fopen, fclose, fprintf, fscanf, fgetc, fputc, fgets and fputs. Data in variables is lost when the program ends; files store it permanently.
- 1Declare a file pointer
FILE *fp;
- 2Open the file
fp = fopen("emp.txt", "w");
- 3Read or write
fprintf, fscanf, fputc, fgetc
- 4Close the file
fclose(fp);
| Mode | Meaning |
|---|---|
| "r" | Read; file must exist |
| "w" | Write; creates a new file or overwrites |
| "a" | Append; adds to the end |
| "r+" | Read and write |
Exam tip
Always check whether fopen returned NULL (file not found or not openable) before reading.
Example
Writing and reading a file: FILE *fp = fopen("marks.txt", "w"); fprintf(fp, "%d", 90); fclose(fp); writes 90 to the file. Reopening it with "r" and fscanf(fp, "%d", &m); reads 90 back into m.
Exam tip
Always check whether fopen returned NULL before using the file, and close every file you open.
Topic 4
Macros and preprocessor directives
A macro defined with #define is replaced by the preprocessor before compilation. Macros with arguments must use brackets to avoid errors.
- Preprocessor: processes lines beginning with # before compilation.
c#define PI 3.14159 /* object-like macro */
#define SQUARE(x) ((x) * (x)) /* function-like macro: parenthesise! */
#define DEBUG 1
#if DEBUG
printf("Debug mode\n");
#else
printf("Release mode\n");
#endif
#ifndef MYLIB_H /* include guard in a header file */
#define MYLIB_H
int add(int a, int b);
#endif- Conditional directives: #if, #ifdef, #ifndef, #elif, #else, #endif — compile code selectively (debug builds, platform-specific code).
- Other directives: #undef, #include, #pragma, #error.
Example
Why brackets matter: with #define SQUARE(x) x * x, SQUARE(2 + 3) expands to 2 + 3 * 2 + 3 = 11, not 25. Writing #define SQUARE(x) ((x) * (x)) gives the correct 25.
Exam tip
Types of directives: file inclusion (#include), macro substitution (#define) and conditional compilation (#if, #ifdef).
Topic 5
Header files
Header files contain function declarations and macros. Standard headers come with the compiler and are included with angle brackets; user-defined headers are included with quotes and are searched in the current folder first.
- Standard headers: stdio.h (I/O), stdlib.h (memory, conversions), string.h (strings), math.h (maths), ctype.h (character tests), time.h.
- User-defined headers: declare functions and macros shared across files; include with quotes — #include "mylib.h"; protect with include guards.
Example
Including headers: #include <math.h> lets a program use sqrt(49), which returns 7.0. A programmer's own file area.h containing #define PI 3.14159 is included with #include "area.h".
Exam tip
< > for standard headers, " " for your own headers.
Key terms
- Pointer
- Variable storing an address
- Dereferencing
- Accessing the value at an address using *
- File pointer
- FILE * used to access a file
- Macro
- Name replaced by text during preprocessing
- Include guard
- Directives preventing double inclusion of a header
Quick revision
- & and *; declaration, initialisation, NULL; pointer arithmetic.
- Array name as pointer; a[i] = *(a + i); pointers to structures (->).
- fopen modes, fprintf, fscanf, fgetc, fputc, fclose; text and binary files.
- #define macros, function-like macros, conditional compilation.
- Standard and user headers; include guards.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.What does the & operator return?
- Q2.How is a[i] expressed using pointers?
- Q3.Name four file opening modes.
- Q4.What is the difference between a macro and a function?
- Q5.What is conditional compilation?
- Q6.What is an include guard?
Long-answer questions
- Q1.Explain pointers in C with examples.
- Q2.Explain the relationship between pointers and arrays.
- Q3.Explain file handling in C with a program.
- Q4.Explain macros, conditional preprocessor directives and header files.
Stuck on this unit?
Message SBS on WhatsApp for help with Programming in C, or to ask about studying B.Sc IT at Synetic.
