What does "Objective-C is a superset of C more strictly than C++" mean exactly?
From what i read there: Why is Objective-C not very popular outside of the Apple community?
Objective-C is a superset of C (much more strictly than C++, in fact) so the issue of backward compatibility does not arise. Anything you can do in C you can do in Objective-C.
Being a superset is binary, like being pregnant. Obj-C is a superset of C, and C++ is not.
What do they mean by superset? In what way does objective-C would be more close//backward compatible to C? In what way does objective-C follow the C philosophy more closely than C++?
Can any C program be compiled without modification by a objective-C compiler (100% compatibility)?
This is more a question about programming language design and compatibility than a wars about which one is better.
I prepared a simple diagram; it is not very pretty, but hopefully gets the point across:
- Red: the set of all programs valid in C, C++, and Objective-C (relatively small)
- Green: the set of all programs valid in C and Objective-C, but invalid in C++ (even smaller)
- Gray: the set of all programs valid in Objective C and C++, but invalid in C (empty, as far as I know)
- Blue: the set of all programs valid only in Objective C (relatively large)
- Yellow: the set of all programs valid only in C++ (largest)
The set of valid C programs (in red and green) is an strict subset of the set of valid Objective C programs (blue)
-
What do they mean by superset?
They mean strict superset. Any valid C program will compile with an Objective-C compiler. Some valid C programs will not compile with a C++ compiler.
-
In what way does objective-C would be more close//backward compatible to C?
Here's a simple example:
int *foo = malloc(12);
Compiles in C and Objective-C, but not in C++. There are, of course, other examples as well.
-
In what way does objective-C follow the C philosophy more closely than C++?
All - Objective-C is a strict superset of C.
-
Can any C program be compiled without modification by a objective-C compiler (100% compatibility)?
Yes.
From the ground up, C++ has been designed as a "better C", fixing design omissions, both real and perceived, as the authors of C++ went through the language. The result of this design decision has been that X
being a valid C program did not guarantee that X
would compile, let alone run, when processed by the C++ compiler. The changes touched such basic constructs as string literals (they became const char*
), assignment of void
pointers, conversions between enum
s and integral types, semantics of compound assignment operators, and so on.
Moreover, once C99 came along, features that made it into the updated C standard were left out from the updated C++ standard. Again, very important language features were left out - most notably, designated initializers and variable-size arrays.
In contrast, Objective C has been positioned as a superset of C, requiring all valid C programs to be compilable with an Objective C compiler.
"Objective-C is a superset of C" means that every valid C program is a valid Objective-C program (with the same meaning).
It is sometimes said, although not by C++ experts, that C++ is a superset of C. This isn't accurate, which is why your quotation is making a big deal of comparing the two.
Objective C is a set of backward-compatible extensions to C. This is possible because the Objective C features are delimited in two very simple ways:
- use of the character
@
. This character is not currently used in the C language. - a simple syntactic extension for invoking methods,
[obj method:argument]
. In C, square brackets are used in a very specific way for array subscripting, and so this is invalid C syntax. Extensions which build on invalid syntax do not change the meaning of anything that is valid in the host language.
So easy to see that no program which uses Objective C extensions can be a strictly conforming ISO C program, no matter how simple. Moreover, every ISO C program can be declared, by definition, to be a valid Objective C program. Objective C can easily follow developments like C99 and C11.
On the other hand, C++ is not simply extensions to C; it is a different language which changes the meaning of some of the syntax of C. C++ and C are separately maintained, and so their relationship changes over time. For instance, C has acquired new features that are completely absent in C++, and quite likely will not go into C++, such as C99 variable-length arrays. C++ cannot easily pick up new C features.
If you write a portable C program, it should be at the same time an Objective C program. But additional care will be needed so that it is also a C++ program with the same meaning. (This practice is not unheard of, and the dialect it requires is informally known as "Clean C").
A trivial example of a C program that breaks when treated as C++ is any C program which uses a C++ keyword as an identifier, such as class
or virtual
. Objective C does not introduce any reserved keywords. It has new keywords that are introduced by the @
character, like @interface
.