Objective-C Program Structure

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Objective-C Program Structure

Before we study basic building blocks of the Objective-C programming language, let us look a bare minimum Objective-C program structure so that we can take it as a reference in upcoming chapters.

Objective-C Hello World Example

A Objective-C program basically consists of the following parts:

  • Preprocessor Commands

  • Interface

  • Implementation

  • Method

  • Variables

  • Statements & Expressions

  • Comments

Let us look at a simple code that would print the words "Hello World":


@interface SampleClass:NSObject
- (void)sampleMethod;

@implementation SampleClass

- (void)sampleMethod{
   NSLog(@"Hello, World! ");


int main()
   /* my first program in Objective-C */
   SampleClass *sampleClass = [[SampleClass alloc]init];
   [sampleClass sampleMethod];
   return 0;

Let us look various parts of the above program:

  1. The first line of the program #import is a preprocessor command, which tells a Objective-C compiler to include Foundation.h file before going to actual compilation.

  2. The next line @interface SampleClass:NSObject shows how to create an interface. It inherits NSObject, which is the base class of all objects.

  3. The next line - (void)sampleMethod; shows how to declare a method.

  4. The next line @end marks the end of an interface.

  5. The next line @implementation SampleClass shows how to implement the interface SampleClass.

  6. The next line - (void)sampleMethod{} shows the implementation of the sampleMethod.

  7. The next line @end marks the end of an implementation.

  8. The next line int main() is the main function where program execution begins.

  9. The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.

  10. The next line NSLog(...) is another function available in Objective-C which causes the message "Hello, World!" to be displayed on the screen.

  11. The next line return 0; terminates main()function and returns the value 0.

Compile & Execute Objective-C Program:

Now when we compile and run the program, we will get the following result.