Tag Archives: C

How to create infinite loops in Objective-C and C

Infinite loops can be useful to execute some code until an exit signal is given. For example, a command line menu could be waiting forever until the user makes a valid choice. There are other approaches of course, but in case you need to know the syntax, here it is for all three loops.

Infinite for loop

Infinite while loop

Infinite do-while loop

To use the latter variations in C, replace the TRUE condition with 1.

How to read keyboard input in C

We can use the scanf() function to get user input in C. Here’s a quick implementation of scanf():

scanf() will wait for the user to press Enter before giving its returned value back to our app. We must define a variable to hold its output. We can even define multiple variables, each of which will be populated with whatever is being typed in until Enter is pressed. Consider this:

Here we wait for three string items to be added (numbers entered will be interpreted as strings). To grab a numeric value from the keyboard, we cam use %d like so (d as in decimal, variable defined as int):

Note the ampersand in front of our variable, without which scanf would populate a pointer. We can also mix and match keyboard input like this:

Demo Project

You’ll find a quick demo project on GitHub, which also serves as a template on how to setup Xcode to open Terminal and allow for keyboard input when the project is run.

How to define a struct in C

Structures (or structs) in C are something like “mini-objects”: they are wrappers around multiple variables, much like the properties of an object. Since plan C doesn’t have objects, these are as close as it gets to them, and they can be quite helpful. Apple uses them extensively (think of a CGRect for example), and it’s easy to build our own.

Here’s how we can do that.

Creating a struct

We can define a struct like this:

Here we create a struct with the name of cube and define three variables inside it. They don’t have to be of the same type, you can mix and match int, float, char and all the rest of the merry gang. The variables of a struct are called members.

When defined, a struct can be seen as a template. To use it, we need to initialise a new instance of the struct and populate the member variables, like so:

Our instance of the struct is called massiveCube, and we can refer to its member variables by dot notation.

The whole setup can be done in one step too, like this:

Note here that we still need to populate the variables. Further structs from the same template can be created as shown above.

Typedef-ing a struct

If we don’t want to create new struct instances with “struct cube myCube”, we can write a type definition for a struct.

I’ve typedef’d previous cube structure as MegaCube. Of course we can choose any name we like here, something descriptive is always a good idea. Again, our friends at Apple to this quite frequently. Now we can create new instances simply by referring to them without the struct word and only use our own definition:

This makes our code more readable and is a way of making C code “our own”.

How to break a for loop in C and Objective-C

Did you know that we can break out of a for loop before it finishes? It’s true – and it works with both regular for loops, as well as fast enumeration (for…in loops). And we can do it in either of two ways.

Let’s imagine we have an array with one thousand words. We’re only interested in finding a single one, and at that point we want to stop the loop.

Breaking with the break statement

The break statement will get us out of a for loop and continue programme execution after the finishing bracket of the loop. Here’s an example with a regular for loop:

The break statement also works with fast enumeration:

Breaking with “goto label”

Goto sounds like an old BASIC command – and we can still use it in Objective C. It doesn’t work on its own though and needs an arbitrary label to which we can direct the loop after we’ve broken from it.

Here’s how we might do this with a regular for loop:

Notice that when we declare the label where we’d like the programme to continue, we must do so with a colon (and a semi colon to finish the line).

The same principle also works with fast enumeration:

How to generate a random number in iOS

We can use the C function rand() for this:

This generates an integer between 1 and 10. Alternatively you can use the arc4random function:

This will generate a random number between 0 and 9.

One thing of note is that often this function will generate the same number multiple times in a row which may not be what you want.

If you search the Xcode documentation you’ll also find a link to Apple’s Randomization Services. Read it and weep here: http://developer.apple.com/library/ios/#documentation/Security/Reference/RandomizationReference/Reference/reference.html.