Codefix C Language
Codefix C Language
int main() {
int num1 = 5, num2 = 10;
int sum = add(num1, num2);
int arr[5];
for (int i = 0; i <= 5; i++) { // Error 2: Out-of-bounds access in array.
arr[i] = i * 2;
}
return 0;
}
if (is_prime(number))
printf("%d is a prime number.\n", number);
else
printf("%d is not a prime number.\n", number);
return 0;
}
3.Fibonacci Series
#include <stdio.h>
int main() {
int terms, first = 0, second = 1, next;
printf("Fibonacci Series: %d, %d", first, second); // Error 1: This will cause an issue when
terms < 2.
for (int i = 2; i < terms; i++) { // Error 2: Loop should start from 3; also changed the
condition from <= to <.
next = first + second;
printf(", %d", next);
first = second;
second = next;
}
printf("\n");
return 0; // Error 3: Return type of the function is int, but it should return 0 only for
successful execution, check for negative terms first.
}
int main() {
int size;
printf("Enter the number of elements in the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
bubble_sort(arr, size);
return 0; // Error 3: No validation for non-positive sizes, which can cause issues with array
declaration.
}
5.Reverse a String
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Error 2: Incorrectly handling newline character; if the input is exactly 100 characters, it
may not properly remove the newline.
str[strcspn(str, "\n")] = 0;
reverse_string(str);
printf("Reversed string: %s\n", str);
// Error 3: Missing check for empty string; reversing an empty string should be handled
explicitly.
if (strlen(str) == 0) {
printf("You entered an empty string.\n");
}
return 0; // Error 4: No check for buffer overflow; if user enters more than 99 characters, it
can cause undefined behavior.
}