Site hosted by Angelfire.com: Build your free website today!
undefined
undefined
 City & Guilds 7261/224 Coding and Programming in C II 

C Programming Introduction


The C Programming Language

1 INTRODUCTION

C was created by Dennis Ritchie in 1972 to enable him to write a new UNIX operating system in a high-level language rather than assembler. However, because C can be ported easily from one system architecture to another, and because it allows developers to get into the heart of a machine, it has found a niche between assembler and high level programming languages.

2 PRODUCING AN EXECUTABLE FILE
2.1 Source Code

Source code is human readable and contains the statements which constitute your program. Source code files normally have a .c extension. Here is an example of a source code file, which contains a program to display the words hello world on the screen:

   #include <stdio.h>

   /* main: display message on screen */
   int main(void)
      {
      printf("hello world\n");
      return 0;
      }
We'll look at this in detail later.

2.2 Object Files

Source code is normally compiled in to .o files, or .obj files, these are object files. An object file is not human readable. It contains information that the linker needs to produce an executable file.

2.3 Library Files

Library files are normally supplied with C and they contain the object files for standard functions. They are normally stored in the subdirectory lib. They are not human readable.

2.4 Executable Files

After object files are produced, they are linked with library files, using the linker, to produce an executable file. An executable file contains executable code (machine instructions for the processor) and is not human readable. Executable files can have the file extension .EXE but it really depends on what system they're being created. In UNIX you can specify the executable file name.

3 CODE ANALYSIS

Let us now examine the program given previously. A C program consists of functions and variables. A function contains statements that specify the computing operations to be done, and variables store values used during the computation. In the previous example, there were no variables.

3.1 Function 'main'

You can give functions any names you like (subject to the syntax of identifiers and name space restrictions), but main is special. All programs must have a function called main because this is where all C programs commence execution.

3.2 #include Preprocessor Directive

The line

   #include <stdio.h>

is replaced by the file stdio.h.  Being enclosed by <> means the file is searched for in an implementation-defined way.  There are 15 header files specified by Standard C.  To include one in your source code, use the format above, specifying each one on a separate line.  Your compiler may have its own non-Standard header files, in addition to the Standard C ones.

3.3 Function Overview

A function can pass data to another function by providing a list of values, called arguments, to the function it calls.  The parentheses after the function name enclose the argument list.  In this example main expects no arguments, indicated by the word void.

Braces {} enclose the statements in a function.  The function main contains two statements,

   printf("hello world\n");
   return 0;

To call a function, cite its name followed by a parenthesized list of arguments, if any.  printf is called with the argument "hello world\n" in this case, which displays the text on screen.  If a function expects no arguments (its argument list when defined is void), call it with an empty argument list, that is, ().  How to write functions is described later in the course.

The return 0 is important.  The zero is the exit status of the program, which can be interrogated by the caller.  Zero is guaranteed to indicate success in the operating system being used.  On UNIX, 0 indicates success, so no change is made.  On DEC VAX/VMS, success is indicated by 1 in $SEVERITY, so the zero will be translated to 1.

The exit status is not an issue in this course, and not even mentioned in the syllabus.  Just get into the habit of returning zero from main for this course.

4 STRING CONSTANTS

A sequence of characters in double quotes, like "hello world\n", is called a character string, string literal or string constant.

The \n sequence represents the newline character to the compiler, which when printed advances the output to the next line.

printf never supplies a newline automatically, so our first program could have been written:

   #include <stdio.h>

   int main(void)
   {
      printf("hello ");
      printf("world");
      printf("\n");
      return 0;
   }

to produce identical output.  \n represents a single character, and is an escape sequence.  Among the other escape sequences provided by C are \t for horizontal tabulation, \b for backspace, and \" for the double quote.  For the backslash itself \\ is used.  Escape sequences are necessary in order to allow non-printable or hard-to-type characters to be represented.

Back To C Main Page

Martin O'Brien <martin.o_brien@which.net>

Last update: 9 May 1999