r/C_Programming • • Jul 17 '26

First real C project

Hello everyone! This is my first real C project and I would like some feedback on what I can improve. It's my first attempt at a sorting algorithm (selection sort) and it is 100% AI free.

#include <stdio.h>

int main() {
    int num_len;
    printf("How many numbers to sort?\n");
    scanf("%d", &num_len);
    
    int numbers[num_len];

    printf("which numbers?\n");
    for (int i = 0; i < num_len; i++) {
        scanf("%d", &numbers[i]);
    }

    for (int i = 0; i < num_len-1; i++) {
        int iMin = i;

        for(int j = i+1; j < num_len; j++) {
            if(numbers[j] < numbers[iMin]) {
                iMin = j;
            }
        }

        if(iMin != i) {
            int temp = numbers[i];
            numbers[i] = numbers[iMin];
            numbers[iMin] = temp;
        }
    }

    for (int i = 0; i < num_len; i++) {
        printf("%d", numbers[i]);
        printf(" ");
    }
    printf("\n");
}
24 Upvotes

11 comments sorted by

View all comments

Show parent comment

4

u/LowLevelHuman Jul 18 '26

this, I would say OP could also try malloc. His example is the easiest malloc can get and a great start for it.

OP, if you want to try with malloc, you can check your program runs leak free with valgrind as such:
valgrind ./executable

valgrind also has different flags like —show-leak-kinds=all and —leak-check=full to make the report more thorough/more useful.

Valgrind is on linux. If you are not using linux I am sure there is something akin that exists