About C Programming
C Programming in the second semester picks up where Programming Principles and Algorithms left off. Once you can write loops and functions, this subject teaches the parts of C that make it powerful and, for many students, difficult: arrays, pointers, strings, structures, the preprocessor and files. A good grip on pointers here makes Data Structures in the next semester much easier.
What the units cover
- Arrays: one- and two-dimensional arrays, declaring and initialising them, and common operations such as searching, sorting and matrix addition.
- Pointers: what a pointer stores, the address-of (&) and dereference (*) operators, pointer arithmetic, pointers with arrays and functions, and dynamic memory allocation with malloc, calloc and free.
- Strings: character arrays, reading and printing strings, and library functions such as strlen, strcpy, strcmp and strcat.
- Structures: defining structures, arrays of structures, pointers to structures, passing structures to functions, and unions.
- Introduction to the C preprocessor: #include, #define, macros with arguments, and conditional compilation.
- File handling: opening and closing files, file modes, reading and writing with fprintf, fscanf, fgets and fputs, and text versus binary files.
What the exam looks like
The May 2016 paper on this page had five compulsory 3-mark questions, two out of three 7½-mark questions, and three out of five 15-mark questions, for 75 marks. The short questions were direct, such as initialising a two-dimensional array or explaining how a structure differs from an array. The long questions mixed theory and code, for example writing a function that uses pointers to add two matrices, explaining the difference between functions and parameterised macros, counting occurrences of a word in a text, and describing different ways of updating a data file.
How to study it
Draw memory diagrams. When you learn pointers, sketch boxes for variables and arrows for pointers until expressions such as *p++ and (*m)[5] versus *m[5] make sense. Then write and run small programs for each topic, and practise writing them on paper with clean indentation.
Topics worth practising
- Pointers with arrays, and the difference between (*m)[5] and *m[5]
- Dynamic arrays using malloc
- Structure pointers and passing structures to functions
- Macros versus functions
- File modes and a program that reads and writes a file
- break versus continue
Use the notes, book notes and video lectures here to learn each unit, then solve the previous year papers on this page.
