简体   繁体   中英

C program compiles, but terminates right after execution

I have a simple c program that consists of main.c and selection_sort.c . I am compiling with gcc -Wall -Wextra main.c selection_sort.c I get no errors of warnings, but when executed it immediately terminates without any printf or system quot . I am using Linux OS.

//main.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void selection_sort();

int main(void) {
    printf("Program started...\n");
    selection_sort();
    printf("Selection_sort has finished...\n");

    return 0;
}


//selection_sort.c
#include <stdio.h>
#include <stdlib.h>
#define size 10000

void selection_sort() {
    int i,j, array[size];

    for(i = 0; i < size; i++) {
        int num = rand() % size;
        array[i] = num;
        printf("%d ", num);
    }

    for(i = 0; i < size; i++){
        int max_index = i;
        for(j = 0; j < size; j++) {
            if(array[j] > max_index) {
                max_index = array[j];
            }
        }

        int tmp = array[i];
        array[i] = array[max_index];
        array[max_index] = tmp;
    }

    printf("\n");

    for(i = 0; i < size;i++){
        printf("%d", array[i]);
    }       
}

您应该使用编译:

    gcc -o main  main.c selection_sort.c -Wall -Wextra

Try this :

gcc -c main.c
gcc -c selection_sort.c
gcc -o myprog main.o selection_sort.o
./myprog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM