问题
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