Mac 操作系统上的“cc -std=c99”和“c99”有什么区别?

发布于 2024-10-02 15:31:43 字数 1366 浏览 13 评论 0原文

给出以下程序:

/*  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) {
                if (!(i % 3) || !(i % 5)) {
                        unsigned long int tmp_sum = sum;
                        sum += i;
                        if (sum < tmp_sum)
                                return 0;
                }
        }

        return sum;
}

在 Mac OS 系统(Xcode 3.2.3)上,如果我使用 cc 使用 -std=c99 标志进行编译,一切似乎都正确:

nietzsche:problem_1 robert$ cc -std=c99 problem_1.c -o problem_1
nietzsche:problem_1 robert$ ./problem_1 
Sum: 233333333166666668

但是,如果我使用 c99 编译它,就会发生这样的情况:

nietzsche:problem_1 robert$ c99 problem_1.c -o problem_1
nietzsche:problem_1 robert$ ./problem_1 
Error: Unsigned Integer Wrapping.

您能解释一下这种行为吗?

Given the following program:

/*  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) {
                if (!(i % 3) || !(i % 5)) {
                        unsigned long int tmp_sum = sum;
                        sum += i;
                        if (sum < tmp_sum)
                                return 0;
                }
        }

        return sum;
}

On a Mac OS system (Xcode 3.2.3) if I use cc for compilation using the -std=c99 flag everything seems just right:

nietzsche:problem_1 robert$ cc -std=c99 problem_1.c -o problem_1
nietzsche:problem_1 robert$ ./problem_1 
Sum: 233333333166666668

However, if I use c99 to compile it this is what happens:

nietzsche:problem_1 robert$ c99 problem_1.c -o problem_1
nietzsche:problem_1 robert$ ./problem_1 
Error: Unsigned Integer Wrapping.

Can you please explain this behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒芜了季节 2024-10-09 15:31:43

c99gcc的包装器。它的存在是因为 POSIX 需要它。 c99 默认情况下会生成 32 位 (i386) 二进制文件。

ccgcc 的符号链接,因此它采用 gcc 具有的任何默认配置。默认情况下,gcc 会生成本机架构中的二进制文件,即 x86_64。

unsigned long 在 OS X 上的 i386 上为 32 位长,在 x86_64 上为 64 位长。因此,c99 将具有“无符号整数环绕”,而 cc -std=c99 则没有。

您可以通过 -W 64 标志强制 c99 在 OS X 上生成 64 位二进制文​​件。

c99 -W 64 proble1.c -o problem_1

(注意:gcc 是指实际的 gcc 二进制文件,例如 i686-apple-darwin10-gcc-4.2.1。)

c99 is a wrapper of gcc. It exists because POSIX requires it. c99 will generate a 32-bit (i386) binary by default.

cc is a symlink to gcc, so it takes whatever default configuration gcc has. gcc produces a binary in native architecture by default, which is x86_64.

unsigned long is 32-bit long on i386 on OS X, and 64-bit long on x86_64. Therefore, c99 will have a "Unsigned Integer Wrapping", which cc -std=c99 does not.

You could force c99 to generate a 64-bit binary on OS X by the -W 64 flag.

c99 -W 64 proble1.c -o problem_1

(Note: by gcc I mean the actual gcc binary like i686-apple-darwin10-gcc-4.2.1.)

南汐寒笙箫 2024-10-09 15:31:43

在 Mac OS X 下,cc 是 gcc 的符号链接(默认为 64 位),而 c99 不是(默认为 32 位)。

/usr/bin/cc-> gcc-4.2

他们对数据类型使用不同的默认字节大小。

/** sizeof.c
 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv)
{
   printf("sizeof(unsigned long int)==%d\n", (int)sizeof(unsigned long int));

   return EXIT_SUCCESS;
}

cc -std=c99 sizeof.c
./a.out
sizeof(unsigned long int)==8


c99 sizeof.c
./a.out
sizeof(unsigned long int)==4

很简单,当使用 c99 编译器时,您会溢出(也称为包装)您的整数变量。

.PMCD。

Under Mac OS X, cc is symlink to gcc (defaults to 64 bit), and c99 is not (defaults to 32bit).

/usr/bin/cc -> gcc-4.2

And they use different default byte-sizes for data types.

/** sizeof.c
 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv)
{
   printf("sizeof(unsigned long int)==%d\n", (int)sizeof(unsigned long int));

   return EXIT_SUCCESS;
}

cc -std=c99 sizeof.c
./a.out
sizeof(unsigned long int)==8


c99 sizeof.c
./a.out
sizeof(unsigned long int)==4

Quite simply, you are overflowing (aka wrapping) your integer variable when using the c99 compiler.

.PMCD.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文