01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

294 APPENDIX B The basics of C<br />

compiler or support libraries. Identifiers can be of any length but are case sensitive,<br />

so fooBar, FOOBAR, and FooBar are all valid identifiers that refer to different items in<br />

an application.<br />

Following are some examples of valid variable identifiers:<br />

■<br />

■<br />

■<br />

MyVariable<br />

variable123<br />

Another_Variable<br />

And here are some examples of invalid variable identifiers:<br />

■<br />

■<br />

■<br />

■<br />

■<br />

$Amount<br />

cost(in_cents)<br />

monthly rental<br />

10timesCost<br />

int<br />

The last invalid variable identifier, int, deserves additional comment. At first glance,<br />

it may appear to pass all the rules for valid identifiers. The keyword int, however,<br />

already has a special meaning in C source code. It identifies the integer primitive data<br />

type. Keywords that already have a special meaning in a C application are called<br />

reserved words. A reserved word is an identifier that has a special meaning to the C compiler<br />

and therefore can’t be used as an identifier in your source code. Other examples<br />

of reserved words include if, return, and else, among others.<br />

B.1.1<br />

Hungarian notation<br />

Although you’re free to name your variables using any identifier that conforms to the<br />

rules previously specified, over the years a number of traditions and conflicting styles<br />

have developed regarding the best way to name identifiers. Many developers already<br />

familiar with C-based applications are aware of a convention called Hungarian notation.<br />

Hungarian notation is the concept of encoding into your variable names a description<br />

of the data type associated with the variable. This is done via a prefix that represents<br />

the data type; for example, iCount indicates an integer count, and chGender<br />

indicates a variable capable of storing a character. Table B.1 lists some of the most<br />

common prefixes you may encounter.<br />

Table B.1<br />

Common Hungarian notation prefixes for variable names in C source code<br />

Example variable identifiers<br />

Description<br />

chFoo<br />

iFoo, nBar, cApples<br />

bFlag, fBusy<br />

Character (char)<br />

Integer, number or count (int)<br />

Boolean or flag (BOOL)<br />

pszName Pointer to null terminated string (char *)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!