IMPORTANT QUESTIONS
1.Write any two string handling functions in C with their syntax and purpose.
strcpy()
Syntax: strcpy(dest, src);
Purpose: Copies src string to dest.
strlen()
Syntax: strlen(str);
Purpose: Returns the length of the string str.
2.If p is a pointer to a float array a[10] with base address 1000, what is the value of p after p++?
Since p is a float pointer and sizeof(float) = 4,
after p++, p becomes 1004.
3.Illustrate with an example for &, *, and -> with pointers and structures.
struct Student { int id; };
struct Student s = {101}, *ptr = &s;
& → address: &s
* → value: (*ptr).id
-> → shortcut: ptr->id
4.Name two functions used in Random Access files in C.
fseek() – moves file pointer to a specific location
ftell() – returns current position of file pointer
5.Senior Citizen and Eligible for Booster" if age > 60 and vaccination input is 2Otherwise print "Below 60, and
Eligible for Vaccination Use Conditional Operator (?:).
#include <stdio.h>
int main() {
int age, vaccine;
// Get input from user
printf("Enter age: ");
scanf("%d", &age);
printf("Enter vaccination detail (2 for completed): ");
scanf("%d", &vaccine);
// Use conditional operator to display result
(age > 60 && vaccine == 2) ?
printf("Senior Citizen and Eligible for Booster\n") :
printf("Below 60, and Eligible for Vaccination\n");
return 0;
}
6.
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter number of rows (waiters): ");
scanf("%d", &rows);
printf("Enter number of tables per row: ");
scanf("%d", &cols);
int bill[rows][cols];
// Input bill values
for (int i = 0; i < rows; i++) {
printf("Enter bills for waiter %d: ", i + 1);
for (int j = 0; j < cols; j++) {
scanf("%d", &bill[i][j]);
}
}
// Find and display highest bill for each waiter
for (int i = 0; i < rows; i++) {
int max = bill[i][0];
for (int j = 1; j < cols; j++) {
if (bill[i][j] > max) {
max = bill[i][j];
}
}
printf("Highest bill served by waiter %d is %d\n", i + 1, max);
}
return 0;
}
7.
Binary Search is an efficient technique for searching in sorted arrays.
Steps:
1.Find middle index: mid = (low + high) / 2
2.Compare key with middle element:
If key < mid, search in left half
If key > mid, search in right half
If key == mid, found!
❗
3.Repeat until low > high
Important:
The array must be sorted.
→
Original array:
{12, 66, 90, 14, 19, 63, 88, 25, 37, 49, 110} unsorted
Step 1: Sort it:
{12, 14, 19, 25, 37, 49, 63, 66, 88, 90, 110}
Step 2: Apply Binary Search to find 14
→ → →
Search Steps:
→ → →
low = 0, high = 10 mid = 5 arr[5] = 49 14 < 49
→ → →
new high = 4 mid = 2 arr[2] = 19 14 < 19
→ → →✅
new high = 1 mid = 0 arr[0] = 12 14 > 12
✅
new low = 1 mid = 1 arr[1] = 14 Found!
Key 14 found at index 1 (in sorted array)
8.In a Marathon, there are 5 tracks. In each track, n number of participants are placed.
Find the youngest and oldest participant in each track.
#include <stdio.h>
int main() {
int tracks = 5;
int n;
for (int i = 0; i < tracks; i++) {
printf("\nEnter number of participants in track %d: ", i + 1);
scanf("%d", &n);
int age[n];
printf("Enter ages of %d participants in track %d: ", n, i + 1);
for (int j = 0; j < n; j++) {
scanf("%d", &age[j]);
}
// Initialize youngest and oldest
int youngest = age[0], oldest = age[0];
// Find youngest and oldest
for (int j = 1; j < n; j++) {
if (age[j] < youngest)
youngest = age[j];
if (age[j] > oldest)
oldest = age[j];
}
}
→
// Display result for this track
printf("Track %d Youngest: %d, Oldest: %d\n", i + 1, youngest, oldest);
return 0;
}
9.Create an array of lucky ticket numbers announced for prize bonanza.
Get a ticket number and check if it is for prize, and display the result.
Use pointer to array concept.
#include <stdio.h>
int main() {
int lucky[] = {101, 202, 303, 404, 505}; // Example lucky ticket numbers
int *ptr = lucky; // Pointer to array
int ticket, found = 0;
printf("Enter your ticket number: ");
scanf("%d", &ticket);
// Check using pointer
for (int i = 0; i < 5; i++) {
if (*(ptr + i) == ticket) {
found = 1;
break;
}
}
if (found)
🎉
printf(" Congratulations! Ticket %d is a winning ticket!\n", ticket);
else
printf("Sorry! Ticket %d did not win.\n", ticket);
return 0;
}
Sample Output:
CopyEdit
🎉
Enter your ticket number: 303
Congratulations! Ticket 303 is a winning ticket!
10.Declare a float array of size 5 and assign 5 values to it.
float arr[5] = {10.5, 20.2, 15.8, 30.0, 5.5};
11.Which is better to use: Macro or Function? Justify.
Function is better because:
It provides type checking
Uses less memory (macro expands code)
Easier to debug
Macro is faster but riskier (no type safety).
12.Give an example for fseek()
FILE *fp = fopen("data.txt", "r");
fseek(fp, 0, SEEK_END); // Move to end of file
13.C Program to Multiply Two Matrices (2D arrays)
#include <stdio.h>
int main() {
int a[10][10], b[10][10], result[10][10];
int r1, c1, r2, c2;
// Input matrix sizes
printf("Enter rows and columns for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for second matrix: ");
scanf("%d%d", &r2, &c2);
// Check matrix multiplication condition
if (c1 != r2) {
printf("Matrix multiplication not possible.\n");
return 0;
}
// Input first matrix
printf("Enter elements of first matrix:\n");
for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
// Input second matrix
printf("Enter elements of second matrix:\n");
for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
// Initialize result matrix to 0
for (int i = 0; i < r1; i++)
for (int j = 0; j < c2; j++)
result[i][j] = 0;
// Matrix multiplication logic
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
// Display result
printf("Resultant Matrix:\n");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
14.C Program to Perform Scaling of Two Matrices (2D arrays)
#include <stdio.h>
int main() {
int matrix1[10][10], matrix2[10][10];
int rows, cols;
int scalar;
// Input matrix size
printf("Enter number of rows and columns: ");
scanf("%d%d", &rows, &cols);
// Input scalar
printf("Enter the scalar value: ");
scanf("%d", &scalar);
// Input first matrix
printf("Enter elements of first matrix:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &matrix1[i][j]);
// Input second matrix
printf("Enter elements of second matrix:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &matrix2[i][j]);
// Scale and display first matrix
printf("Scaled Matrix 1:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix1[i][j] * scalar);
}
printf("\n");
}
// Scale and display second matrix
printf("Scaled Matrix 2:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix2[i][j] * scalar);
}
printf("\n");
}
return 0;
}
14.Write a C program to write all the members of an array of structures to a file using fwrite(). Read the array from
the file and display on screen.
#include <stdio.h>
struct Student {
char name[50];
int marks;
};
int main() {
FILE *fp;
struct Student students[3] = {
{"Alice", 85},
{"Bob", 90},
{"Charlie", 78}
};
struct Student readStudents[3];
// Write to file
fp = fopen("students.dat", "wb");
fwrite(students, sizeof(struct Student), 3, fp);
fclose(fp);
// Read from file
fp = fopen("students.dat", "rb");
fread(readStudents, sizeof(struct Student), 3, fp);
fclose(fp);
// Display
printf("Student details from file:\n");
for (int i = 0; i < 3; i++) {
printf("Name: %s, Marks: %d\n", readStudents[i].name, readStudents[i].marks);
}
return 0;
15.Write a C program to get name and marks of 'n' number of students from user and store them in a file.
#include <stdio.h>
struct Student {
char name[50];
int marks;
};
int main() {
FILE *fp;
struct Student s;
int n, i;
fp = fopen("students.txt", "w"); // open for writing
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter name and marks of student %d:\n", i + 1);
scanf("%s %d", s.name, &s.marks);
fprintf(fp, "%s %d\n", s.name, s.marks);
}
fclose(fp);
printf("Data written to file.\n");
return 0;
}
16.Write a C program to read name and marks of 'n' students and store them in a file. If the file previously exists, then
append the information.
#include <stdio.h>
struct Student {
char name[50];
int marks;
};
int main() {
FILE *fp;
struct Student s;
int n, i;
fp = fopen("students.txt", "a"); // open in append mode
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter name and marks of student %d:\n", i + 1);
scanf("%s %d", s.name, &s.marks);
fprintf(fp, "%s %d\n", s.name, s.marks);
}
fclose(fp);
printf("Data appended to file.\n");
return 0;
18.What is difference between the statements a = 5 and a == 5 in C language?
→
→
a = 5 Assignment operator; assigns value 5 to variable a.
a == 5 Equality comparison operator; checks if a is equal to 5