Compiling Code
First item that needs to be covered is how to compile some code. If you are using Xcode then this is simple. You can either select the button that looks like a play, or run, button:

Or use the key combination, command-B or command-R. But if you are using the command line then you compile code by typing:
% llvm-gcc hello.c -o hello
This takes a source code file, hello.c, and compiles it into an executable file hello which can be run. This is the basics that we will follow. Whenever I state to build the project you will need to do this. And then run the executable. If you are using Xcode then the run button will compile and run the program. We will, at some point in time, be compiling more source files at the same time and they will all need to be listed before the -o hello.
Source Code -- What does it look like?
Source code is what we are going to type to tell the computer what the program will do. It's basically a list of instructions. In it's most basic form a typical file will look similar to this:
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
All C language source code has an #include statement. The reason for this is simple the basic commands that make up the C language are:
term | Description |
auto |
optional local declaration |
break |
used to exit loop and used to exit switch |
case |
choice in a switch |
char |
basic declaration of a type character |
const |
prefix declaration meaning variable can not be changed |
continue |
go to bottom of loop in for, while and do loops |
default |
optional last case of a switch |
do |
executable statement, do-while loop |
double |
basic declaration double precision floating point |
else |
executable statement, part of "if" structure |
enum |
basic declaration of enumeration type |
extern |
prefix declaration meaning variable is defined externally |
float |
basic declaration of floating point |
for |
executable statement, for loop |
goto |
jump within function to a label |
if |
executable statement |
int |
basic declaration of integer |
long |
prefix declaration applying to many types |
register |
prefix declaration meaning keep variable in register |
return |
executable statement with or without a value |
short |
prefix declaration applying to many types |
signed |
prefix declaration applying to some types |
sizeof |
operator applying to variables and types, gives size in bytes |
static |
prefix declaration to make local variable static |
struct |
declaration of a structure, like a record |
switch |
executable statement for cases |
typedef |
creates a new type name for an existing type |
union |
declaration of variables that are in the same memory locations |
unsigned |
prefix declaration applying to some types |
void |
declaration of a typeless variable |
volatile |
prefix declaration meaning the variable can be changed at any time |
while |
executable statement, while loop or do-while loop |
That is it. Those are all the reserved keywords in the C language. So let's quickly break down the commands in our simple C program. First there is #include <stdio.h>, simply put this tells the complier to include the computer file stdio.h. stdio.h is unlike our hello.c source code file that was compiled above. It is called a header file and it also had a stdio.c file but it was compiled into executable code. The header file is simply a directory. It lets you know which functions are available to use in the stdio executable file. And you will see other header files. Programmers usually call them library files, because just like a library has a lot a books that you can read and use, library files have a lot of functions that will help us extend our program beyond the above list of C commands. We will even create our own for our project.
The next line is int main(). This is the main function of our C program. This function must be created because when you run your program the main function will be called and execute the instructions that you wrote. You will also note that at the end of the line is a curly brace '{'. This is actually a token that tells the compiler that the next lines make up this function. It has a ending one too at the very end. Usually, blocks of code are wrapped up in the curly braces.
If you notice on the next line there is a function called printf(). It is being passed a string literal "hello world\n" and is terminated with a semicolon ';'. All commands are terminated with a semicolon. It signals the end of the line. Also printf is not listed in our list of commands. How can we use it? Well, if you remember the #include statement, stdio.h is where printf is found. It can take a string literal as such, and print it to the screen. We can even pass printf numbers to print to the screen. You'll see this demonstrated later in our program.
The last line that must be included is return 0; it states that, if no error was encountered then everything ran correctly. The 0 basically means 0 errors. It has to be included at the end of the main function because we told the computer that main will return a number. ???? How did the program state that? At the very beginning of main is a keyword int. int specifies that main will return an integer. Functions don't have to return anything in which case we could have typed void. But it is convention for C that main always return a int and that 0 is that int.
Conclusion
And that is pretty much the basics of a C program. What would the simple program hello.c do? Well open up a text editor or your IDE and type in the program. Save the program as hello.c and compile it. If you entered everything correctly then it should print something out to the screen. If you have a problem then just leave a comment and I'll respond. Next post I will type up the program specifications and we can start figuring out how we want to do somethings.
Also, unlike other tutorials. Instead of explaining everything up front with small programs showing all the different things you need to know; I plan to discuss what the program needs to do (the logic) and write the code, and then explain all that the code is telling the computer. This is how I understand learning a language. Most books will tell you every bit of the structure of a program and give you sample code to type but there is no end program. They are all jumbles. I don't have a problem with that it's just I rather would write a program that is fun and learn what I need to know. That means we will jump around over and into a lot of concepts. Please ask questions if you don't understand something and I will do a better job explaining it or give out links to websites that have documentation.
One great website is the C Programming.com website. It will have information on C++, which is an objected oriented language like Objective-C, but it is not a language that we will use at all because it is like Objective-C. My goal is to become more proficient at C and learn Objective-C.
Next post what do the Ants & Doodlebugs do?