繁体   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