簡體   English   中英

C程序在gcc和llvm編譯器中的行為有所不同

[英]C program behaves differently in gcc and llvm compiler

我寫了一個程序,它由main和一個函數展開組成。 問題是代碼在使用Xcode(最新版本)編譯和運行時返回了預期的結果,但是當通過gcc編譯器通過終端運行並編譯時,代碼在運行后立即卡住(沒有警告或錯誤!)。 這是我用來在終端中編譯代碼的命令:

gcc expand.c -o expand -Wall -pedantic -ansi

下面是我的代碼。 我不知道我的問題是什么:

#include <stdio.h>
#define MAX_LEN 100
#define ATOI_GAP 48

void expand(char s1[], char s2[]);

int main() 
{
int i;
char s2[MAX_LEN]; /* declare the target array */
char s1[4]; /* declare the source array */
s1[0] = 'a';
s1[1] = '-';
s1[2] = 'z';
s1[3] = '\0';

for(i = 0; i < MAX_LEN; ++i) {  /* print s2 array */
    printf("%d ", s2[i]);
} 

expand(s1, s2);

for(i = 0; s2[i] != '\0'; ++i) {  /* print s2 array */
    printf("%c ", s2[i]);
}

return 0;
}

/* the function gets string s1 of format "letterX-letterY"
and fills the "-" with the consequent letters from letterX to
letterY. For example, if s1 = "a-d", then s2 will be "abcd"*/

void expand(char s1[], char s2[]) {
int start = s2[0] = s1[0]; /* the first letter of the array s2 is the same as that of the array s1 */
int stop = s1[2]; /* determine at which letter we need to stop */
int j;
printf("inside expand");

for(j = 1; j < stop - '0' - ATOI_GAP; ++j) {
    s2[j] = ++start; /* fill in the gap */
}
s2[j] = '\0'; 
printf("finished expand");

}

發現了問題,我錯誤地運行了輸出C文件。 我以前用所有C輸出文件導出了路徑,因此要調用有問題的文件,我只是在終端中鍵入“文件名”。 但是,導出的路徑來源不正確,所以我沒有得到任何結果。 當我以“ ./filename”運行文件時,一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM