What is the difference between `cc -std=c99` and `c99` on Mac OS?

▼魔方 西西 提交于 2019-12-04 07:23:16

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!