Take the 2-minute tour ×

I am little confused finding C style syntax in an Objective-C project (for example below syntax is not how method are defined in Objective-C, by the book). I am clear that this works since the code I have compiles without errors - but I am not sure how and why, this code is regular Objective-C .h,.m files. Can someone explain how this fits in?

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//use of round brackets 

void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, 
CGColorRef  endColor);
// C style syntax for passing params

Also this is very specific around the Core Graphics code that I have seen so far, is it allowed to write regular Objective-C methods like this also or only files with CG code...?

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

Objective-C is just a superset of C, in the same way as C++. (Both were originally implemented as preprocessors that convert the code to straight C code.) Objective-C method calls are translated to calls to the C function objc_msgSend() (and its variants) and it's possible (though tedious) to call it directly.

The gory details are spelled out here:


share|improve this answer
are there any guidelines or rules of thumb when writing code that's mix of objective-c and pure c. ( e.g memory management, casting ) etc. –  dotnetcoder Mar 2 '12 at 5:14
and does it mean , i can write a objective-c class and have a c-stye method in it , it seems a bit weird but makes sense then. –  dotnetcoder Mar 2 '12 at 5:18
Nothing beyond the rules for Objective-C, since all Objective-C code is such a "mix". Obj-C objects are just normal pointers, and can be treated as such. Just be aware of the Obj-C ownership rules if you aren't using ARC (see developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/…) and use the Obj-C API where appropriate—for example, don't call free() on an Obj-C object, use the release method. –  davehayden Mar 2 '12 at 5:27
It's possible using the low-level Obj-C stuff to add a function to a class as a method, but it's rarely necessary and I'm pretty sure Apple won't accept an app into the App Store that calls those functions. If this is all just to avoid having to learn Objective-C syntax, relax—it's a bit confusing at first (I remember it took me days to figure out why some methods started with a + character and others with a -) but it's not that hard. It'll all make sense soon! :) –  davehayden Mar 2 '12 at 5:32
add comment

Core Graphics is a C API, not Objective-C. Since Objective-C is a superset of C, any valid C code will compile just fine in .m files.

share|improve this answer
add comment

Objective-C is a superset of C, so you can define plain old C functions in a .m file, and you can call plain old C functions in a .m file using the normal C syntax.

The CGColorSpaceCreateDeviceRGB function is a plain old C function. It is part of the Core Graphics framework (also known as Quartz 2D), which has a pure C API - the API only uses plain C, not Objective-C.

You cannot define Objective-C object methods using plain old C function syntax - you must use Objective-C method syntax. And you should not try to send messages to Objective-C objects using plain old C syntax - you should use the Objective-C message sending syntax (the square brackets).

share|improve this answer
add comment

objc supports standard C that why you find c code in objc project.

As for the framework provided by Apple, if it has coreprefix,like CG standing for core graphic, it usually means it is written in C.

share|improve this answer
add comment

Your Answer


