Objective C Tutorial - Objective-C Basic Syntax






A Objective-C program consists of various strings.

A string is either a keyword, an identifier, a constant, a string literal, or a symbol.

Semicolons ;

In Objective-C program, the semicolon is a statement terminator.

Each statement must be ended with a semicolon.

For example, following are two different statements:

NSLog(@"Hello, World! \n");
return 0;

Comments

Comments are text in your Objective-C program to explain the meaning of a statement.

They are ignored by the compiler.

They start with /* and terminate with the characters */ as shown below:

/* my first program in Objective-C */

You cannot nest comments within comments.





Identifiers

An Objective-C identifier identifies a variable, a function, or other user-defined items.

An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9).

Objective-C does not allow characters such as @, $, and % within identifiers.

Objective-C is a case-sensitive programming language.

Thus, aVaraible and AVaraible are two different identifiers in Objective-C.

Here are some examples of acceptable identifiers:

_temp
a13b9
a_123
abc
j
m1
move_name
myname50
retVal




Objective-C Variables

A variable is a name given to a storage area.

Each variable in Objective-C has a specific type.

A variable definition contains a data type and a list of variables of that type.

type variable_list;

type must be a valid Objective-C data type including char, w_char, int, float, double, bool or any user-defined object, etc.,

variable_list can have one or more identifiers separated by commas.

The following code defines int, char, float and double type variables.

int    i, j, k;
char   c, ch;
float  f, salary;
double d;

Variables can be initialized by assigning an initial value in their declaration.

The initializer has an equal sign followed by a constant expression.

type variable_name = value;

The following code shows how to initialize variables during declaration.

extern int d = 3, f = 5;    // declaration of d and f. 
int d = 3, f = 5;           // definition and initializing d and f. 
byte z = 2;                 // definition and initializes z. 
char x = 'x';               // the variable x has the value 'x'.