0% found this document useful (0 votes)
3 views18 pages

PWC

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

PWC

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

Q1.

Entry-controlled and Exit-controlled loops

**Entry-controlled loops:** The condition is checked before entering the


loop body. If the condition is false initially, the loop doesn't execute at all.
Examples include `for` and `while` loops.

**Example:**
```c
// Entry-controlled loop (while loop)
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
```

**Exit-controlled loops:** The condition is checked after the loop body is


executed. Even if the condition is false initially, the loop executes at least
once. The `do-while` loop is an example.

**Example:**
```c
// Exit-controlled loop (do-while loop)
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
```

### Q2. IF-ELSE control statement

The `if-else` statement allows for conditional execution of code blocks. It


executes one block if the condition is true and another block if it is false.

**Example:**
```c
int x = 10;
if (x > 5) {
printf("x is greater than 5\n");
} else {
printf("x is not greater than 5\n");
}
```

### Q3. Initialization of arrays

Arrays can be initialized in various ways:


**Inline initialization:**
```c
int arr1[3] = {1, 2, 3};
```

**Partial initialization:**
```c
int arr2[5] = {1, 2}; // Remaining elements initialized to 0
```

**Omitting size:**
```c
int arr3[] = {1, 2, 3}; // Compiler determines size
```

**Using loops:**
```c
int arr4[5];
for (int i = 0; i < 5; i++) {
arr4[i] = i;
}
```
### Q4. Definition and initialization of String

#### Definition of a String

To define a string in C, you declare a character array. The size of the array
should be large enough to hold the string and the terminating null
character.

**Syntax:**

```c

char string_name[size];

```

#### Initialization of a String

Strings can be initialized in various ways:

1. **Direct Initialization:**

The string can be directly assigned a value when it is declared.

```c

char str1[] = "Hello, World!";

```
In this case, the size of the array is determined by the compiler. It
includes the characters in the string plus the null character.

2. **Character Array Initialization:**

You can initialize the string using individual characters and explicitly
include the null character.

```c

char str2[14] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};

```

3. **Using `strcpy` Function:**

The `strcpy` function from the `string.h` library can be used to copy a
string into another string variable.

```c

#include <string.h>

char str3[50];

strcpy(str3, "Hello, World!");

```

#### Examples and Explanation


**Example 1: Direct Initialization**

```c

#include <stdio.h>

int main() {

char greeting[] = "Hello, World!";

printf("%s\n", greeting); // Output: Hello, World!

return 0;

```

- Here, `greeting` is a character array initialized directly with the string


"Hello, World!". The compiler automatically adds the null character at the
end.

**Example 2: Character Array Initialization**

```c

#include <stdio.h>

int main() {

char greeting[14] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};

printf("%s\n", greeting); // Output: Hello, World!

return 0;
}

```

- In this example, the string is initialized character by character, including


the null character at the end.

**Example 3: Using `strcpy` Function**

```c

#include <stdio.h>

#include <string.h>

int main() {

char greeting[50];

strcpy(greeting, "Hello, World!");

printf("%s\n", greeting); // Output: Hello, World!

return 0;

```

- The `strcpy` function copies the string "Hello, World!" into the `greeting`
array. The null character is automatically appended by `strcpy`.

#### Key Points to Remember


1. **Null Character (`'\0'`):** Every string in C ends with a null character,
which marks the end of the string.

2. **Array Size:** When declaring a string, ensure the array size is sufficient
to hold the string plus the null character.

3. **String Functions:** C provides various string handling functions in the


`string.h` library, such as `strcpy`, `strlen`, `strcat`, etc., which
facilitate string manipulation.

### Q5. General form of a function definition

**Syntax:**
```c
return_type function_name(parameter1_type parameter1,
parameter2_type parameter2, ...) {
// Function body
// Statements
return value; // optional
}
```

**Example:**
```c
int add(int a, int b) {
return a + b;
}
```

### Q6. Function call by Value

