C 程序可以编译,但执行后立即终止

标签 c linux gcc

我有一个简单的 c 程序,由 main.cselection_sort.c 组成。 我正在使用 gcc -Wall -Wextra main.c Selection_sort.c 进行编译 我没有收到警告错误,但执行时它立即终止,没有任何 printfsystem quot。我使用的是 Linux 操作系统。

//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 -c main.c
gcc -c selection_sort.c
gcc -o myprog main.o selection_sort.o
./myprog

关于C 程序可以编译,但执行后立即终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20630552/

相关文章:

linux - Bash 脚本意外错误 EOF

c++ - GCC 7 C++ 17 对折叠表达式的支持

c - 查找 1 或 0 的连续位串

c++ - 比较常规选择和准备选择性能

c - 如何从 c 头文件中提取函数原型(prototype)以实现自动完成功能

c - 一台物理设备 -> 两台字符设备

linux - 淡化未聚焦的 GNU Emacs 框架(X 窗口)

c++ - 从 gcc 函数树节点检索函数参数

linux - 用于了解可执行文件的文本、BSS 等字段的命令

C++ 字符串到 C 字符串