C Programming Questions and Answers
PART - A (4 × 2 = 8)
1. Applications of C programming language:
- System programming (Operating Systems)
- Embedded Systems
- Game Development
- Database Management Systems
- Networking and Communication Systems
- Compiler Design
2. Purpose of break, continue, and goto statements in C:
- break: Exits a loop or switch statement prematurely.
- continue: Skips the current iteration of a loop and moves to the next iteration.
- goto: Transfers control to a labeled statement within the program.
3. Declaration of a total array of size 5 and assigning five values in C:
int total[5] = {10, 20, 30, 40, 50};
4. String operation functions in C:
- strlen() - Returns string length
- strcpy() - Copies one string to another
- strcat() - Concatenates two strings
- strcmp() - Compares two strings
- strchr() - Finds a character in a string
- strstr() - Finds a substring in a string
PART - B (10+12+10)
5. Structure of the C programming language:
A C program consists of:
- Preprocessor directives (#include<stdio.h>)
- Global declarations
- main() function (Entry point)
- Variable declarations
- Executable statements
- Functions (if needed)
6. Input and output statements in C language:
- Input:
- scanf("%d", &var); (Reads input)
- gets(str); (Reads a string)
- Output:
- printf("Hello"); (Displays output)
- puts(str); (Displays a string)
7. C program to implement the transpose of a matrix:
7. C program to implement the transpose of a matrix:
#include <stdio.h>
int main() {
int matrix[3][3], transpose[3][3];
int i, j;
printf("Enter 3x3 matrix elements:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
scanf("%d", &matrix[i][j]);
// Transpose logic
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%d ", transpose[i][j]);
printf("\n");
}
return 0;
}
8. C program to implement selection sort:
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, min_idx, temp;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
int i;
selectionSort(arr, n);
printf("Sorted array: \n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}