Why can .c files contain blocks?

ぃ、小莉子 提交于 2019-12-24 13:42:48

问题


I think block is the Objective-C feature. Recently I am adding the block to some C source file and I found it just compile OK.

I get confused, for *.c file, should the compiler(Clang) consider it as the pure C file, why it can permit block?


回答1:


Actually Clang introduces blocks

to C and, by extension, Objective-C, C++, and Objective-C++

As you can read here in the overview.

Of course, using blocks makes your source incompatible with C11 standard but, you know, as long as your code is for Mac OS X only...

EDIT: Blocks are also available in GCC, didn't know about it.




回答2:


Blocks were added in such a manner that they are also a "C" feature in Apple's compilers.

From Apple's Blocks Programming Topics

Blocks are available in GCC and Clang as shipped with the OS X v10.6 Xcode developer tools. You can use blocks with OS X v10.6 and later, and iOS 4.0 and later. The blocks runtime is open source and can be found in LLVM’s compiler-rt subproject repository. Blocks have also been presented to the C standards working group as N1370: Apple’s Extensions to C. As Objective-C and C++ are both derived from C, blocks are designed to work with all three languages (as well as Objective-C++). The syntax reflects this goal.



来源:https://stackoverflow.com/questions/28092207/why-can-c-files-contain-blocks

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