objective c

Objective-C Basic Syntax

« Previous Tutorial Next Tutorial »

Here are the topics which helps you in understanding the basic syntax of an Objective-C program

Let's start with tokens in Objective-C

Tokens in Objective-C

Tokens is either a keyword, or an identifier, or a constant, or a string literals, or a symbol. An Objective-C program simply consists of various tokens. For instance, here is an statement consisting six tokens:

NSLog(@"Hello, Tokens\n");

Here are the list of individual tokens from the above statement:

Semicolons in Objective-C

Semicolon is simply a statement terminator. In other words, each individual statement in Objective-C must be ended with a semicolon (;). Here is an example having two statements

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

Comments in Objective-C

Comments are the text which are ignored by the compiler. In other words, comments are the text which can only be read by human, but ignored by the compiler.

Comments plays important role in reviewing the program later or debugging program, since these are just like helping text in an Objective-C program. Comments starts with /* and ends with */. Here is an example showing the comment:

/* this is a comment */

Here is one more example, also showing the comment:

/* This is objective-c basic syntax tutorial.
 * You are learning comments in Objective-C.
 * Comments helps in debugging the program */

Identifiers in Objective-C

Identifiers are the name used to identify a variable, function, or any other user-defined things. Here are the rules to follow for naming the identifier:

Here are some examples of valid identifiers in Objective-C:

Keywords in Objective-C

Keywords are the reserved words having their special meaning and purpose. These reserved words can't be used as constants or variable or any other identifier names. Here, the following table lists the keywords in Objective-C:

break enum register typedef
auto else long switch
char float short unsigned
case extern return union
const for signed void
default if static while
continue goto sizeof volatile
double protocol interface implementation
do int struct _Packed
NSObject NSInteger NSNumber CGFloat
weak unsafe_unretained; readwrite readonly
property nonatomic; retain strong

Whitespace in Objective-C

In Objective-C, a line containing only whitespace, possibly with a comment, is known as a blank line, so an Objective-C compiler completely ignores it. Whitespace is used in Objective-C in describing the blanks, tabs, newline, etc.

Here is an example showing whitespace in Objective-C:

int num;

« Previous Tutorial Next Tutorial »


Quick Links
Signup - Login - Give Online Test