Extension 3: ASCII, Booleans, Characters

Type char

A variable of type char is used to store a single character. It is denoted as a character, flanked by two single quotes. (Be careful—single quotes are used for characters, double quotes signify a string.) ‘a’, ‘;’, and ‘8’ all represent character types. The first is simply the letter a. The second is the semicolon symbol; it is not the same as the one used to mark the end of a line. The last example is the character 8, not the integer 8.

This Photograph is created using only the ASCI...

ASCII Art—Image via Wikipedia

Escape sequences, when enclosed in single quotes, are also considered to be char, even though they are composed of more than one symbol.

To print a character or escape sequence through NSLog, use %c.

ASCII Values

In C, chars are represented as 8-bit ASCII values. Internally, characters are stored as numbers, with a range from 0-127. This makes for interesting uses, including the following simple cypher program:


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    // A simple cypher program
    NSLog(@"Hello");	// Original message
	NSLog(@"%c%c%c%c%c", ('H' + 1), ('e' + 1), ('l' + 1), ('l' + 1), ('o' + 1));		// Cypher 1
	NSLog(@"%c%c%c%c%c", (('H' * 2) % 127), (('e' * 2) % 127), (('l' * 2) % 127), (('l' * 2) % 127), (('o' * 2) % 127));	 /* Cypher 2...
																															 note the use of the modulus operator
																															 to limit the domain */
	[pool drain];
    return 0;

The output of this program is:


One thing to note before we analyze this program—the last output only displays four characters because the ASCII value of capital H is 72. The operation results in an ASCII value of 17, which is a “special”, non-printing character (see Wikipedia link above).

Code, Demystified

The first important line prints out the NSString constant @”Hello”. Not much to it. The next line prints out five characters, as represented by the escape sequences. Looking at the arguments to NSLog, we see that we are simply taking the characters in @”Hello” and adding one to their ASCII values. If you check the output, you’ll find that Ifmmp are just the letters of Hello, but shifted “over” by one. Note that ASCII values define all capitals before lowercase, not all letters in alphabetical order; the letters A-Z come numerically before a-z.

We do the same thing on the Cypher 2 line, except that this time there is a little bit more involved. We take the ASCII value and multiply it by 2. To keep our code neat and prevent warnings, we use the modulus operator to limit the range of the result—in this case, the result can only be between 0 and 127, exactly the range of ASCII. This is another practical use of the modulus operator—to limit the range of a result to a specific set of numbers.

Note that when you write chars in your code, you must delineate them with single quotes. Those quotes do not get printed in the final output.


Boolean values are very simple—they are literally a YES or NO. In other languages, including Java and plain C, they are represented as true or false. Although initially the latter may seem more reasonable, in usage the former makes more grammatical sense. For example, in the following method call,

[self presentModalViewController:mvc animated:YES];

using the value YES makes much more sense from a reader’s perspective than true.

Booleans are simple true or false values; they are usually used to determine if a condition is true or not, and used to determine whether some code should be executed (for() statements). In Objective-C, the integer values 0 and 1 are synonymous, and can be used interchangeably (with a few exceptions). Their usage will be detailed in the next Lesson.

Previous Post
Leave a comment


  1. Objective-C Lesson 4: if() statements and Booleans « Programming for iOS
  2. Learn Objective-C in 24 Days « Programming for iOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

  • Welcome

    My goal is to make CupsOfCocoa into a beautiful source for beginners to the iPhone platform to get started. Subscribe below for more, and stay tuned!

  • Contact Me

    If you need to contact me for any reason, feel free to send me an email.
  • The Giving Spirit

    If you've found this site helpful, would you consider donating a little sum? Any amount is appreciated...Thanks so much!

  • Roadmap

  • Enter your email address to follow this blog and receive notifications of new posts by email.

    Join 220 other followers

  • Back to the Past

    October 2010
    S M T W T F S
  • Time Machine

  • You count!

  • Worldwide Stats

    free counters
%d bloggers like this: