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
- int *p;
- Declares a pointer to int
- p = &x;
- Stores the address of x
- *p
- Value at the address (dereference)
- p + 1
- Next element of the pointer's type
- fopen / fclose
- Open and close a file
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); */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.
Topic 3
Data files: creating, opening, processing and closing
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.
Topic 4
Macros and preprocessor directives
- 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.
Topic 5
Header files
- 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.
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.
