0% found this document useful (0 votes)
7 views

C Programming Exam Solutions

The document provides solutions to a C programming exam, covering topics such as header files, macros, structures, memory allocation, and various C programs. It includes code examples for sorting integers, checking palindromes, adding complex numbers, manipulating 2D arrays, converting strings, counting words in files, multiplying matrices, and managing book details with structures. Each section contains concise explanations and relevant code snippets to illustrate the concepts.

Uploaded by

localboy10008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C Programming Exam Solutions

The document provides solutions to a C programming exam, covering topics such as header files, macros, structures, memory allocation, and various C programs. It includes code examples for sorting integers, checking palindromes, adding complex numbers, manipulating 2D arrays, converting strings, counting words in files, multiplying matrices, and managing book details with structures. Each section contains concise explanations and relevant code snippets to illustrate the concepts.

Uploaded by

localboy10008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

C Programming Exam Solutions

Section A (Very Short Answers)


1. 1. What are header files and their uses?

Header files in C (like stdio.h, math.h) contain function declarations, macros, and constants
used in programs. They allow code reuse and help manage complex programs by separating
function declarations from their definitions.

2. 2. What is the difference between macro and function?

Macros are preprocessor directives expanded at compile-time, while functions are compiled
and executed at runtime. Macros are faster but error-prone; functions offer type checking
and debugging ease.

3. 3. What is structure? How is it different from union?

Structure is a user-defined data type that groups variables of different types. In a structure,
all members occupy separate memory locations. In a union, all members share the same
memory, so only one can be used at a time.

4. 4. What is static memory allocation and dynamic memory allocation?

Static allocation: Memory is allocated at compile-time (e.g., arrays). Dynamic allocation:


Memory is allocated at runtime using malloc(), calloc(), etc., and can be resized/freed as
needed.

5. 5. What is the difference between getc(), getchar(), getch(), and getche()?

getc(): Reads a character from a file. getchar(): Reads from standard input. getch(): Reads
without echoing character. getche(): Reads and echoes character (used in conio.h).

Section B (Short Answers)


6. 6. C Program to Read 20 Integers and Sort in Ascending Order

```c
#include <stdio.h>
int main() {
int a[20], i, j, temp;
printf("Enter 20 integers:\n");
for(i = 0; i < 20; i++)
scanf("%d", &a[i]);
for(i = 0; i < 19; i++)
for(j = i + 1; j < 20; j++)
if(a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
printf("Sorted numbers:\n");
for(i = 0; i < 20; i++)
printf("%d ", a[i]);
return 0;
}
```

7. 7. C Program to Check Palindrome Without Using String Library

```c
#include <stdio.h>
int main() {
char str[100];
int i, len = 0, isPalindrome = 1;
printf("Enter a string: ");
scanf("%s", str);
while (str[len] != '\0') len++;
for(i = 0; i < len/2; i++) {
if(str[i] != str[len-1-i]) {
isPalindrome = 0;
break;
}
}
if(isPalindrome)
printf("Palindrome\n");
else
printf("Not a Palindrome\n");
return 0;
}
```

8. 8. Add Two Complex Numbers Using Structure

```c
#include <stdio.h>
struct Complex {
float real, imag;
};
struct Complex add(struct Complex a, struct Complex b) {
struct Complex result;
result.real = a.real + b.real;
result.imag = a.imag + b.imag;
return result;
}
int main() {
struct Complex c1 = {2.3, 4.5}, c2 = {1.6, 2.5}, sum;
sum = add(c1, c2);
printf("Sum = %.1f + %.1fi\n", sum.real, sum.imag);
return 0;
}
```

Section C (Detailed Answers)


9. 9. Pointer and Accessing 2D Array

```c
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *ptr = &arr[0][0];
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", *(ptr + i*3 + j));
}
printf("\n");
}
return 0;
}
```

10. 10. String vs Array & Convert Lower to Upper

String: A collection of characters ending with \0.


Array: A collection of elements of the same data type.
```c
#include <stdio.h>
int main() {
char str[100];
int i = 0;
printf("Enter lowercase string: ");
scanf("%s", str);
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
i++;
}
printf("Uppercase: %s\n", str);
return 0;
}
```

11. 11. Count Words and Characters in a File

```c
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *f = fopen("test1.txt", "r");
char ch;
int words = 0, chars = 0, inWord = 0;
if (!f) return 1;
while ((ch = fgetc(f)) != EOF) {
chars++;
if (isspace(ch)) inWord = 0;
else if (!inWord) {
words++;
inWord = 1;
}
}
fclose(f);
printf("Words: %d, Characters: %d\n", words, chars);
return 0;
}
```

12. 12. Multiply Two 4x4 Matrices

```c
#include <stdio.h>
int main() {
int a[4][4], b[4][4], c[4][4] = {0};
printf("Enter first 4x4 matrix:\n");
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
scanf("%d", &a[i][j]);
printf("Enter second 4x4 matrix:\n");
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
scanf("%d", &b[i][j]);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
for(int k = 0; k < 4; k++)
c[i][j] += a[i][k] * b[k][j];
printf("Result matrix:\n");
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
```

13. 13. Structure for Book Details

```c
#include <stdio.h>
#include <string.h>
struct Book {
char title[50];
char author[50];
float price;
};
int main() {
struct Book books[3];
int i, max = 0, min = 0;
for(i = 0; i < 3; i++) {
printf("Enter title, author, price of book %d:\n", i+1);
scanf(" %[^
]%*c %[^
]%*c %f", books[i].title, books[i].author, &books[i].price);
}
for(i = 1; i < 3; i++) {
if(books[i].price > books[max].price)
max = i;
if(books[i].price < books[min].price)
min = i;
}
printf("Most Expensive Book: %s by %s ($%.2f)\n", books[max].title, books[max].author,
books[max].price);
printf("Lowest Priced Book: %s by %s ($%.2f)\n", books[min].title, books[min].author,
books[min].price);
return 0;
}
```

You might also like