In call by value, the actual value of the argument is passed to the function.
Changes to the parameter inside the function do not affect the original
value.

**Example:**
```c
void change(int x) {
x = 5;
}

int main() {
int a = 10;
change(a);
printf("%d\n", a); // Output: 10
return 0;
}
```

### Q7. Program to calculate the area of a circle


**Example:**
```c
#include <stdio.h>
#define PI 3.14159

double calculateArea(double radius) {


return PI * radius * radius;
}

int main() {
double radius = 5.0;
double area = calculateArea(radius);
printf("The area of the circle is: %f\n", area);
return 0;
}
```

### Q8. Function call by reference

In call by reference, the address of the argument is passed to the function.


Changes to the parameter inside the function affect the original value.

**Example:**
```c
void change(int *x) {
*x = 5;
}

int main() {
int a = 10;
change(&a);
printf("%d\n", a); // Output: 5
return 0;
}
```

### Q9. Structures

**Definition:**
A structure is a user-defined data type in C that allows combining data
items of different kinds.

**Terms:**
- **Structure tag:** Name given to the structure.
- **Structure members:** Variables declared inside the structure.

### Q10. Define a structure


**Example:**
```c
#include <stdio.h>

struct Person {
char name[50];
int age;
float salary;
};

int main() {
struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 30;
person1.salary = 50000.50;

printf("Name: %s\n", person1.name);


printf("Age: %d\n", person1.age);
printf("Salary: %.2f\n", person1.salary);

return 0;
}
```
### Q11. Array of Structures

**Example:**
```c
#include <stdio.h>

struct Book {
int id;
char title[50];
float price;
};

int main() {
struct Book library[3] = {
{1, "C Programming", 299.99},
{2, "Data Structures", 349.99},
{3, "Algorithms", 399.99}
};

for (int i = 0; i < 3; i++) {


printf("Book ID: %d\n", library[i].id);
printf("Book Title: %s\n", library[i].title);
printf("Book Price: %.2f\n", library[i].price);
}

return 0;
}
```

### Q12. Structure to hold library book data

**Example:**
```c
#include <stdio.h>

struct Book {
int book_code;
char book_title[50];
int num_copies;
int num_copies_issued;
};

int main() {
struct Book library = {101, "C Programming", 5, 2};
printf("Book Code: %d\n", library.book_code);
printf("Book Title: %s\n", library.book_title);
printf("Number of Copies: %d\n", library.num_copies);
printf("Number of Copies Issued: %d\n", library.num_copies_issued);
return 0;
}
```

### Q13. Applications of pointers, advantages & disadvantages

**Applications:**
- Dynamic memory allocation
- Arrays, strings, and data structures like linked lists
- Function arguments (especially for large structures)
- Memory management

**Advantages:**
- Efficient array and string handling
- Dynamic memory allocation
- Easier handling of complex data structures

**Disadvantages:**
- Increased complexity and potential for errors
- Harder to debug
- Risk of memory leaks and pointer-related bugs

### Q14. Pointer with its syntax & example

**Syntax:**
```c
type *pointer_name;
```

**Example:**
```c
int x = 10;
int *p = &x;
printf("Value of x: %d\n", *p);
```

### Q15. Dynamic memory allocation in C

**Concept:**
Dynamic memory allocation allows for allocating memory during runtime
using functions from the `stdlib.h` library.

**Functions:**
- `malloc()`: Allocates a block of memory.
- `calloc()`: Allocates an array of blocks and initializes them to zero.
- `realloc()`: Reallocates memory to resize a previously allocated block.
- `free()`: Frees dynamically allocated memory.

### Q16. Example of `free` function in dynamic memory allocation

**Example:**
```c
#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr = (int *)malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
free(ptr); // Freeing the allocated memory
return 0;
}
```

These detailed answers should cover all the aspects required for a
comprehensive understanding of each topic. Let me know if you need any
further clarification!

You might also like