Your First Objective-C Program
Every Objective-C program built with Foundation begins by importing the Foundation umbrella header, #import <Foundation/Foundation.h>, which brings in NSString, NSLog, and the other core Foundation classes, followed by a standard C main function that serves as the program's entry point exactly as it does in plain C. The classic first program prints a greeting using NSLog(@"Hello, World!"); instead of C's printf, because NSLog understands Objective-C's %@ format specifier for objects and automatically writes timestamp and process information alongside the message.
Cricket analogy: Like a match always starting with the toss before a single ball is bowled, an Objective-C program always starts by importing Foundation before a single line of object code runs.
Anatomy of the Program
The @autoreleasepool { } block wraps the body of main() to give Objective-C's autorelease mechanism a scope in which temporary objects, like those returned by many Foundation convenience methods, get cleaned up once the block exits, which matters even under ARC because ARC still relies on autorelease pools for certain temporary objects. Inside that block, ordinary Objective-C statements, variable declarations, method calls in bracket syntax, control flow identical to C, make up the actual logic of the program, and the function still ends with return 0; exactly like a C program.
Cricket analogy: Like a single innings (the @autoreleasepool block) containing every ball bowled in that innings and being scored as a complete unit once declared closed, @autoreleasepool contains every temporary object created within it and cleans them up once the block exits.
Compiling and Running
With Foundation imported and Xcode's Command Line Tools installed, a file named hello.m compiles from the terminal with clang -framework Foundation hello.m -o hello, producing an executable binary named hello that can then be run directly with ./hello to print the greeting to the console. If clang reports an error about a missing framework, it usually means the Command Line Tools are missing or the -framework Foundation flag was omitted, since Foundation is not linked by default the way it is inside an Xcode project target.
Cricket analogy: Like a bowler needing to actually deliver the ball down the specific line and length practiced in the nets before it counts as a wicket, a source file needs the correct -framework Foundation flag before it will actually compile and run.
#import <Foundation/Foundation.h>
int main(void) {
@autoreleasepool {
NSString *greeting = @"Hello, World!";
NSLog(@"%@", greeting);
}
return 0;
}
/*
$ clang -framework Foundation hello.m -o hello
$ ./hello
2026-07-10 09:12:03.441 hello[3021:70842] Hello, World!
*/NSLog writes to stderr, not stdout, and automatically prefixes every message with a timestamp, the process name, and the process ID before your actual text, that's why the sample output above shows a date and 'hello[3021:70842]' ahead of 'Hello, World!' rather than just the plain greeting printf would produce.
Mixing printf and NSLog carelessly is a common beginner mistake: printf expects C strings and %s, while NSLog expects Objective-C objects and the %@ format specifier, passing an NSString directly to printf's %s, or a C string to NSLog's %@, produces garbage output or a crash rather than a compile error in some cases.
- Every Foundation-based Objective-C program starts with #import <Foundation/Foundation.h>.
- main() is the entry point, identical in role to a plain C program's main().
- @autoreleasepool { } scopes temporary autoreleased objects so they're cleaned up when the block exits.
- NSLog(@"...") is Objective-C's console-output function, understanding %@ for objects.
- A single .m file compiles with clang -framework Foundation file.m -o output.
- The resulting binary runs directly from the terminal with ./output.
Practice what you learned
1. Which header must be imported before using NSLog or NSString?
2. What is the purpose of wrapping main()'s body in @autoreleasepool { }?
3. Which format specifier does NSLog use to print an Objective-C object like an NSString?
4. Where does NSLog write its output?
5. What command compiles hello.m into a runnable binary named hello from the terminal?
Was this page helpful?
You May Also Like
What Is Objective-C?
A history and overview of Objective-C, the C-based, Smalltalk-inspired language that powered Apple's platforms for decades.
Installing Xcode and Setup
How to install Xcode, set up a new Objective-C project, and compile Objective-C from the command line.
Objective-C Data Types
Objective-C's primitive types, Foundation typedefs like BOOL and NSInteger, and object types accessed through pointers.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics