C Programming in Mac OS X

Discussion in 'Mac Programming' started by richard.mac, May 7, 2008.

  1. richard.mac macrumors 603

    richard.mac

    Joined:
    Feb 2, 2007
    Location:
    51.50024, -0.12662
    #1
    in Uni atm im studying an intro to Ansi C Programming. in my first assignment today we used Cygwin which is a Unix emulator in Windows and Crimson which is a text editor to write C programs on the lab PCs.

    how can i do this on my Mac without using Windows? presumably this can be done on Mac OS X as its Unix based. all the lecturers use Macs as OS X's Unix core obviously appeals to them but the lecturer atm hasnt done an example of writing and compiling a program… just gone through the theory.

    i realise that i can just use Terminal in place of Cygwin, but what app do i use to write C programs that also colour codes commands to make it more user friendly? TextEdit? XCode?

    also im very new to C Programming so can anyone give me any pointers or tips that helped you when you first learnt C? thanks :)
     
  2. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #2
    use xcode

    or use text edit (or any text editor) with terminal and compile using gcc within terminal

    for color coding syntax, id reccomend text wrangler this is a great free advanced text editor
     
  3. bigandy macrumors G3

    bigandy

    Joined:
    Apr 30, 2004
    Location:
    Murka
  4. richard.mac thread starter macrumors 603

    richard.mac

    Joined:
    Feb 2, 2007
    Location:
    51.50024, -0.12662
    #4
    oh ok thanks dude.

    i assumed i needed to use Xcode but when i opened my program i couldnt compile it. so i just use Terminal to compile. Thanks!
     
  5. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #5
    yea just treat it like cygwin when compiling with the gcc flag
     
  6. richard.mac thread starter macrumors 603

    richard.mac

    Joined:
    Feb 2, 2007
    Location:
    51.50024, -0.12662
    #6
    thanks for the suggestion :) does TextMate compile? otherwise ill just stick to Terminal and XCode as its included with OS X.

    yeah the whole "gcc -ansi -pedantic xxx.c" thing… very weird!?
     
  7. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #7
    you can compile within xcode.

    however i would use a simple editor (as its way less clutter than xcode) and terminal

    (i added my text editor preference in my first post fyi)

    just my 2 cents.

    best of luck to you though!
     
  8. richard.mac thread starter macrumors 603

    richard.mac

    Joined:
    Feb 2, 2007
    Location:
    51.50024, -0.12662
    #8
    thanks

    yeh saw that. i was going to add another reply into my last post but couldnt be bothered. i always edit my post too many times!

    i just opened my program with Xcode and it colour codes. so ill just use Xcode but ill give Text Wrangler a try… actually i remember seeing Text Wrangler's icon in the lecturer's dock!
     
  9. aaronw1986 macrumors 68030

    Joined:
    Oct 31, 2006
    #9
    Smultron is a free editor which I use for my C programming. It has colored syntax as well.
     
  10. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #10
    cool deal, just didnt know if you saw that as i edited the post with that info. never know!
     
  11. richard.mac thread starter macrumors 603

    richard.mac

    Joined:
    Feb 2, 2007
    Location:
    51.50024, -0.12662
    #11
    thanks for the suggestion buddy.

    does anyone have any tips? like where you had trouble when you first learnt C. i dont think im up to scratch with my work. i need to read up more. heres a program that i wrote today.
    Screencapture 1.jpg
     
  12. richard.mac thread starter macrumors 603

    richard.mac

    Joined:
    Feb 2, 2007
    Location:
    51.50024, -0.12662
    #12
    sorry MacRumors is too fast for me! im not the fastest typist and always edit my posts to make them perfect.
     
  13. aaronw1986 macrumors 68030

    Joined:
    Oct 31, 2006
    #13
    One tough part of C is having to manually manage memory resources unlike in Java which automatically does it for you. You may not be that far into the language yet. Just a tip, printf() statements are a great way to help debug if you run into problems.
     
  14. richard.mac thread starter macrumors 603

    richard.mac

    Joined:
    Feb 2, 2007
    Location:
    51.50024, -0.12662
    #14
    i know that printf displays text on the screen e.g.

    Code:
    printf("The answer is %i", x);
    but how does this help with debugging?
     
  15. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #15
    lets you confirm if the values are what you are expecting at points in the code


    say you have a math program.

    putting printf statements at each step will read the value at that step so it can help troubleshoot in a sense
     
  16. richard.mac thread starter macrumors 603

    richard.mac

    Joined:
    Feb 2, 2007
    Location:
    51.50024, -0.12662
    #16
    ohhhh ok… kinda like working out a maths problem on paper using steps instead of punching numbers into a calculator?
     
  17. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #17
    yea. pretty just lets you isolate what part of code is messed up
     
  18. richard.mac thread starter macrumors 603

    richard.mac

    Joined:
    Feb 2, 2007
    Location:
    51.50024, -0.12662
    #18
    ok thanks for the tip guys! anyone have anymore? it would really help me…thanks guys

    also sorry for all the questions but does anyone know a good read or a wiki or something to help with C programming beginners?
     
  19. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #19
    Does that really compile with -ansi and -pedantic?

    For starters, if you leave off the return type of a function, "int" is assumed (but it's considered bad style), yet your program doesn't "return" anything. Also, if you want a main() with no arguments, you'll have to specify "void".

    So your main() becomes:

    int main(void)
    {
    // some code
    return 0;
    }

    Also, I'd check whether x1 and x2 are not (accidentally) the same value, as that'd give you a division by zero.

    Good luck!
     
  20. litesgod macrumors newbie

    Joined:
    Apr 3, 2008
    #20
    All good insights. Also- do you understand what you are doing in the code (which is really the important part if you are learning)? Looking at that code, my first question would be do you understand what scanf is doing, and why you need to use &x1, &y1 etc. Why doesn't scanf("%f %f", x1, y1) work? If you can answer that, I think you are well on your way to understanding C :)
     
  21. CaptainZap macrumors regular

    Joined:
    Jan 17, 2007
    #21
    The K&R C Programming Language book is a great way to learn C. It's amazing how much you learn from it.
     
  22. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #22
    Along the lines of debugging, learn to use assert and macros.

    in your code you can do this:

    #define DEBUG 1

    ...


    #ifdef DEBUG
    printf("Hey Debugging is on!, my Value is: %d\n", myValue);
    #endif

    When your happy with your code, you just comment out the DEBUG definition:

    /* #define DEBUG 1 */

    assert is a sanity checker. You essentially give it a something that should evaluate to true. If it's false, your program will tell you exactly where you went wrong.

    From your code:

    assert( (x2 - x1) > 0 );
     
  23. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #23
    On top of that reading comp.lang.c is another invaluable C learning resource.
     
  24. cruzrojas macrumors member

    cruzrojas

    Joined:
    Mar 26, 2007
    Location:
    USA
    #24
    As usual I would recommend giving emacs a try, if you become proficient at it you can execute commands, compile and run your programs without leaving the editor. (nor having to reach the mouse).

    About a good read that will help you with C, I came across this post
    http://www.macresearch.org/programming-c-mac-beginners-tutorial
    on macresearch recently. I skim through the masters of the void website and indeed is a good starting point for someone trying to learn C.

    Enjoy.
     
  25. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #25
    Chiming in a bit late. I don't know if you were explicitly asking for commentary on code style, but it can't hurt (other than your feelings, I guess, but it shouldn't) at this stage.

    Comments are very important, and proper commenting is an art. Everyone has their opinions, so this is only going to be an expression of my own.

    You do not need to comment variable declarations in the vast majority of the cases. If you simply cannot express what a variable will be used for via its name, then perhaps a comment is appropriate. In the case of this program, simply stating that you are declaring variables does not add clarity to the code.

    I want to be clear that I am not discouraging comments by any means, but if they only serve to muddle up the flow of the code without adding clarity, then it is generally best to omit them.

    On a related note, you may want to name m and c something more clear, like slope and y_intercept.

    The last thing is that you aren't using newlines (\n) at the end of your expressions in printf.

    Good luck, keep posting.

    -Lee
     

Share This Page