Mac上的`cc

编程入门 行业动态 更新时间:2024-10-27 12:23:50
本文介绍了Mac上的`cc -std = c99`和`c99`有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给定以下程序:

/ *查找低于1000的所有3或5的倍数的总和。 #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\\\,sum); } else { printf(Error:Unsigned Integer Wrapping.\\\); } 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) c $ c> 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 错误:无符号整数换行。你可以解释一下这个行为吗?

$ b >解决方案

c99 是 gcc 的包装器。它存在,因为POSIX需要它。 c99 将默认生成一个32位(i386)二进制文件。

cc 是一个指向 gcc ,因此它需要任何默认配置 gcc 。 gcc 默认情况下会在本机架构中生成一个二进制文件,即x86_64。

在OS X上的i386上为32位长,在x86_64上为64位长。因此, c99 将有一个Unsigned Integer Wrapping, cc -std = c99 不会。 >

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

c99 -W 64 proble1.c -o problem_1

(注意: gcc $ c> i686-apple-darwin10-gcc-4.2.1 。)

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?

解决方案

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.)

更多推荐

Mac上的`cc

本文发布于:2023-10-08 02:23:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mac   cc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!