相关疑难解决方法(0)

强制编译器符合C99标准

当我发现我已经使用了一段时间的匿名结构实际上只能在C11中使用,而不是C99时,我正在对我的项目进行编码,这是我想编写的标准.

给出以下代码:

struct data {
    int a;
    struct {
        int b;
        int c;
    };
};

int main()
{
    struct data d;

    d.a = 0;
    d.b = 1;
    d.c = 2;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码应仅在C11中编译(或者如果编译器扩展提供此功能并且已启用).那么让我们看看不同编译器的结果:

铿锵5

compiler:
    Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
    Target: x86_64-apple-darwin13.1.0
    Thread model: posix
command: 
    clang -std=c99 -Wall test.c -o test
result: 
    **OK**
Run Code Online (Sandbox Code Playgroud)

gcc 4.1

compiler:
    gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
command: 
    gcc -std=c99 -Wall test.c -o test
result: 
    **NOT OK**
    test.c:6: …
Run Code Online (Sandbox Code Playgroud)

c gcc c99 clang c11

8
推荐指数
2
解决办法
8181
查看次数

Mac OS上的`cc -std = c99`和`c99`有什么区别?

鉴于以下计划:

/*  Find the sum of all the multiples of 3 or 5 below 1000. */

#include <stdio.h>

unsigned long int method_one(const unsigned long int n);

int
main(int argc, char *argv[])
{
        unsigned long int sum = method_one(1000000000);
        if (sum != 0) {
                printf("Sum: %lu\n", sum);
        } else {
                printf("Error: Unsigned Integer Wrapping.\n");
        }

        return 0;
}

unsigned long int
method_one(const unsigned long int n)
{
        unsigned long int i;
        unsigned long int sum = 0;
        for (i=1; i!=n; ++i) …
Run Code Online (Sandbox Code Playgroud)

c compiler-construction xcode c99

5
推荐指数
2
解决办法
3599
查看次数

在Windows PowerShell中设置GCC的别名

我正在尝试在Windows PowerShell中设置一个"gcc99"别名,它等于"gcc -std = C99 -pedantic -Wall".我们的想法是使用更少的击键来确保GCC以c99模式运行.(我已尽力使以下帖子中的指南适应Windows PowerShell:在GCC中设置std = c99标志)

当我在设置这样的别名(下面的图1)后尝试编译时,我收到一个错误.作为参考,如果我使用扩展命令进行编译,我不会收到此错误(参见下面的图2).作为测试,我尝试将gc99设置为"gcc"的别名(没有其他值)并且它工作正常(参见下面的3).请忽略我在代码中尚未解决的许多警告:)

有什么建议?

(我不确定为什么我的标题没有出现在下面的图片中.我正在使用自动创建的图片格式,例如,对于第一张图片:" 标题 "后面跟着" 1:链接"下一行.)

图1:尝试通过设置为

图表2:通过

图表3:通过设置为

c powershell alias gcc c99

2
推荐指数
1
解决办法
1494
查看次数

可以在GCC中将std = c99设置为默认值吗?

首先,是否可以编辑GCC命令行设置,以便我不必每次都使用-std = c99标志进行编译?

其次,为什么c99不是默认行为?使用c99进行编译允许某些现代编程约定,例如在for循环中创建和初始化索引变量.

for(int i = 0; i < 10; i++) {...
Run Code Online (Sandbox Code Playgroud)

没有c99标志,这是不可能的,我将不得不在自己的线上声明,我认为这是愚蠢的.为什么GCC默认不会用C99编译?(正常好奇,不抱怨.请不要抨击)

谢谢.

c gcc c99

2
推荐指数
2
解决办法
3323
查看次数

Scanf数组限制在C?

我在C中有一些小错误:

错误:表达式必须具有常量值

我知道,这意味着我的限制必须有一个恒定的价值,但是当我遇到这种情况时,我怎么能解决这个问题呢?

printf("Type limit: ");
scanf("%i",&limit);
int arr[limit];
Run Code Online (Sandbox Code Playgroud)

谢谢.

编辑:

好的,另一个问题,对不起,如果我垃圾邮件.

    int num,limit,i;
    printf("Type limit: ");
    scanf("%i",&limit);
    int *arr = (int*)malloc(limit*sizeof(int));
    for(i=0;i<limit;i++)
    {
        printf("Type num %i: ",i);
        arr[i] = scanf("%i",&num);
    }
    system("pause");
    return 0;
Run Code Online (Sandbox Code Playgroud)

错误4错误c2109下标需要数组或指针类型

c arrays scanf variable-length-array

0
推荐指数
1
解决办法
2053
查看次数

新动态记忆"第一次"

为什么错误

#include <stdio.h>

int main(void)
{
    int *p, size, i;
    FILE *fp;

    fp = fopen("input.txt","r");
    fscanf(fp, "%d", &size);

    p = (int*)malloc(size*sizeof(int));  //error
    for (i = 0; i <size; i++)
        fscanf(fp, "%d", &p[i]);

    for (i = size-1; i>= 0; i--)
        printf("%d\n", p[i]);

    free(p);
    fclose(fp);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在ubuntu上使用"Geany"

并在Geany编译器上:

fileName.c:11:2:警告函数'malloc'的隐式声明[-Wimplicit-function-declatation] fileName.c:11:12:警告:内置函数'malloc'的不兼容隐式声明[默认启用] fileName.c:18:12:warning:函数'free'的隐式声明[-Wimplicit-function-declaration] fileName.c:18:12:警告:内置函数'free'的不兼容隐式声明[enabled-by默认]编译成功完成

c dynamic-memory-allocation

0
推荐指数
1
解决办法
249
查看次数