-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hello.c
86 lines (69 loc) · 1.96 KB
/
test_hello.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function declarations
void printMessage(const char *message);
int add(int a, int b);
double multiply(double a, double b);
char* concatenateStrings(const char *str1, const char *str2);
void printArray(int arr[], int size);
int findMax(int arr[], int size);
int main() {
// Demonstrating printMessage function
printMessage("Hello, world!");
// Demonstrating add function
int sum = add(5, 7);
printf("Sum: %d\n", sum);
// Demonstrating multiply function
double product = multiply(3.5, 2.0);
printf("Product: %.2f\n", product);
// Demonstrating concatenateStrings function
char *combined = concatenateStrings("Hello, ", "world!");
printf("Concatenated String: %s\n", combined);
free(combined);
// Demonstrating printArray and findMax functions
int numbers[] = {4, 7, 1, 8, 5, 9};
int size = sizeof(numbers) / sizeof(numbers[0]);
printArray(numbers, size);
int max = findMax(numbers, size);
printf("Maximum Value: %d\n", max);
return 0;
}
// Function definitions
void printMessage(const char *message) {
printf("%s\n", message);
}
int add(int a, int b) {
return a + b;
}
double multiply(double a, double b) {
return a * b;
}
char* concatenateStrings(const char *str1, const char *str2) {
// Allocate memory for the concatenated string
char *result = (char*)malloc(strlen(str1) + strlen(str2) + 1);
if (result == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
// Concatenate the strings
strcpy(result, str1);
strcat(result, str2);
return result;
}
void printArray(int arr[], int size) {
printf("Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int findMax(int arr[], int size) {
int max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}