Objective-C Basic Syntax

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

  • tokens
  • semicolons
  • comments
  • identifiers
  • keywords
  • whitespaces

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:

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

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:

  • An identifier starts with a letter A-Z or a-z or an underscore (_) followed by zero or more letters, underscores (_), and digits (0-9)
  • Objective-C doesn't allow to name identifiers using punctuation characters such as @, $, or %, etc.
  • Objective-C is a case-sensitive programming language. Therefore, Total and total are the two different identifiers in Objective-C

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

  • total
  • sum
  • tot
  • abc
  • sum_numbers
  • find_total
  • a_34
  • name30
  • _temp
  • a
  • a3w23
  • StudentMarks

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;

Objective-C Online Test

« Previous Tutorial Next Tutorial »