How can I compile and run C in Xcode?
Our teacher is teaching use some Data Structures algorithms that are being implemented in C. He uses Visual Studio in class, but I want to be able to run simple programs that we write in Xcode.
I've tried to run the code but in Xcode 5 the options to do so are greyed out. Besides just installing Xcode, what steps should I need to take so that I can run simple C programs on my Mac?
- Open Xcode
- Select File->New->New Project
- Select macOS (OS X in older versions) -> Command line tool
- Choose C as type
- Fill out the name and the other relevant wizards parts you need
- click the main.c file to select it
And you should be good to go
In Xcode 6 & 7, you can do the following steps:
Creating the File
- Select File > New > Project > OS X
- Select Command Line Tool
- Click Next
- Input your Product Name and choose C as the language
- Click Next
- Choose where to save your project.
- Click Create
Running the Code
- Select Product > Run
- Check the console to check result.
That's it!
Check your the main.c
file and you'll find a pre-filled 'Hello World' code in the editor. Good luck!
The above answers are all great. I'll just add my $0.02. I like using Xcode where it is basically just a text editor (not the more complex Xcode...which I need to get used to!).
So, here's what you can do:
- Click on Xcode
- Ignore the pop-up window asking about a new project
- At the top of your monitor, select "File"
- Select "New"
- Select "File"
- You can then select a C file as your option.
You can skip steps 3-5 with command + N!
You'll get a basic editor you can write your code in (nice syntax coloring, basic display, etc.).
Then, you can just compile and run your program in your terminal window (your standard gcc file and ./a.out stuff).