Intro to Objective-C, Pre-Lesson B: C Programming

[Just joining us? The tutorial begins HERE.]

Those of you with a strong background in C can ignore this. This lesson is meant to get those of you with no C / Java background up to speed.

When I first learned C, I did it using two primary resources: my very patient coworkers and friends, and Carl Harold’s Higher Computing for Everyone. I’ve referred countless friends to this website. It’s fantastic. If you dedicate yourself to working through the material present on that website (both the Learn to Program and Writing Basic Programs sections), you will have a solid enough foundation to continue on with this tutorial. Spend all the time you need there. We’ll be here when you return.

If you get confused, have questions, or want clarification on the topics you come across while learning C, there are several resources I’d recommend:

  • Google: I’m not kidding. Chances are, someone else has experienced your problem before, has already posted their question in a forum, and has already obtained an answer publicly. A quick google search on your error message will likely lead you to your solution. More on error messages in a bit.
  • Stack Overflow: Chances are, if you run a search on a question about programming, the answer will come up on this website.
  • CProgramming.com: Tutorials, Forums, etc.
  • The Crashspace Public Forum: A public forum where many of my nerd friends hang out to talk about tech, or help each other with projects/code/etc.

So, now you have all of these resources to get on your feet! But… where should you write all of this fine code you’re about to learn?? Why, in an IDE, of course!

IDEs are Integrated Development Environments. Development is essentially a synonym for programming. There are oodles of IDEs out there. Some are better for some languages, others better for others. Normally, I force my n00b friends to learn with gcc, a command line compiler. I do this because I think teaching them how to navigate a Unix environment and become comfortable with a command line interface is important. For you guys, however, I would ask that you use Xcode. You can code in both C and Objective-C in Xcode because, after all, Objective-C is a derivative of C.

Getting Started with in C in Xcode: HelloWorld

HelloWorld is the traditional “First Program” of each programming language. When you’re learning a new language, it’s a good idea to learn how to make the “HelloWorld” of that language first. A HelloWorld is a program so simple, it only outputs “Hello World” to the screen. Writing this simple program helps you learn how to use your IDE, before you start worrying about the code itself.

Open up Xcode, which should now be in your Applications Folder if you’re on a Mac. After it’s open, right-click on the Xcode icon in your dock, and select Options -> Keep in Dock. You’ll be using it often, so you might as well keep it handy.

In Xcode, navigate to the File Menu, and choose New -> New Project.

From the Left-Hand Menu, under Mac OS X, choose Application, and then Command Line Tool.

Name your project HelloWorld_C, give yourself a fake company name, and choose Type: Foundation.

Save your project to a sensible location on your hard drive. I would recommend making a folder called “C_Programming”, or something similar, and saving it there. After you’ve saved your file, Xcode should be open and look something like this:

The part that is most interesting to you at this junction is in the far upper left corner: main.m. Click on main.m, and your screen will now look like this:

Fascinating! This is the Obj-C Hello World program. Too fancy for us. Delete every line that does not begin with “//“. Lines that begin with //, or are surrounded by /* */ denote a comment.

Comments are notes that you can make in your code that your compiler will ignore. They are meant to be read by other programmers to help them make sense of who you are and what you’re trying to do with your code. Clear, informative commenting is key. Try to keep that in mind as you’re learning. Most code is written by teams of programmers, and it makes everyone’s life easier if they can glance through your code quickly, read the comments, and have a clear idea about what it does.

Now that you’ve deleted the Obj-C HelloWorld code, type in the following:

#include <stdio.h>

int main() {
printf("Hello, World!");

return 0;
}

Your screen should now look like this:

Now, note the three areas I highlighted in red below:

First step: Make sure the boxes in the upper right red square are all clicked. These activate the two sidebars, and the bottom bar. The bar on the bottom shows your program’s output. Now, click the Run button, highlighted in the upper left red square. This will run your program. If your program is perfect, there will be no errors, your build will succeed, and Hello, World! will show up in your output. (A bunch of other gibberish will show up in your output as well, but don’t worry about this for now.)

Did it work? Pat yourself on the back! You’ve written your first program in C!

Troubleshooting

Before I send you off on your own, one last thing: Error Messages. It’s important to know how to troubleshoot an error message, so I’ll walk you through it once. (Warning: LMGTFY.) Let’s intentionally make an error in our HelloWorld program, so we can see an error. Here, I’ve changed printf to brintf:

Trying to run this results in Xcode sending me a disheartening Build Failed message, and my program not running. Blast! But luckily, Xcode was so kind as to provide me an error message: Implicit declaration of function ‘brintf’ is invalid in C99.

This is Xcode’s way of saying “WTF is brintf?” But if you didn’t know this, running a search on the error message would help you out:

And hey, look at that! There’s StackOverflow in the top result. What’d I tell ya?

Well, there you have it, n00bs! Now that you know how to write a simple program using Xcode, head on over to Higher Computing for Everyone and learn the foundations of C! We’ll be here when you’re ready to move on to Objective-C.

Creative Commons License
Intro to Objective-C Tutorial by Michelle Leonhart is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Based on a work at www.tinwhiskers.net.


NEXT LESSON: Lesson 1: Hello World