How do I go from programming in Objective-C to coding in C?

Discussion in 'Mac Programming' started by moonman239, Jan 13, 2015.

  1. moonman239 macrumors 68000

    Mar 27, 2009
    I use Objective-C for iPhone app development. Now, I want to tackle C so that I can build modules for Python. How do I go about doing this?
  2. Sydde macrumors 68020


    Aug 17, 2009
    You just need to study up a bit on procedural C (or C++) and read up a bit on how to structure Python modules – like here, or search a bit on your own.

    Procedural C is a bit more terse than Objective-C, which can make it harder to read if you are not used to it, but it is not really an enormous leap. Not having ivars may also take a little getting used to.
  3. subsonix macrumors 68040

    Feb 2, 2008
    Get some books, K&R old but classic, C in a nutshell (O'Reilly) is good and cover C99. Also get hold of the standard, or at least a draft version to consult in moments of doubt. Then write and read lots of code, especially pointer and memory allocation intensive code to get that down, data structures, trees, queues, graphs etc. involves a lot of that if you do it in C.
  4. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    You might be coding in C already.

    Objective C is a strict superset of C. Just use Objective C, without using classes, objects and methods, and you will be using C. Then put the code in .c files instead of .m files, and you're done.

    You can do almost the same things in C as in (manually memory managed) Objective C by using structs and passing them around as explicit function parameters (instead of as the hidden parameters in Obj C methods).

    But have you checked into coding Python modules in Objective C?
  5. ArtOfWarfare macrumors G3


    Nov 26, 2007
    Just out of curiosity, why are you writing a Python module in C rather than Python?
  6. hhas macrumors regular

    Oct 15, 2007
    I'd avoid reading K&R: its coverage of C is frankly naive. You want something that isn't afraid to dissect C's many warts and shortcomings and discuss how best to work around them in real-world use.

    Honestly, if the OP already knows ObjC then the main task is learn their way around Python's C API. There's no need to muck around with malloc/free and other clunky C-isms unless you're wrapping C libraries that require their use.

    Alternatively, OP might want to consider sticking with ObjC, and just use PyObjC to access ObjC frameworks directly from Python. Not sure if PyObjC works on iOS, but it's a mature and capable technology, fully supported on OS X, and even included in OS X's own Python 2.x as standard.
  7. subsonix macrumors 68040

    Feb 2, 2008
    Sure, and that's why I said books, plural, it's still a good book to have read imho.

    Yeah, but that's just the interface, all implementation will be in C. Since the OP asked assumably there is a need for it and although you're indirectly using C from Obj-C it's not really the same if you drop all statements using [] and @. So if you want to get up to speed on the parts of C that's not really used from Obj-C normally, I think it's a good idea to focus on those exact parts.
  8. Peter Cabrera macrumors newbie

    Oct 21, 2015
    If you know about the OOP concepts than you can easily go with python or other scripting languages.

Share This Page