2024 U1 Q5

Introduction

This question tested the following topics/items:

  • Debugging
  • Programming style for maintainable code: indentation and comments
  • Identifying and correcting errors in C code (for loops, printf format specifiers, string literals)
  • File operations in C: read, append and close

Part a) i)

Debugging is the process of detecting, locating and correcting (removing) errors in a program.

See this note: Debugging

Part a) ii)

Indentation: Code is indented every time a control structure (e.g. a loop or an IF) is entered. This shows which statements belong to each block, making the logic easier to read, follow and modify.

Comments: Comments explain what sections of code are intended to do (and why), making it easier for the programmer or another programmer to understand and update the code later.

See this note: 2019 U1 Q5

Part b)

LineErrorCorrected line
Line 2The loop variable a is never incremented (“a” by itself does nothing), so the loop never endsfor (a = 1; a <= 5; a++)
Line 4%s is the format specifier for a string, but b is an intprintf ("%d", b);
Line 5The newline \n is not enclosed in double quotes (it must be a string)printf ("\n");

Corrected code:

int a, b;
for (a = 1; a <= 5; a++)                //a was not incremented
    for (b = 1; b < a; b = b + 1)
        printf ("%d", b);               //%d is used for integers, not %s
printf ("\n");                          //string must be in double quotes

Note: As in similar past questions, the indentation suggests that the inner loop and the last printf may have been meant to be inside braces { } for the outer loop. The question only asks for the three errors, so this is not required.

See this note: Adding 2 numbers – Outputting the result with printf(), Examples of Errors During Compilation Stages (Lexical, Syntax, Semantic)

Part c) i)

FILE *fp;
int num;
fp = fopen("data.txt", "r");    //open the file for reading
fscanf(fp, "%d", &num);         //read a value from the file

Part c) ii)

fp = fopen("data.txt", "a");    //open the file in append mode
fprintf(fp, "%d\n", num);       //write the value to the end of the file

Part c) iii)

fclose(fp);                     //close the file

Further explanation

A complete program using all three operations:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;          //file pointer
    int num;

    //(i) Read: open the file for reading and read a value from it
    fp = fopen("data.txt", "r");
    if (fp == NULL)
    {
        printf("Error opening file\n");
        return 1;
    }
    fscanf(fp, "%d", &num);
    printf("Value read: %d\n", num);
    fclose(fp);

    //(ii) Append: open the file in append mode and add a value to the end
    fp = fopen("data.txt", "a");
    fprintf(fp, "%d\n", 25);

    //(iii) Close: close the file
    fclose(fp);

    return 0;
}

“r” opens an existing file for reading, “a” opens a file for appending (data is added to the end of the file, and the file is created if it does not exist), and fclose() closes the file so that the data is saved and the file pointer is released.

See this note: Tutorial: Storing and loading data in a plain textfile., Strategy For Storing Records In Plaintext Files


© 2026 Vedesh Kungebeharry. All rights reserved.

Leave a Reply