Patrick Dwyer

Objective-C

Programming examples in Objective-C

Echo :: Objective-C

in News, Comparative Programming, Objective-C by patrick

This program is part of the Comparative Programming :: Echo set of examples.

The code for our Objective-C example should look startlingly familiar; almost an exact copy of the C version. The only major difference is the use of an “import” statement instead of an “include”. This is because Objective-C is a superset of ANSI-C, therefor any valid C program is also a valid Objective-C program. The examples will start to differ significanly as we take advantage of Objective-C’s object oriented characteristics, but for our echo program we can stick with a near C example.

Objective-C
#import <stdio.h>

int main(int argc, char *argv[]) {

int i;
for (i = 1; i < argc; i++) {
printf("%s ", argv[i]);
}
printf("\n");
}

Program Source: echo.m

Compiling the example

From the command line:

gcc echo.m -o echo_objc

Running the example

From the command line:

./echo_objc Hello World