6.2. Getting Started

Every C program begins execution in the main function. It is the first piece of code that is run, and it's responsible for ensuring that other parts of your code are executed appropriately. Sometimes a main function can be a few lines, as is often the case in software written with the Cocoa frameworks (see Chapter 8), and at other times it may constitute the whole program. Here is a simple example to get started:

#include <stdio.h>
int main (int argc,  const char * argv[])   {
    printf("Why me? Why C?");
    return 0;
}

The first line in this snippet is called a preprocessor directive:

#include <stdio.h>

The preprocessor, which is explained in more detail later in the chapter, is a program that passes over the source code, modifying it, before the compiler is called to turn the program into binary machine code that the computer can run. This particular line tells the preprocessor to replace the directive with all the text from the file stdio.h. The file stdio.h is part of the standard C library, and the preprocessor automatically knows where to find it. This type of file is known as a header file, and it contains definitions that can be used in C programs. Header files are an important part of C, and you generally need to write many of them to define the functions and data structures in your programs.

The main function itself begins on the next line:

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

The word int at the beginning of the line is known as the return type. It ...

Get Beginning Mac OS® X Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.