PDA

View Full Version : Programming using C




Preston019
Aug 26, 2013, 12:50 PM
Hey guys,

I'm taking a class that requires us to program using C.
I've downloaded and set up Xcode 4, but when I run the program, I only get the output, down in the lower left screen, from one of the files I have and I have two and they both work.
Also, is there a way, in Xcode, to run just one file in a project at a time? I don't want to have a ton of projects just for one file.

On another note, Xcode seems very advanced and overwhelming for me right now, is there a program that I can type my code and then run it in a shell, kind of like IDLE for Python? If not, I'd still like to be able to use Xcode.

I know I can use terminal, but I'm trying to use that as a last resort.

Thank You



djholman
Aug 26, 2013, 02:11 PM
If you want to program in C your FIRST resort should be the terminal and a program called vi.

ravenvii
Aug 26, 2013, 05:16 PM
I write C in Vim and run with the terminal.

eoblaed
Aug 26, 2013, 05:59 PM
Like the others I use terminal, vim, and make/gcc. That's a whole other kit of things to learn if you don't know them already, but I imagine Xcode also has a steep learning curve.

That's the thing about learning your first programming language: you have to learn more than just the language, you also need to learn how to use at least one development environment.

unterrible
Aug 26, 2013, 08:09 PM
You can run just one C file in Xcode by unchecking the "Target Membership" checkbox of the files you don't want to run. "Target Membership" is located in the File Inspector window, which is on the right-side by default. If you closed the File Inspector window, you can access it through the View>Utilities>Show File Inspector menu.

Watabou
Aug 26, 2013, 08:45 PM
If you want to program in C your FIRST resort should be the terminal and a program called vi.

Vim not vi (and yes, there is a difference, the main difference being that vim is more powerful and better). Barely any modern OS out there has vi installed.

If you find a 'vi' program, it's simply a symlink to vim. Sorry it's just a pet peeve of mine.

And I agree with the above, if you just go in using an IDE, you won't learn anything. For C, C++, you most likely never need an IDE. Go with vim or any other text editor. I recommend vim because it will also expose you to the terminal and gcc/clang.

If you absolutely don't want a terminal text editor, try other free text editors out there like Textmate 2, Sublime Text (free to try indefinately), or TextWrangler.

ArtOfWarfare
Aug 26, 2013, 10:42 PM
T.T

Don't use a terminal editor. Everyone suggesting that, knock it off. Those in the party of nay sayers against beginners using IDEs are right: they're too complicated. About the only worst suggestion for a beginner programmer is to use an IDE is to tell them to use a terminal editor. If they pursue that route, they're forced to learn more than an IDE + language: they're forced to learn an entirely new kernel (because chances are they've never used one without a GUI), the editor, and the language.

My suggestion: just use Sublime. It's simple and intuitive enough. Unless I'm mistaken, I believe the default download comes with a compile button in it that automatically detects your language and will compile / run your code.

It has considerable less magic than an IDE, but just a bit more than a terminal program, and it's a lot easier to learn/use.

ravenvii
Aug 27, 2013, 11:28 AM
To the above, when I said I use Vim, I actually meant MacVim. I know, I know, a sinful omission, I apologize. :p

Though the recommendation for Sublime Text is a very good one -- I really like it as well.

robvas
Aug 27, 2013, 02:52 PM
vi/vim is a huge PITA for users, especially a new user. I love it but nobody else can figure out why. Using Sublime or Komodo or Textwrangler is a much better bet

That said, if you're going to compile from the terminal it's only going to help you to learn to edit from the terminal. As well as use grep and awk and sed and...

ravenvii
Aug 27, 2013, 04:53 PM
vi/vim is a huge PITA for users, especially a new user. I love it but nobody else can figure out why. Using Sublime or Komodo or Textedit is a much better bet

That said, if you're going to compile from the terminal it's only going to help you to learn to edit from the terminal. As well as use grep and awk and sed and...

You forgot a /s there.

firewood
Aug 28, 2013, 02:20 PM
You can use Xcode to install the command-line tools, and also to create and edit your pure C programs.

Then, with the command-line tools installed by Xcode, you can use cc or gcc (really just symlinks to cfront/llvm under the current OS X) to compile and then run your programs from a Terminal shell (or even from an ssh remote login to your Mac from your iPhone or iPad !).

I prefer using vi (really a symlink to vim) for quick edits, but the Xcode editors for writing large multi-file-many-thousand line C programs.

If needed, you can switch back to creating an Xcode target for your C program if you wish to use the visual context-aware debug mode of lldb.

Preston019
Aug 28, 2013, 09:56 PM
Hey guys,

Thanks for all of the suggestions!

I've been kind of fiddling around with Xcode and I like how it tells you if something is wrong before even compiling and running it. I think I might just write my text in Xcode and just compile and run with Terminal.

I need to learn eventually, right? :rolleyes:

ArtOfWarfare
Aug 29, 2013, 05:41 AM
Hey guys,

Thanks for all of the suggestions!

I've been kind of fiddling around with Xcode and I like how it tells you if something is wrong before even compiling and running it. I think I might just write my text in Xcode and just compile and run with Terminal.

I need to learn eventually, right? :rolleyes:

Whatever floats your boat. The language doesn't change as a result of your using Xcode instead of something else. There's just the issue that novice programmers tend to fail to see where one thing ends and another begins in an IDE since everything is integrated (what the I stands for) together.

Winni
Aug 29, 2013, 09:55 AM
Hey guys,

Thanks for all of the suggestions!

I've been kind of fiddling around with Xcode and I like how it tells you if something is wrong before even compiling and running it. I think I might just write my text in Xcode and just compile and run with Terminal.

I need to learn eventually, right? :rolleyes:

Well, you already have to learn C - which, besides Objective-C, is one of the ugliest programming languages on the planet in my book. So things can hardly get any worse.

That being said, programmers should know there way around a Unix Shell and the Command Prompt on Windows. The Terminal -- IS -- your friend, on ANY operating system. Embrace it. Love it. It's the thing that gives you much more power than any GUI ever could.

As far as editors go, I won't join the decades-old vi vs Emacs debate. On Linux, I usually use "joe", which uses good-old Wordstar-compatible key bindings. That's something most of you probably never have even heard of, and if so, it probably was in a history lesson. I grew up with Wordstar on CP/M and DOS. (And SPF/PC for that matter.)

On a Mac, I'd use BBEdit. The editor comes with command line tools so that you can type bbedit myprogram.c in the Terminal and edit your file comfortably in GUI mode. TextWrangler is the "free" version of BBEdit, but I don't know if it also supports command line tools. If not, BBEdit is worth the money and probably one of the best editors out there.

On Windows, I recommend the Open Source editor "Notepad++". On Linux, Gedit is a good choice. Or Geany.

Why am I talking about multiple platforms here? Because you want to learn how to program. In C. That's a language that many people have called "portable Assembler". It was designed to work well with any platform. So you should do yourself a favor, download VirtualBox and install Windows and Linux in virtual machines and also play with the C compilers there. (And Python, since you've already mentioned it.)

