Help with C programming.

Discussion in 'Mac Programming' started by Meitou, Sep 20, 2012.

  1. Meitou, Sep 20, 2012
    Last edited by a moderator: Sep 20, 2012

    Meitou macrumors member

    Joined:
    Aug 25, 2012
    #1
    Hey guys, I just started learning C after I finished reading the book " Learn Python the hard way". I've been following tutorials from cprogramming.com but some examples are giving me errors on xcode. I'm unfamiliar with xcode as I used VIM for python. My question is, how do I compile code on xcode but run it on terminal? I tried dragging and dropping the product onto the terminal but it doesn't run. Also, does anyone know a good tutorial for OOP? I need to familiarize myself with it more. For example, this program from cprogramming.com does not give an output when ran on Xcode:
    Code:
    #include <stdio.h>
    
    struct xampl {
      int x;
    };
    
    int main()
    {  
        struct xampl structure;
        struct xampl *ptr;
    
        structure.x = 12;
        ptr = &structure; /* Yes, you need the & when dealing with 
                               structures and using pointers to them*/
        printf( "%d\n", ptr->x );  /* The -> acts somewhat like the * when 
                                       does when it is used with pointers
                                        It says, get whatever is at that memory
                                       address Not "get what that memory address
                                       is"*/
        getchar();
    }
    
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    If you've already been using VIM, then just continue to use it. Under the XCode preferences, make sure the Command Line Tools are installed. Then you can just compile and run the program from the terminal.

    1. Create test.c using the text you posted.
    2. gcc -o test test.c
    3. ./test

    Using XCode, you'll have to create a new "command line tool" project and enter this text. It's really overkill for simple stuff like this.
     
  3. robvas macrumors 68030

    Joined:
    Mar 29, 2009
    Location:
    USA
    #3
    Have you enabled the console window?

    Go to the View menu, select Debug Area, then choose 'Acivate Console'
     

Share This Page