CS25C01 – Computer Programming: C (Regulation 2025)
Question Bank with Answers
UNIT I – BASICS OF C PROGRAMMING — Answers
1. Explain the structure of a C program with an example.
A C program typically contains: 1) Preprocessor directives (e.g., #include ) 2) Global declarations (optional) 3)
main() function — program execution starts here 4) Function definitions (user-defined functions) Example:
#include int add(int a,int b) { return a+b; } int main() { int x=2,y=3; printf("Sum = %d\n", add(x,y)); return 0; }
Compilation: gcc file.c -o file Execution: ./file
2. Write a C program to check whether a given number is prime or not.
#include <stdio.h>
int main() {
int n, i, isPrime = 1;
scanf("%d", &n);
if (n <= 1) isPrime = 0;
for (i = 2; i*i <= n && isPrime; ++i) {
if (n % i == 0) isPrime = 0;
}
if (isPrime) printf("%d is prime
", n);
else printf("%d is not prime
", n);
return 0;
}
3. Discuss about different data types in C with examples.
Basic data types: char (1 byte), int (typically 4 bytes), float (4 bytes), double (8 bytes). Derived types: arrays,
pointers, structures, unions, functions. Qualifiers: signed, unsigned, short, long. Example: unsigned int
u=3000000000U; char ch='A'; float f=3.14f;
4. Differentiate between while and do-while loops with examples.
while(condition) { /* body */ } // condition checked before body — may execute 0 times. do { /* body */ }
while(condition); // body executes at least once — condition checked after. Example while: int i=0; while(i<3) {
printf("%d ", i); i++; } Example do-while: int i=0; do { printf("%d ", i); i++; } while(i<3);
UNIT II – ARRAYS AND STRINGS — Answers
1. Write a C program to find the sum of all elements in a 1D array.
#include <stdio.h>
int main() {
int n, i, sum = 0;
scanf("%d", &n);
int a[n];
for (i = 0; i < n; ++i) scanf("%d", &a[i]);
for (i = 0; i < n; ++i) sum += a[i];
printf("Sum = %d
", sum);
return 0;
}
2. Explain 2D arrays with an example program for matrix addition.
#include <stdio.h>
int main() {
int r, c, i, j;
scanf("%d %d", &r, &c);
int A[r][c], B[r][c], C[r][c];
for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d", &A[i][j]);
for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d", &B[i][j]);
for(i=0;i<r;i++) for(j=0;j<c;j++) C[i][j] = A[i][j] + B[i][j];
for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%d ", C[i][j]); printf("
"); }
return 0;
}
3. Write a program to reverse a string without using library functions.
#include <stdio.h>
#include <string.h>
int main() {
char s[200];
if(!fgets(s, sizeof(s), stdin)) return 0;
int len = strlen(s);
if(len>0 && s[len-1]=='
') s[--len] = '■';
for(int i = len-1; i >= 0; --i) putchar(s[i]);
putchar('
');
return 0;
}
4. Explain different string handling functions in C with examples.
strlen(s) - returns length; strcpy(dest, src) - copy; strcat(dest, src) - concatenate; strcmp(a,b) - compare. Example:
char a[20] = "Hello"; char b[20] = "World"; strcat(a, " "); strcat(a,b); // a becomes "Hello World".
UNIT III – FUNCTIONS AND STORAGE CLASSES — Answers
1. Write a recursive function to find factorial of a number.
#include <stdio.h>
long fact(long n) { if (n<=1) return 1; return n * fact(n-1); }
int main() { int n; scanf("%d", &n); printf("%ld
", fact(n)); return 0; }
2. Differentiate between call by value and call by reference with examples.
Call by value: function receives copies — changes don't affect caller. Example (swap fails): void swap(int a,int
b){int t=a;a=b;b=t;} Call by reference (using pointers): void swap(int *a,int *b){int t=*a;*a=*b;*b=t;} — caller values
change.
3. Explain different storage classes in C with suitable examples.
auto: default for local variables (block scope). static: preserves value across function calls and gives internal
linkage for globals. extern: reference to a global defined elsewhere. register: hint to store in CPU register
(deprecated). Examples: static int cnt=0; // retains value between calls extern int x; // declared elsewhere
4. Write a program to demonstrate array passing to functions.
#include <stdio.h>
void printArr(int a[], int n) {
for (int i=0;i<n;i++) printf("%d ", a[i]);
printf("
");
}
int main() {
int a[] = {1,2,3,4,5};
printArr(a, 5);
return 0;
}
UNIT IV – POINTERS AND STRUCTURES — Answers
1. Write a C program to swap two numbers using pointers.
#include <stdio.h>
void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x=%d y=%d
", x, y);
return 0;
}
2. Explain pointer arithmetic with examples.
If p is int* pointing to a[i], then p+1 points to a[i+1]. Adding 1 advances by sizeof(int). Example: int a[3] = {2,4,6}; int
*p = a; *(p+1) == a[1] == 4.
3. Write a program to create a student structure and display details of students.
#include <stdio.h>
struct Student { char name[50]; int id; float cgpa; };
int main() {
struct Student s1 = {"Alice", 101, 8.5f};
printf("Name: %s
ID: %d
CGPA: %.2f
", s1.name, s1.id, s1.cgpa);
return 0;
}
4. Differentiate between structures and unions with examples.
Structures allocate separate memory for each member; size = sum of members (plus padding). Union overlays
members — size = max(member sizes); only one member valid at a time. Use struct when all members are
needed; union to save memory for mutually exclusive data.
UNIT V – FILE HANDLING — Answers
1. Write a C program to read and write student details into a file.
#include <stdio.h>
struct Student { char name[50]; int id; float cgpa; };
int main() {
struct Student s;
FILE *fp = fopen("students.txt", "w");
if (!fp) return 1;
printf("Enter name id cgpa:
");
scanf("%49s %d %f", s.name, &s.id, &s.cgpa);
fprintf(fp, "%s %d %.2f
", s.name, s.id, s.cgpa);
fclose(fp);
return 0;
}
2. Explain random access in file handling with an example program.
Use fopen with "rb+"/"wb+" and functions fseek, ftell to move file pointer. Example: To update nth record in a
binary file, fseek(fp, n*sizeof(record), SEEK_SET); fwrite(&rec;, sizeof(rec), 1, fp);
3. Write a program to count number of lines, words, and characters in a text file.
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *fp = fopen("input.txt", "r");
if(!fp) return 1;
int c, nw=0, nl=0, nc=0, inword=0;
while((c=fgetc(fp))!=EOF) {
nc++;
if(c=='
') nl++;
if(isspace(c)) inword=0;
else if(!inword){ inword=1; nw++; }
}
fclose(fp);
printf("Lines=%d Words=%d Chars=%d
", nl, nw, nc);
return 0;
}
4. Discuss command line arguments with an example.
int main(int argc,char *argv[]){ for(int i=0;i<argc;i++) printf("%s\n", argv[i]); }
Lab Experiments — Hints / Sample Programs
1. I/O, operators, expressions: Write simple programs using scanf/printf and arithmetic operators.
2. Decision & looping: Implement if-else, switch, for, while, do-while examples (prime, factorial, patterns).
3. Arrays & strings: Matrix operations, searching, sorting, string parsing and manipulation.
4. Functions & recursion: Modularize code, implement recursive fibonacci/factorial, use helper functions.
5. Structures & unions: Design record types for students/employees and serialize to file.
6. Pointers: Pointer arithmetic examples, dynamic memory using malloc (if allowed).
7. File handling: Read/write text files, parse CSV-like lines, simple indexing using fseek.