First time C Programmer, need the basics!

Discussion in 'Mac Programming' started by smokeybear, Jan 30, 2013.

  1. macrumors newbie

    Joined:
    Jan 30, 2013
    #1
    Hey guys I'm wondering if any of you can help me out. I'm using a Macbook Pro to start my school career in computer science.

    Basically I'm beginning my course in learning the basics of C, like some of variables, expressions, keywords, etc. But tomorrow is my first lab, and I'm wanting to get a head start.

    I wondering where do I begin to write this language on my Macbook. I've already looked around and couldn't find anything specific, but I've read around that I can use terminal to write it...or compile it? I'm not positive as to where to begin to write or test stuff out.

    Any words of wisdom would be greatly appreciated.
    Thanks
     
  2. macrumors 6502a

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    The easiest way is to download XCode. One option is to use the IDE built-in to XCode to write C programs. The second is to download the "Command Line Utilities" (look in preferences) and use the Terminal to run gcc like any other Unix system.

    There is a "Mac Programming" forum. You should take further programming questions there.
     
  3. macrumors 65816

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #3
  4. N-M
    macrumors newbie

    Joined:
    Jan 29, 2013
    #4
  5. thread starter macrumors newbie

    Joined:
    Jan 30, 2013
    #5
    Thanks, didn't gather all the information I wanted, but I sure did learn a lot from the advice given and with the help of Google!

    About to enter lab; going to continue to use this thread to ask questions if y'all wouldn't mind sticking around!
     
  6. macrumors newbie

    Joined:
    Mar 29, 2012
    #6
    So I'm a recent college grad and I think I have a good idea of what your professors are going to be looking for. You likely won't be using an IDE (integrated development environment) for you first courses. They will want you to learn the basics before that.

    When you write a C program, you need two things. First, a text editor that you will write your code with, and second, a compiler that will take your code and output a program. The compiler reads in the C code and compiles it, which means that it first does some magic, that you will likely learn later, to transform it into an executable binary file (a program). That executable will be specific to your Operating system and CPU architecture (x86 or x64 for intel macs).

    There is much debate over which the best text editor to use but there are two categories, terminal text editors, such as vim or emacs, and gui editors, such as smultron.

    As far as the compiler goes, most universities use the gnu compiler known as gcc (g++ for c++).

    There are several ways to get gcc on your mac but the easiest is to just install xcode, which installs gcc for you. As a side note, Apple has been using the llvm compiler a lot recently instead of gcc. That doesn't mean that you want to use llvm cause it's better, it's just a note.

    Once you have the tools, it's pretty easy.

    You create a new text file and give some name and the .c extension (Ex: something.c). Save the file
    and then compile it like so

    gcc something.c

    gcc will output the program with the name a.out

    if you type a.out, it will execute your program.

    You can look for an example of a hello world application in C if you want to try it out.

    That's the basics and enjoy.
     
  7. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    It's not quite as simple as "just install Xcode". After installing Xcode, you have to open the preferences, go to the downloads tab, and download the command line tools.

    I believe you have to actually type "./a.out", not just "a.out" for it to execute your code... if you'd like to execute it without the ./ (as you'd execute any other command line program) you'll need to put it in a special directory like /bin

    Just because I brought the special directories up... I'd like to know, where should I put the executables for command line tools I write that I'd like to be able to easily access from any directory?
     
  8. pitaya, Feb 2, 2013
    Last edited: Feb 2, 2013

    macrumors member

    Joined:
    Jun 17, 2012
    #8
    /bin isn't magically special. It's in the shell's command search path.

    /usr/local/bin is a good choice. Is it already listed in $PATH? I don't know if it exists on OS X out of the box. You could add a file to /etc/paths.d/, or an entry to /etc/paths, if you want to use it. If you'd like to run these tools out of your home directory, you could add, e.g., ~/bin to your $PATH. If you're using bash, try putting this in ~/.bash_profile:

    Code:
    export PATH="$PATH:$HOME/bin"
    You might want to check out man hier (which also includes a link to some apple documentation).
     

Share This Page