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

C_Programming_Answers_Set2

The document outlines key concepts in C programming, including differences between arrays and structures, recursion, and memory allocation. It provides code examples for reversing a string, Fibonacci series, and sorting vehicles by price. Additionally, it explains call by value vs call by reference, pointer arithmetic, and dynamic vs static memory allocation.

Uploaded by

jaseenamadappat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C_Programming_Answers_Set2

The document outlines key concepts in C programming, including differences between arrays and structures, recursion, and memory allocation. It provides code examples for reversing a string, Fibonacci series, and sorting vehicles by price. Additionally, it explains call by value vs call by reference, pointer arithmetic, and dynamic vs static memory allocation.

Uploaded by

jaseenamadappat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C Programming Questions - Set 2

1. Difference between Array and Structure:

- Array: Collection of elements of the **same data type**.


- Structure: Collection of elements of **different data types**.

| Feature | Array | Structure |


|-------------|----------------------------|------------------------------|
| Data Types | Same type | Can be different types |
| Memory | Continuous block | Memory based on members |
| Access | Index based | Dot operator |

2. Function to Reverse a String:

void reverse(char str[]) {


int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = temp;
}
}

3. Structure vs Union:

| Feature | Structure | Union |


|-------------|----------------------------|-------------------------------|
| Memory | Each member gets space | Shared space for all members |
| Access | All members at once | One at a time |

4. Recursion and Fibonacci Series Function:

Recursion is a function calling itself.

void fibonacci(int a, int b, int n) {


if (n > 0) {
int c = a + b;
printf("%d ", c);
fibonacci(b, c, n - 1);
}
}

5. Output Prediction and Reason:


Code:
#include<stdio.h>
int main() {
for(int i=0;i<10;i++);
printf("%d",i);
return 0;
}

Output: **Error** - 'i' is not declared in scope of printf.


Reason: Variable 'i' is declared **inside** the for loop, and not available outside.

6. Call by Value vs Call by Reference:

Call by Value: Copies value.


Call by Reference: Uses address.

Example:
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

7. Pointer and Pointer Arithmetic:

Pointer stores address of another variable.

int *ptr, a = 10;


ptr = &a;

Pointer Arithmetic:
- ptr + 1 moves to next memory location.
- Useful in arrays and dynamic memory.

Example:
int arr[3] = {1, 2, 3};
int *p = arr;
printf("%d", *(p + 1)); // prints 2

8. Structure within a Structure:

struct Date {
int day, month, year;
};
struct Student {
char name[50];
struct Date dob;
};

9. Structure for Vehicle and Sort by Price:

struct Vehicle {
char model[50];
int year;
int price;
};

void input(struct Vehicle v[], int n) {


for (int i = 0; i < n; i++)
scanf("%s %d %d", v[i].model, &v[i].year, &v[i].price);
}

void sort(struct Vehicle v[], int n) {


struct Vehicle temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (v[i].price < v[j].price) {
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
}
}
}

10. Pointer to Function:

A pointer that points to a function.

Syntax:
return_type (*pointer_name)(parameter_list);

Example:
int add(int a, int b) { return a + b; }
int (*func_ptr)(int, int) = add;

11. Dynamic vs Static Memory Allocation:


Advantage: Dynamic memory is allocated during runtime, saving memory.

Example:
int *ptr = (int*)malloc(5 * sizeof(int));
if (ptr != NULL) {
for (int i = 0; i < 5; i++) ptr[i] = i;
}

You might also like