CS3251 - Programming in C
Anna University - April/May 2025
Answer Key (Regulation 2021)
---
PART A - (10 × 2 = 20 Marks)
1. Output of the program:
#include <stdio.h>
int main() {
printf("Hello World! %d
", x);
return 0;
Answer: Compilation error: variable 'x' is undeclared.
---
2. Output of the program:
#include <stdio.h>
int main() {
int j = 1;
while(j >= 255) {
printf("%c %d
", j, j);
j++;
}
return 0;
Answer: No output. Condition `j >= 255` is false initially.
---
3. Remove spaces from start and end of a string:
void trim(char *str) {
char *end;
while(isspace((unsigned char)*str)) str++;
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
*(end+1) = '';
---
4. Length of string without using built-in function:
char f[] = "Programming in C";
int len = 0;
while(f[len] != '') len++;
---
5. Constant pointer vs Pointer to constant:
- int *const ptr; // Constant pointer (address can't change)
- const int *ptr; // Pointer to constant (value can't change)
---
6. Array of pointers:
char *arr[] = {"C", "Java", "Python"};
Useful in handling array of strings.
---
7. Structure vs Union:
- Structure: All members get separate memory.
- Union: All members share same memory location.
---
8. Syntax of typedef:
typedef unsigned int u_int;
Creates a new type name.
---
9. Modes of file opening:
- "r": read
- "w": write
- "a": append
- "r+", "w+", "a+": read/write modes
---
10. Use of fseek():
- Used to move the file pointer to a specific location in a file.
fseek(fp, 0, SEEK_SET);
---
PART B - (5 × 16 = 80 Marks)
11. (a)(i) C Program to reverse digits of a number:
int num, rev = 0;
scanf("%d", &num);
while(num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
printf("Reversed = %d", rev);
(a)(ii) Data types in C:
- Basic: int, char, float, double
- Derived: arrays, pointers
- User-defined: struct, union, enum
---
12. (a)(i) Find max among set of numbers:
int a[10], max = a[0];
for(int i = 1; i < n; i++) {
if(a[i] > max)
max = a[i];
(a)(ii) Matrix multiplication:
- Nested loops to multiply A[i][k] * B[k][j]
- Result stored in third matrix
(b)(i) Selection Sort:
for(i = 0; i < n-1; i++) {
min = i;
for(j = i+1; j < n; j++) {
if(a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
(b)(ii) Compare and concatenate two strings manually:
char s1[100] = "Computer", s2[] = "Fundamentals";
// Concatenate
int i = strlen(s1), j = 0;
while(s2[j] != '') s1[i++] = s2[j++];
s1[i] = '';
---
13. (a)(i) Built-in math functions:
- Include <math.h>
- sqrt(), pow(), ceil(), floor()
(a)(ii) Binary search:
while(low <= high) {
mid = (low + high)/2;
if(a[mid] == key) return mid;
else if(a[mid] < key) low = mid + 1;
else high = mid - 1;
(b)(i) Fibonacci using recursion:
int fib(int n) {
if(n <= 1) return n;
return fib(n-1) + fib(n-2);
(b)(ii) Swap using pass by value and reference:
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
---
14. (a) Movie billing system using array of structures:
- Create struct Movie {name, type, director, price}
- Use conditionals to add tax based on type (2D or 3D)
(b)(i) Storage classes:
- auto, extern, static, register
- Define scope and lifetime of variables
(b)(ii) Insert/Delete in singly linked list:
- Use dynamic memory (malloc)
- Traverse and free
---
15. (a) Bookstore program with file operations:
struct Book {
int id;
char name[50];
};
- Use fwrite(), fread() for storing and retrieving based on id
(b)(i) Command-line arguments:
int main(int argc, char *argv[]) {
printf("Arg1: %s", argv[1]);
(b)(ii) Sequential vs Random File Access:
- Sequential: fscanf, fprintf
- Random: fseek(), ftell()