1	#include <stdio.h>
2	#include <stdlib.h>
3	
4	#define ALIVE 1
5	#define DEAD 0
6	int main(int argc, char *argv[])
7	{
8	  int BobStatus = ALIVE;
9	  printf("Bob is ");
10	  BobStatus == ALIVE?printf("alive\n"):printf("dead\n");
11	  system("PAUSE");	
12	  return 0;
13	}

Purpose of the program

This very short program is designed to demonstrate the use of the shorthand if - then operator ?:. This program merely checks the value of the variable BobStatus if it is equal to the define DEAD then the program prints "Bob is dead" to the screen.

Break down of the code

Line 1 - This line starts with a '#' hash sign which indicates that it is a preprocessor directive. The #include directive instructs the preprocessor to copy the contents of the following file into this source code file at this point. The filename is surrounded with '<>' brackets this instructs the preprocessor to look in the system 'include' directory first for this file. So this line copies the contents of the file 'stdio.h' into the source code file at this point before compiling starts. The file 'stdio.h' contains the declaration of many basic input output functions that are used in C programs.

Line 2 - This line works in the same manner as line 1. The difference is that the preprocessor copies the contents of the file 'stdlib.h' in to the source code at this point before compiling starts. The file 'stdlib.h' contains the declaration of many basic functions used by C programs.

Line 3 - Is blank.

Line 4 - This line also starts with a '#' hash sign so is again a preprocessor directive. This one tells the preprocessor that everywhere it finds 'ALIVE' in the source code replace it with the value 1.

Line 5 - Like line 4 this one tells the preprocessor to replace all occurrences of 'DEAD' with the value 0.

Line 6 - This line contains the start of our definition of the main function. As you can see main returns an int value. This value indicates to the operating system whether or not the program has terminated normally. The main function also takes two parameters these contain any command line options that are sent to you program when it starts. E.g. if you start notepad from the command line you would write notepad if you want to start notepad with a file to edit you would write notepad readme.txt. readme.txt would be a command line option. This is not a subject that will be considered further in the book so you may wish to find out more yourself.

Line 7 - This line contains the opening brace of the main function. There must be a closing brace to match every opening brace and vice versa. The closing brace is on line 13.

Line 8 - This line creates a variable called BobStatus of the type int. It then sets the value to be ALIVE. When the preprocessor runs it will replace this value with 1.

Line 9 - This line uses the function printf() which is found in the stdio.h header file. This function is then instructed to print "Bob is " on to the screen. Since there is no '\n' new line marker after "Bob is " any further output will appear on the same line directly after this.

Line 10 - Next we test the value of the variable BobStatus using the ?: operator. The first part is the if statement in this case it corresponds to if(BobStatus == ALIVE). This returns true if BobStatus is equal to ALIVE or false if not. The first statement after the ? will be executed if BobStatus is equal to ALIVE. So "alive\n" will be printed out. If BobStatus is not equal to ALIVE then statement after the : will be executed. Which means that "dead\n" will be printed out. You can test this by changing line 8 to read int BobStatus = DEAD;

Line 11 - The function system("PAUSE") is used to pause the program until a key is pressed. The reason for this is that under windows the console would open, the text would be output to it then it would close. You would see little more than a flash on the screen as this happened. Try commenting out the line to see this happen.

Line 12 - The return statement is used at the end of functions to return an item of data. This item of data should match the return type declared at the start of the function. We are in the main function and as you can see in line 6 this should be an integer. The return statement marks the end of the function any code after this will not be executed your compiler will usually warn you of this when you compile the program. The value 0 is a value that tells the operating system that the program has terminated normally.

Line 13 - This line contains the closing brace of the main function.