Objective-C

Appeared in:
1986
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.m, .l
Versions and implementations (Collapse all | Expand all):
Programming language

Objective-C is an object-oriented language, which extends C with Smalltalk-styled objects, messaging, strings etc.

Objective-C is not only an extension of C but its strict superset, i.e., each program written in C will compile as Objective-C program.

Elements of syntax:

Inline comments //
Nestable comments #if 0 ... #endif
Non-nestable comments /* ... */
Case-sensitivity Yes
Variable identifier regexp [_a-zA-Z][_a-zA-Z0-9]*
Variable assignment <varname> = <value>;
Variable declaration type varname;
Variable declaration with assignment type varname = value;
Grouping expressions ( )
Block { ... }
Physical (shallow) equality ==
Physical (shallow) inequality !=
Deep equality [x isEqual:y] (for objects)
Comparison < > <= >= [x compare:y] (for objects)
Function definition returntype f(type1 p1, type2 p2, ...) (function) or -(returntype)f:(type1)a g:(type2)b ... (method)
Function call f(a, b, ...) (function) or [obj f:a g:b ...] (method)
Function call with no parameters f() (function) or [obj f] (method)
Sequence ;
If - then if (condition) ...
If - then - else if (condition) ... else ... OR ( condition ? ... : ... )
Loop forever while (1) ...
While condition do while (condition) ...
Do until condition do ...while (!condition)
For each value in a numeric range, 1 increment for (int i = 1; i <= 10; i++) ...
For each value in a numeric range, 1 decrement for (int i = 10; i >= 1; i--) ...

Examples:

Factorial:

Example for versions gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25

This example uses recursive factorial definition. Note that 13! and larger causes an overflow, so the last lines of the output look like this:

13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184

#include <stdio.h>

unsigned long long factorial(unsigned long long n)
{
    if (n == 0) {
        return 1;
    } else {
        return n * factorial (n - 1);
    }
}

int main(void)
{
    int n;
    for (n = 0; n <= 16; n++) {
        printf("%i! = %lld\n", n, factorial(n));
    }
    return 0;
}

Hello, World!:

Example for versions Borland C++ Builder 6, Turbo C++ 1.01, g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25
#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    return 0;
}

Fibonacci numbers:

Example for versions gcc 3.4.5, gcc 3.4.5 (Objective-C), tcc 0.9.25

This example uses recursive definition of Fibonacci numbers. Note the difference from C++ example: loop counter must be declared outside of the loop, and printf is used for output instead of std::cout.

#include <stdio.h>

int fibonacci(int n)
{
    return ( n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2) );
}

int main(void)
{
    int n;
    for (n=1; n<=16; n++)
        printf("%d, ", fibonacci(n));
    printf("...\n");
    return 0;
}

Quadratic equation:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25

This example works both for C and C++, as well as for Objective-C which is superset of C.

#include <math.h> 
#include <stdio.h>

int main()
{
  int A, B, C;
  double D;
  printf("A = ");
  scanf("%d", &A);
  if (A == 0) {
    printf("Not a quadratic equation.\n");
    return 0;
  }
  
  printf("B = ");
  scanf("%d", &B);
  printf("C = ");
  scanf("%d", &C);

  D = B * B - 4 * A * C;
  if (D == 0) {
    printf("x = %f\n", -B / 2.0 / A);
    return 0;
  }
  
  if (D > 0) {
    printf("x1 = %f\nx2 = %f\n",
           (-B + sqrt(D)) / 2.0 / A, (-B - sqrt(D))/ 2.0 / A);
  } else {
    printf("x1 = (%f, %f)\nx2 = (%f, %f)\n",
           -B / 2.0 / A, sqrt(-D) / 2.0 / A, -B / 2.0 / A, -sqrt(-D) / 2.0 /A);
  }
  return 0;
}

CamelCase:

Example for versions gcc 3.4.5 (Objective-C)
#import <Foundation/Foundation.h>

NSString *camelCase(NSString *s) {
  return [[[s capitalizedString] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"] invertedSet]] componentsJoinedByString:@""];
}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSLog(@"%@", camelCase(@"Test Example One"));
    NSLog(@"%@", camelCase(@"exampleTwo "));
    NSLog(@"%@", camelCase(@"!!! is_this_3RD EXAMPLE?.."));

    [pool drain];
    return 0;
}