1	#include <stdio.h>
2	#include <stdlib.h>
3	
4	int main(int argc, char *argv[])
5	{
6	  char Name[100];
7	  int Age;
8	  char NameFromFile[100];
9	  int AgeFromFile;
10	  
11	  FILE * OurFile;
12	  
13	  /*Demonstrate that we can read and write to and from the console
14	  using fprintf and fscanf with the pointers stdout and stdin*/
15	  fprintf(stdout, "Please enter your first name and your age\n");
16	  fscanf(stdin, "%s%d", Name, &Age);
17	  
18	  /*Try to open a file to write to, check it is valid*/
19	  if((OurFile = fopen("Test.txt", "w")) == NULL)
20	  {
21	      /*If the file pointer is NULL complain and exit*/
22	      fprintf(stdout, "Sorry but I can't open a file to write to\n\n");
23	      exit(1);
24	  }
25	  
26	  fprintf(stdout,"\nWriting your information to file\n");
27	  /*Use the OurFile pointer to write information to the file*/    
28	  fprintf(OurFile, "%s %d",Name, Age);
29	  /*Close the file now we have finished with it*/
30	  fclose(OurFile);
31	  
32	  /*Try to open a file to read from, check it is valid*/
33	  if((OurFile = fopen("Test.txt", "r")) == NULL)
34	  {
35	      /*If the file pointer is NULL complain and exit*/
36	      fprintf(stdout, "Sorry but I can't open a file to read from\n\n");
37	      exit(1);
38	  }
39	  
40	  fprintf(stdout,"Reading your information from file\n");
41	  /*Use the OurFile pointer to read information from a file*/ 
42	  fscanf(OurFile, "%s%d",NameFromFile, &AgeFromFile);
43	  fclose(OurFile);
44	  
45	  fprintf(stdout,"\nYour name is %s, ", NameFromFile);
46	  fprintf(stdout,"and your age is %d\n", AgeFromFile);
47	  
48	  system("PAUSE");	
49	  return 0;
50	}

Purpose of the program

This program

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 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 5 - 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 48 - 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 49 - 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 50 - This line contains the closing brace of the main function.