Story: I tried to convert a c99 script to regular gcc.
Problem: The output is empty.
Expected output: 3,2,1
length is the number of elements in the array.
Update: the script is designed to sort the elements of the array in a descending order.
The code:
#include <stdio.h>
int main() {
int arr[] = { 1,2,3 };
int temp = 0;
int length = sizeof(arr) / sizeof(arr[0]);
int i = 0;
int j = i + 1;
for (i < length; i++;) {
for (j < length; j++;) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
int y = 0;
for (y < length; y++;) {
printf("%d ", arr[y]);
}
return 0;
}
for-loop syntax is wrong. You're using a condition where the initializer is expected and incrementing where the condition is expected.for (y < length; y++;)is weird — you have a condition where you should have an initialization (or nothing — so the test does nothing at all) and they++is the tested condition and it fails on the first iteration becauseyis zero or false on the first iteration and it is a post-increment. All your code is legitimate under C90, let alone C99 or C11 or C18 (unless the automatic array initialization was not supported in C90 — I'd need to research that, and I'm too lazy to do so because it is almost 20 years irrelevant).arr[0]from withinarr's initializer has fun issues: stackoverflow.com/a/52309196/1848654