客观C 2.0℃的超集? [英] Is objective C 2.0 a proper superset of C?

查看:118
本文介绍了客观C 2.0℃的超集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过这个目标-C是C的超集,但客观-C 2.0?

I've heard that objective-C is a proper superset of C, but is objective-C 2.0?

我想问的原因是,要么没有,否则我误解了那句超集,因为这code是有效的C语法:

The reason I ask is that either it isn't, or I misunderstand the phrase 'proper superset', because this code is valid C syntax:

#import <stdio.h>

int main () {
    char *nil = "hello";
    printf("%s\n",nil);
}

但在Objective-C 2.0不能编译。显然,这是一个很容易可以解决的问题,但我正在写一篇论文,并认为这一点是必须指出的。

But does not compile in Objective-C 2.0. Obviously, this is an easily fixable problem, but I'm writing a paper, and feel that this is something that should be pointed out.

推荐答案

不是关键字。 中定义的 objc.h 的[在Mac OS X](和 __ DARWIN_NULL 是真的只是 NULL

nil is not a keyword. nil is defined in objc.h [on Mac OS X] (and __DARWIN_NULL is really just NULL):

#ifndef nil
#define nil __DARWIN_NULL   /* id of Nil instance */
#endif

这就是是编译型语言中没有真正的一部分,但在编译过程中使用的约定是由系统库延续。

That is, nil isn't really part of the compiled language, but a convention used during compilation that is perpetuated by the system libraries.

鸡蛋里挑骨头,很明显。你真的可以的编译Objective-C的来源,而不,虽然

Splitting hairs, obviously. You really could compile Objective-C source without nil, though.

这就好比问我能写出具有命名变量的工具紧缩同时仍使用 zlib.h 接口?。当然。但是,这将是丑陋的。

It is akin to asking "Can I write a tool that has variables named deflate while still using the zlib.h interface?". Sure. But it'll be ugly.

而且,事实上,在编译器不会自动包括objc.h 的。这样的:

And, in fact, the compiler does not automatically include objc.h. This:

#include <stdio.h>

int main() {
    int nil = 5;
    fprintf(stdout, "Hello %d\n", nil);
    return 0;
}

编译和运行在一个标准的基础工具项目就好了(在main.m文件),一旦你删除,导入基金会,因此precompiled / preFIX头,objc.h. (所以,是的,开箱时,X code模板的的原因 objc.h 来通过进口方式进口的可可/ Cocoa.h 基金会/ Foundation.h

Compiles and runs just fine in a standard Foundation tool project (in the main.m) once you remove the precompiled/prefix header that imports Foundation and, hence, objc.h. (So, yes, out of the box, the Xcode templates do cause objc.h to be imported by way of importing Cocoa/Cocoa.h or Foundation/Foundation.h.)

这篇关于客观C 2.0℃的超集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