PWC
PWC
**Example:**
```c
// Entry-controlled loop (while loop)
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
```
**Example:**
```c
// Exit-controlled loop (do-while loop)
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
```
**Example:**
```c
int x = 10;
if (x > 5) {
printf("x is greater than 5\n");
} else {
printf("x is not greater than 5\n");
}
```
**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
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];
```
1. **Direct Initialization:**
```c
```
In this case, the size of the array is determined by the compiler. It
includes the characters in the string plus the null character.
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'};
```
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];
```
```c
#include <stdio.h>
int main() {
return 0;
```
```c
#include <stdio.h>
int main() {
char greeting[14] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
return 0;
}
```
```c
#include <stdio.h>
#include <string.h>
int main() {
char greeting[50];
return 0;
```
- The `strcpy` function copies the string "Hello, World!" into the `greeting`
array. The null character is automatically appended by `strcpy`.
2. **Array Size:** When declaring a string, ensure the array size is sufficient
to hold the string plus the null character.
**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;
}
```
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;
}
```
int main() {
double radius = 5.0;
double area = calculateArea(radius);
printf("The area of the circle is: %f\n", area);
return 0;
}
```
**Example:**
```c
void change(int *x) {
*x = 5;
}
int main() {
int a = 10;
change(&a);
printf("%d\n", a); // Output: 5
return 0;
}
```
**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.
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;
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}
};
return 0;
}
```
**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;
}
```
**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
**Syntax:**
```c
type *pointer_name;
```
**Example:**
```c
int x = 10;
int *p = &x;
printf("Value of x: %d\n", *p);
```
**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.
**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!