If you want to use an IDE, stick to multi-platform monsters like Eclipse or Netbeans. Yes, both are written in Java. That's why they run everywhere. They're both free. That's why everybody is using them.

But frankly speaking, I'm also in the club of people who would advice you against using a full blown IDE. They only add to the complexity and that doesn't do you any favors when you're still learning how to walk. Things like BBEdit/TextWrangler or Notepad++ already offer more than you will actually need.

Keep it simple and try to have some fun with the stuff that you learn.

robvas
Aug 29, 2013, 11:06 AM
There's a book by Apress out there called 'C programming on the Mac' and it covers exactly what you're talking about. Those are the black/yellow books with the fruits on the cover.

Not sure if it covers the latest version of Xcode, though.

blackenedheart
Sep 4, 2013, 07:41 AM
There is nothing wrong with learning in XCODE 4. Just learn how to call NSLog and call your C functions from that... NSLog is a great way to see what your code is doing.

XCODE 4 is a great product and it is very powerful. That being said, you should learn Objective-C and focus less on C. Overall I think the Objective-C classes are very powerful and easy to use once you learn them.

Qaanol
Sep 4, 2013, 05:15 PM
There is nothing wrong with learning in XCODE 4. Just learn how to call NSLog and call your C functions from that... NSLog is a great way to see what your code is doing.

printf

If you are programming in C, learn to use printf

printf is a great way to see what your code is doing

ArtOfWarfare
Sep 10, 2013, 11:37 AM
printf

If you are programming in C, learn to use printf

printf is a great way to see what your code is doing

NSLog is also C, but you'll need to include different libraries to use it.

devilofspades
Sep 24, 2013, 04:44 PM
Well, you already have to learn C - which, besides Objective-C, is one of the ugliest programming languages on the planet in my book. So things can hardly get any worse...


...Why am I talking about multiple platforms here? Because you want to learn how to program. In C. That's a language that many people have called "portable Assembler". It was designed to work well with any platform. So you should do yourself a favor, download VirtualBox and install Windows and Linux in virtual machines and also play with the C compilers there. (And Python, since you've already mentioned it.)

If you want to use an IDE, stick to multi-platform monsters like Eclipse or Netbeans. Yes, both are written in Java. That's why they run everywhere. They're both free. That's why everybody is using them.

But frankly speaking, I'm also in the club of people who would advice you against using a full blown IDE. They only add to the complexity and that doesn't do you any favors when you're still learning how to walk. Things like BBEdit/TextWrangler or Notepad++ already offer more than you will actually need.

Keep it simple and try to have some fun with the stuff that you learn.

i would disagree with this whole heartedly. i won't get into the great debate over objective-c vs other oop laguages. personally i think the syntax and eligance of objective-c is second to none. i will tell you this, objective-c is c. unlike say c#, you can actually use pure c code along side objective-c code, and this is actually quite common. lower level apple apis are actually c based and have little to no objective-c in them. so by all means learn as much about c simply for the sake of knowing whats going on under the hood, and down the road grappling with more complex apple api.

if you are new to learning a language, one of the worst and most frustrting things when something goes wrong that has nothing to do with what you are learning. installing compilers, libraries, etc can all distract and get in the way of doing work. i am not saying down the road it is not good to learn, but just getting started, xcode is a canned turnkey purpose built platform for the mac that "just works". i mean windows doesn't even have a native c compiler.

for practicing c (or even c++, yes xcode does c++) using the "command line utility" template is your friend. this is your jumping off point, to just start typing code and seeing what happens. this is going to be purley text based in a console window with console output, but thats basically what c was for. unix terminals with no gui.