-1

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;
}
  • 3
    What does "c99 to regular stuff" mean? I don't see anything here that is even C99-specific. – jamesdlin Sep 22 '19 at 06:09
  • 2
    Your `for`-loop syntax is wrong. You're using a condition where the initializer is expected and incrementing where the condition is expected. – jamesdlin Sep 22 '19 at 06:12
  • "Regular gcc" defaults to C17, which is even newer than C99. – melpomene Sep 22 '19 at 06:14
  • 3
    More accurately, regarding your for-loop usage, the *syntax* is ok; the *logic* is wrong. This will compile, but will *not* do what you probably expect. At least three warnings about no-effect code should come from this. If that doesn't happen, you need to turn up your warnings and treat them as errors. – WhozCraig Sep 22 '19 at 06:15
  • 1
    You loop `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 the `y++` is the tested condition and it fails on the first iteration because `y` is 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). – Jonathan Leffler Sep 22 '19 at 06:16
  • @JonathanLeffler No need to research that, the ANSI-C syntax of a for(;;) loop has never changed. The loops are plain buggy. Sümer Kolçak : You should review the syntax of for loops. I'm really surprised your code doesn't take forever to run. – Michaël Roy Sep 22 '19 at 06:27
  • @MichaëlRoy: It's not the `for` loop syntax that I'm worried about (I know that's OK). It is the `int arr[] = { 3, 2, 1 };` for an automatic array — I have twitching memories that think it might not have been allowed in strict C90 (even though compilers like GCC probably allow it, even in C90 mode). – Jonathan Leffler Sep 22 '19 at 06:29
  • @JonathanLeffler There's nothing wrong with `int arr[] = { 3, 2, 1 };` in C90. – jamesdlin Sep 22 '19 at 06:33
  • @jamesdlin: After doing the research (despite saying I wouldn't), I agree; my twitching must be due to something else. I (now) think that constants were OK, but computed values were not. Testing `int arr[] = { 3, 2, arr[0] + 1 };` gets a warning from GCC with `-std=c90 -pedantic`, but not otherwise. The warning/error (depending on `-Werror`) is: `initializer element is not computable at load time`. – Jonathan Leffler Sep 22 '19 at 06:37
  • 1
    @JonathanLeffler Well, you're right about constants, but referring to `arr[0]` from within `arr`'s initializer has fun issues: https://stackoverflow.com/a/52309196/1848654 – melpomene Sep 22 '19 at 06:41
  • @melpomene: It was a simple way of getting a non-constant expression into the initializer — adequate to demonstrate my point. I've not checked the other question, but the issue is likely about the sequence of operations in an initializer. C11 [§6.7.9 Initialization ¶23](https://port70.net/~nsz/c/c11/n1570.html#6.7.9p23): _The evaluations of the initialization list expressions are indeterminately sequenced with respect to one another and thus the order in which any side effects occur is unspecified._ Fair cop. – Jonathan Leffler Sep 22 '19 at 06:45
  • @JonathanLeffler It's a bit worse. I believe `int i = 0; return i;` has undefined behavior in C. I should probably ask a separate question about that. – melpomene Sep 22 '19 at 06:46
  • 1
    I don’t see how your two statement fragment would have UB, @melpomene. – Jonathan Leffler Sep 22 '19 at 06:52

2 Answers2

1

Your syntax for for loops is the issue.

Here is the correct way to write your loops.

int i, j;
for (i = 0; i < length; ++i)         // for (initialisation; test condition; operation)
{
    for (j = i + 1; j < length; ++j) // note that j is initialized with i + 1 on each iteration of 
                                     // the outer loop.  That's what makes the bubble sort work.
    {
         /* test and swap if needed */
    }
}

for (i = 0; i < length; ++i)  // note that i is reset to zero, so we can scan the array from 
                              // a known position (the top) to bottom.
{
    /* printout */
}
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
0

Your semicolon is in the wrong place, move it to the far left just inside the parentheses.

Loop syntax is:

for (intializer; break condition; iterator)

Ian Newson
  • 7,679
  • 2
  • 47
  • 80