简体   繁体   English

C程序在gcc和llvm编译器中的行为有所不同

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

I wrote a program that consists of main and a function expand. 我写了一个程序,它由main和一个函数展开组成。 The problem is that the code returns the intended result when compiled and run with Xcode (latest version) however when compiled and run with gcc compiler through terminal the code is stuck immediately after running (no warnings or errors!). 问题是代码在使用Xcode(最新版本)编译和运行时返回了预期的结果,但是当通过gcc编译器通过终端运行并编译时,代码在运行后立即卡住(没有警告或错误!)。 This is the command I use to compile the code in the terminal: 这是我用来在终端中编译代码的命令:

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

Below is my code. 下面是我的代码。 I have no idea what my problem is: 我不知道我的问题是什么:

#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");

}

Found the issue, I was incorrectly running the output C file. 发现了问题,我错误地运行了输出C文件。 I previously exported the path with all my C output files, so to call the file in question I was just typing "filename" in the terminal. 我以前用所有C输出文件导出了路径,因此要调用有问题的文件,我只是在终端中键入“文件名”。 However, the exported path wasn't sourced properly so I wasn't getting any result. 但是,导出的路径来源不正确,所以我没有得到任何结果。 When I run the file as "./filename" everything is working. 当我以“ ./filename”运行文件时,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果使用 CLion 而不是直接使用 gcc 编译,C 程序的行为会有所不同。 为什么? - C Program behaves differently if compiled using CLion rather than using gcc directly. Why? 使用C89和C99编译时,C程序在运行时的行为有何不同? - What C program behaves differently in run-time when compiled with C89 and C99? 从C / C ++内部访问GCC编译器开关 - Access GCC Compiler Switches From Inside C/C++ Program 以可视c ++格式读取文件的行为与在C程序中读取行为不同 - Reading file with visual c++ form behaves differently than reading in C program 带有SIGFPE异常的程序在gdb下的行为有所不同 - Program with SIGFPE exception behaves differently under gdb 非常简单的C程序无法在gcc编译器中编译 - Extremely simple C program won't compile in gcc compiler 来自GCC编译器的C程序段错误:连接两个字符串 - c program segment error from GCC compiler: concatenate two strings C程序(GCC编译器)中的AT&T汇编语法? - AT&T Assembly Syntax in C program (GCC compiler)? 如何在 GCC 编译器的 c 程序中使用 .lib 文件方法 - How to Use .lib file method in c program on GCC Compiler 为什么GCC / Clang在不同情况下的初始化行为不同? - Why GCC/Clang behaves differently on initialization in different cases?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM