CSC115 · TU past paper
C Programming 2074 question paper
The complete TU 2074 exam paper for C Programming (CSC115), all 12 questions with solved model answers written to the mark scheme.
Tap a question to open its answer.
- 110 marksStructure of C programHideAnswer
Discuss structure of a C program with suitable example.[10]
Structure of a C Program
Introduction
Every C program follows a standard, well-defined structure. Understanding this structure is essential because the compiler processes each section in a specific order. A well-structured C program is easier to read, debug, and maintain.
Sections of a C Program
A C program is typically divided into the following sections:
1. Documentation Section
- Contains comments that describe the purpose of the program, author name, date, etc.
- Comments are ignored by the compiler.
- Single-line comment:
// comment - Multi-line comment:
/* comment */
/* Program to add two numbers Author: Ram Date: 2081 */
2. Preprocessor Directives
- Lines that begin with
#are processed before compilation. #includeis used to include header files (libraries).#defineis used to define constants or macros.- These directives make library functions like
printf()andscanf()available to the program.
#include <stdio.h> #define PI 3.14
3. Global Declarations
- Variables or functions declared outside all functions are called global.
- They are accessible throughout the entire program.
int count = 0; /* global variable */
4.
main()Function- The
main()function is the entry point of every C program. - Execution always begins from
main(). - It returns an integer value to the operating system.
return 0signals that the program ended successfully.- Curly braces
{ }define the body of the function. - Every statement inside must end with a semicolon (
;).
int main() { /* statements */ return 0; }
5. User-Defined Functions
- Functions written by the programmer to perform specific tasks.
- They are defined outside
main()but can be called frommain()or other functions. - They help in making the program modular and reusable.
int add(int x, int y) { return x + y; }
Complete Example
The following example demonstrates all sections of a C program:
/* Program to add two numbers and display the result */ /* 1. Documentation */ #include <stdio.h> /* 2. Preprocessor directive */ int result; /* 3. Global declaration */ int add(int x, int y); /* Function prototype */ int main() /* 4. main() function */ { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); result = add(a, b); printf("Sum = %d\n", result); return 0; /* Signals successful end */ } int add(int x, int y) /* 5. User-defined function */ { return x + y; }Sample Output
Enter two numbers: 5 3 Sum = 8
Explanation of Key Elements
Element Purpose #include <stdio.h>Includes standard I/O library for printf()andscanf()int main()Starting point of the program; returns integer to OS { }Curly braces define the body of a function return 0Signals the program ended successfully ;Every statement must end with a semicolon scanf()Reads input from the user printf()Displays output to the screen
How a C Program Runs
Once the source code is written following the above structure, it goes through the following stages before producing output:
source.c --> Compiler --> source.obj --> Linker --> source.exe --> Output- Compilation: The compiler translates source code into object code and catches syntax errors.
- Linking: The linker combines object code with library functions (like
printf,scanf) to produce an executable file. - Loading: The loader places the executable into memory.
- Execution: The CPU runs the program and produces output.
Summary
A well-structured C program contains five main sections: documentation, preprocessor directives, global declarations, the
main()function, and user-defined functions. Following this structure makes programs organized, readable, and easy to maintain. Themain()function is mandatory in every C program as it serves as the entry point for execution. - 210 marksConditional StatementsHideAnswer
Discuss different types of if statements with suitable example of each.Different if statement with switch statement.[8+2]
(a) Types of if Statements Selection statements (if, if...else, switch) are a category of statements in C that control the flow of execution based on conditions. --- Syntax: Description: If the condition evaluates to true (non-zero), the...
- 310 marksIntroductionHideAnswer
What is the structure? How is it different from array? Create a structure student having data members name, roll number and percentage.Complete the program to display the name of student having percentage greater than or equal to 60.[5+5]
--- (a) What is a Structure? A structure is a user-defined data type in C that allows grouping of variables of different data types under a single name. Each variable inside a structure is called a member or data member. Feature Array St...
- 45 marksAlgorithms and FlowchartHideAnswer
What is algorithm? How is it different from flow chart? [2+3]
Algorithm and Flowchart
What is an Algorithm? [2 marks]
An algorithm is a finite, step-by-step set of instructions to solve a problem. It must be clear, unambiguous, and always terminate.
Properties of a Good Algorithm:
Property Description Finiteness Must end after a finite number of steps Definiteness Each step must be clear and unambiguous Input Has zero or more inputs Output Produces at least one output Effectiveness Each step must be simple enough to be carried out Example: Algorithm to find the largest of two numbers
Step 1: Start. Step 2: Read a and b. Step 3: If a > b, print a. Else print b. Step 4: Stop.
Difference Between Algorithm and Flowchart [3 marks]
A flowchart is a diagram that represents an algorithm using standard symbols connected by arrows. It shows the flow of control from start to end.
Basis Algorithm Flowchart Nature Written in plain text or pseudocode as numbered steps Represented as a diagram using standard symbols and arrows Form Textual / verbal description Visual / graphical representation Symbols No special symbols required Uses standard symbols (oval, rectangle, diamond, parallelogram, etc.) Ease of Understanding Easier to write but may be harder to follow for complex logic Easier to understand visually, especially for branching and loops Modification Easy to modify by editing text Harder to modify as the diagram must be redrawn Purpose Describes the logic of a solution in steps Depicts the flow of control from start to end graphically Key Relationship
An algorithm and a flowchart both serve the same purpose: to plan and represent the solution to a problem before coding. The algorithm is the textual form, while the flowchart is its graphical equivalent. A flowchart is essentially a visual translation of an algorithm.
- 55 marksData typesHideAnswer
What is type conversion? Discuss type casting with suitable example. [5]
Type conversion is the process of converting a value from one data type to another data type. In C, since different variables have different data types (int, float, char, double, etc.), it is sometimes necessary to convert a value of one...
- 65 marksDecision Making and LoopingHideAnswer
Write a program that computes the sum of digits of a given integer number. [5]
To find the sum of digits of an integer, we repeatedly extract the last digit using the modulus operator (% 10), add it to a sum variable, and then remove the last digit using integer division (/ 10). This continues until the number beco...
- 75 marksUser defined functionsHideAnswer
What is function? Discuss the benefits of using function. [1.5+3.5]
Function in C Programming
What is a Function? [1.5 marks]
A function is a self-contained, named block of code that performs a specific task. It is written once and can be called (invoked) multiple times from anywhere in the program.
General Syntax:
return_type function_name(parameter_list) { // body of the function return value; }Example:
int add(int a, int b) { return a + b; }A C program must have at least one function:
int main(), which is the starting point of every C program.
Benefits of Using Functions [3.5 marks]
1. Reusability
A function is written once and can be called multiple times from different parts of the program. This avoids writing the same code repeatedly.
int square(int n) { return n * n; } // Can be called as: square(3), square(5), square(10) ...2. Modularity / Divide and Conquer
A large, complex program can be broken down into smaller, manageable, and independent modules (functions). Each function handles one specific task, making the overall program easier to design and understand.
3. Readability and Clarity
Programs written using functions are easier to read and understand. The
main()function becomes shorter and cleaner because the detailed logic is moved into separate named functions.4. Easy Debugging and Testing
Since each function performs a specific task, errors can be located and fixed more easily. Functions can be unit tested individually (tested separately) before being combined into the full program.
5. Reduced Program Length (Avoids Code Duplication)
Without functions, the same logic would need to be written multiple times. Functions reduce the total size of the program by replacing repeated code with a single function call.
6. Easy Maintenance
If a change is needed in a particular task, only the corresponding function needs to be modified. There is no need to search and change the same logic at multiple places throughout the program.
7. Abstraction
The user of a function does not need to know how it works internally. They only need to know what input to provide and what output to expect. This hides complexity and promotes abstraction.
Summary Table
Benefit Description Reusability Write once, use many times Modularity Breaks program into smaller parts Readability Makes code cleaner and easier to follow Easy Debugging Errors are isolated within individual functions Reduced Code Eliminates repetition Easy Maintenance Changes are made in one place only Abstraction Hides internal complexity from the caller - 85 marksIntroduction to ArrayHideAnswer
Write a program to find sum and average of 10 integer numbers stored in an array. [5]
Arrays use a loop variable as the subscript (starting from 0) to process elements. A one-dimensional array of size 10 has indices from arr[0] to arr[9]. Step Description ------------------- int arr[10] Declares a one-dimensional array of...
- 95 marksPointers and ArraysHideAnswer
Define pointer. Discuss the relationship between pointer and one-dimensional array. [1.5+3.5]
--- A pointer is a variable that stores the memory address of another variable. Instead of holding a data value directly, a pointer holds the address where that data is stored in memory. Syntax: Example: Here, ptr is a pointer to an inte...
- 105 marksInput Output Operations in FileHideAnswer
Write a program to read and print data stored in a file input.txt. [5]
- A file is a collection of related data placed on disk. - fopen() is used to open a file with a specified mode. - Mode "r" opens an existing file for reading only; previous data is not erased. - fgetc() reads a single character from a f...
- 115 marksGraphics FunctionHideAnswer
Why do we need graphics functions? Write a program to draw a circle. [2+3]
--- (a) Why Do We Need Graphics Functions? Graphics functions are needed for the following reasons: 1. Visual Representation: Graphics functions allow us to draw shapes, lines, circles, and other visual elements on the screen, making out...
- 125 marksIncrement or Decrement operatorHideAnswer
Discuss increment and decrement operator with example. [5]
C has two useful operators: increment (++) and decrement (--). These are unary operators because they operate on only a single operand. - The increment operator (++) increments the value of a variable by 1. - The decrement operator (--) ...