Take the 2-minute tour ×

I'm working through a book exercise to generate some random serial number, and here's my function:

NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    '0' + random() % 10,
                                    'A' + random() % 26,
                                    '0' + random() % 10,
                                    'A' + random() % 26,
                                    '0' + random() % 10];

This works and has an output like: 2J6X7. But before, the 0s and As I had wrapped in double quotes, and an example output was 11764ıÒ˜. What did I do wrong my first time around, and why did using single quotes fix it?

share|improve this question

5 Answers 5

up vote 10 down vote accepted

The difference between single and double quotes is that double quotes declare a string, and single quotes declare a single character. Try doing this, you will get a syntax error:

'More than one character'

The reason that your code outputted a bunch of random characters is that strings are not integers like most other data types, but pointers. This means that when you type "A string", the result of the expression is the memory location that the characters are stored at. This could be anywhere in memory, depending on when you start the program. So, when you added random() to the string, it gave you a random memory address! The statement was equivalent to this in English:

Store the characters "A" in memory, and then give me the memory
address a random amount of cells later.

The random amount of cells later could be anything else in your program. The pointer was interpreted as an character (because of %c), but it wasn't meant to, giving you seemingly random output.

share|improve this answer
Quite comprehensive, thanks :) (boy is going backwards from dynamic languages tricky...) –  Alex Mcp Jun 6 '10 at 1:10
You won't necessarily get a syntax error. For example, you can do OSType aValue = 'htk1'; But it's more than likely that you will. –  Dave DeLong Jun 6 '10 at 1:41

The single quotes are giving you an ASCII representation of each character (char), to which you are adding a number. The double quotes give you a string (char *), to which adding a number really doesn't make much sense.

I might be wrong here, but I think at least the first half is right!

share|improve this answer

Single quotes are used for single character constants. Double quotes are used for strings, either C strings or NSStrings (which have '@' prefixes). When you used double quotes, the + meant pointer arithmetic. The resulting pointers were passed where characters were expected, which leads to undefined behavior. The correct version does integer arithmetic on the chars, which works as expected.

share|improve this answer

The numbers that you use for modulus controls how many ASCII characters after the one specified within the single quotes that can show up in the final string. Take a look at this table: http://www.asciitable.com/, for a reference on the ASCII numbers.

'0' + random() % 10 will give you characters from '0' to '10'

'A' + random() % 26 will give you characters from 'A' to 'Z'

Apologies if this is really obvious..

I'd like to add a question to this: is there a nice way to generate a similar string using regular expressions instead?

share|improve this answer
Hey thanks for the insight into the modulus too. As far as the other question, based on my experience with Stack Overflow, you'd be better off just asking that on your own as a unique question. –  Alex Mcp Jun 6 '10 at 3:30

Adrian's answer is right. I'm just trying to add clarification.

"A" does three things. It reserves space for 2 characters, the A and a nul terminator. It puts the codes which represent those two characters in that space. It returns the address of the first one. That address is of the type: char *

'A' just returns the value of the code for A, the same value as was put in the first memory cell above. the value is of type char.

So as you can see there is a big difference.

share|improve this answer

Your Answer


