-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
135 lines (104 loc) · 2.93 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
// Function to subtract two numbers
int subtract(int a, int b) {
return a - b;
}
// Function to multiply two numbers
int multiply(int a, int b) {
return a * b;
}
// Function to divide two numbers (handles division by zero)
float divide(int a, int b) {
if (b == 0) {
printf("Error: Division by zero!\n");
return 0;
}
return (float)a / b;
}
int main() {
int x, y, choice;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Choose operation:\n");
printf("1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide\n");
scanf("%d", &choice);
int result;
float result_f;
switch (choice) {
case 1:
result = add(x, y);
printf("Result: %d\n", result);
break;
case 2:
result = subtract(x, y);
printf("Result: %d\n", result);
break;
case 3:
result = multiply(x, y);
printf("Result: %d\n", result);
break;
case 4:
result_f = divide(x, y);
printf("Result: %.2f\n", result_f);
break;
default:
printf("Invalid choice!\n");
}
printf("Looping through numbers 1 to 5:\n");
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
// pgm 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function that takes ownership of a dynamically allocated string
void take_ownership(char *str) {
printf("Received string: %s\n", str);
free(str); // Freeing the memory, as this function owns it
}
// Function that borrows a string (does not free it)
void borrow_string(const char *str) {
printf("Borrowed string: %s\n", str);
}
int main() {
// Allocating memory dynamically (ownership is with main)
char *my_string = (char *)malloc(50 * sizeof(char));
if (!my_string) {
printf("Memory allocation failed\n");
return 1;
}
strcpy(my_string, "Hello from C!");
// Borrowing: Just passing reference, ownership remains with main
borrow_string(my_string);
// Ownership transfer: `take_ownership` now owns and frees the memory
take_ownership(my_string);
// This would cause an error because the memory was freed!
// printf("After free: %s\n", my_string); // UNDEFINED BEHAVIOR!
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Function with potential buffer overflow
void unsafe_copy(char *dest, char *src) {
int i = 0;
while (src[i]!= '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0'; // Null-terminate the destination string
}
int main() {
char source = "This is a long string that might cause an overflow!";
char destination; // Small buffer
unsafe_copy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}