Unit 4 of 4 · B.Sc IT Sem 1

Unit 4: Pointers, files and preprocessor directives

Programming in C notes · PTU syllabus (BSIT102/BSBC102)

3 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. Pointers: address operator, declaration and initialisation
  3. Pointers and arrays
  4. Data files: creating, opening, processing and closing
  5. Macros and preprocessor directives
  6. Header files
  7. Key terms
  8. Quick revision
  9. Important questions

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
Key termsPointer essentials
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
1

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; — &x is the address of x; p holds it.
  • *p (dereferencing) gives the value at that address, so *p is 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); */
2

Topic 2

Pointers and arrays

  • The name of an array is the address of its first element: a is the same as &a[0], and *(a + i) is a[i].
  • Pointer arithmetic: p + 1 moves to the next element (by the size of the data type).
  • Pointer to a structure uses the arrow operator: struct student *ptr = &s1; ptr->marks is the same as (*ptr).marks.
3

Topic 3

Data files: creating, opening, processing and closing

Data in variables is lost when the program ends; files store it permanently.

ProcessWorking with a file in C
  1. 1Declare a file pointer

    FILE *fp;

  2. 2Open the file

    fp = fopen("emp.txt", "w");

  3. 3Read or write

    fprintf, fscanf, fputc, fgetc

  4. 4Close the file

    fclose(fp);

ModeMeaning
"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.

4

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.
5

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

  1. Q1.What does the & operator return?
  2. Q2.How is a[i] expressed using pointers?
  3. Q3.Name four file opening modes.
  4. Q4.What is the difference between a macro and a function?
  5. Q5.What is conditional compilation?
  6. Q6.What is an include guard?

Long-answer questions

  1. Q1.Explain pointers in C with examples.
  2. Q2.Explain the relationship between pointers and arrays.
  3. Q3.Explain file handling in C with a program.
  4. 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.

WhatsApp us