Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Meitou

macrumors member
Original poster
Aug 25, 2012
33
0
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();
}
 
Last edited by a moderator:
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.
 
Have you enabled the console window?

Go to the View menu, select Debug Area, then choose 'Acivate Console'
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.