CST8810 Computer Programming I
Maitang Mark
Updated October 12, 2005

Week 6 Notes - Beginning C Programming


What is a program?
The collection of detailed instructions that you supply when you want your computer to perform a specific task is known as a program.

C Compiler
The three most popular C/C++ compilers - Turbo C, Borland 5, and Microsoft Visual C++ are available in the College's Lab. Turbo C is strictly for 16-bit DOS applications. Borland is an excellent product for 32-bit C or C++ programs. Microsoft Visual C++ allows programmers to migrate to Windows programming at a later day. It is more complicated for simple C programs. We will use Microsoft Visual C++ for this course.

If you are looking for a free C compiler, DevC++ from Blookshed.net is recommended.

The Programming Process

  1. Plan the steps of the program.
  2. Compose (using an editor).
  3. Compile the program.
  4. Check errors, fix them with the editor and compile. (Repeat until all errors are gone.)
  5. Execute (build and run) the program. (Repeat from step 2 if results are erroneous.)
Another process which should be performed throughout the programming process is documenting the program.

Programming errors are called bugs. The process of eliminating programming errors is called debugging.

The first C program.

 /*  firstProgram.c
    It prints a message on the screen.
    CST8810 Programming I
    Your name,  date
*/
#include <stdio.h>	/* for printf() */

int  main()
{
        printf("This C stuff is easy! \n");
        return 0;
}
This program consists of :
  • A header comment
    Start the program with a multiple-line header comment. This comment must be enclosed by /* and */. It contains the physical file name, a short description of what the program does, course number, the programmer's name and a date of program creation.
  • An include statement
    #include <stdio.h>
    This is for including the library header file. All header files have a .h extension. The stdio.h contains all the input and output functions. In this program, the printf() function from stdio.h is needed for displaying the message.
  • A main() function
    int  main()				/* the function header line     */
    {					/* start of the function body   */
       printf("This C stuff is easy! \n");  /* a statement to display a message */
       return 0;				/* the last line of the function */
    }					/* end of the function body      */
    
    • Every C program starts with a main() function.
    • A function has a header line and a function body enclosed by a pair of braces: { and }.
    • The last line of the function is the return statement.
    • Each statement must be terminated by a semicolon (;).
    • In the above program, only the statement printf() produces output displays. The message to be displayed must be supplied to the printf() in double quotes.
    • The '\n' means a new line.

C Functions

C programs are made up of one or more functions. The main() is where the action starts when the program is executed (or is run). A function is referred to by the function name and followed by a pair of brackets ( ).

Function Names

Function names are mostly in lower cases. Examples are main(), addition(), subtraction(), total(), calcGrossPay(), and calc_gross_pay().
add3() is a valid function name. 3add() is not a valid function name. Function names cannot start with a digit.

The escape sequences

Escape sequences always start with a backslash (\). They provide special meaning to the C program. The most common escape sequences are the following:

      \n           Newline. Positions the cursor at the beginning of the next line.
      \t Horizontal tab. Moves the cursor to the next tab stop.
      \r Carriage return. Moves the cursor to the beginning of the
current line; does not advanced to the next line.
      \a Alert. Sounds the system bell.
      \\ Backslash. Prints a backslash character in a printf statement.
      \" Double quote. Prints a double quote character in a printf statement.
      \' Single quote. Prints a single quote character in a printf statement.

The Data Used In C

Programs process data into meaningful information. Data in C are strings, characters, and numbers.
  1. Strings

    A string is a series of characters enclosed in a pair of double quotation marks (").
    Examples are "Welcome to C world." and "The Ascii code number for A is 64.".

  2. Characters

    There are 256 characters. The Ascii code numbers for them range from 0 to 255. Some of the characters also correspond to the keys on the computer keypad. The Appendix C The Ascii Table on pages 380-389 of the text contains all 256 characters.

    The Ascii coding system is accepted by all computers. It is also expanded to 65,534 characters, called UNICODE, for internet-related processing.

    In C, characters are represented by being enclosed in a pair of single quotes (), also called apostrophes. Examples are A', +', 8', b', and /'.

    There are printable characters ( such as A', /', and those with Ascii codes ranging from 32-126 inclusive) and non-printable characters (such as the tab key, the enter key, the delete key, the backspace key, and those with Ascii codes above and beyond the range 32-126).

    C uses a list of escape sequences to represent those non-printable characters.

  3. Numbers

    You may consider that C programs deal strictly in numbers. Characters are, in a sense, numbers (or Ascii numbers). Numbers are separated into integers and floating-point numbers.

    • Integers are whole numbers with no decimal points. Characters are integers.
      Integers can be negative or positive, such as 5, -11, 0 or 33333.

    • Floating-point numbers are numbers with decimal points and fraction digits. They take more space to store. They are in essence stored in two numbers.
            123.456 is represented in 1.23456 *10^2.
            This floating-point number is stored as 123456 and 2.
      0.00123 is represented by 1.23 and -3.

Printing Data and Conversion Codes

The main purpose of the printf() function is to display C data. It has a format:

                  printf( ControlString [, data]... );

      The pair of square brackets ([ ] ) indicates an option that is not strictly required.
      In contrast, the ControlString is strictly required.
      The three dots (...) indicate that the preceding option may be repeated.

The following examples illustrate the use of printf().

      printf( "A control string with no data to take care of.\n");
      printf( "Year is %d.\n", 2004);                                     /* It prints "Year is 2004". */
      printf( "Year is %d, week is %d.\n", 2004, 5 );             /* It prints "Year is 2004, week is 5". */
      printf( "The letter %c has an Ascii code number %d.\n", 'A', 'A' );
                                          /* Characters can be printed as a character or as a number. */
      printf( "This %s costs $%f.\n", "sweater", 35.99 );       /* This sweater costs $35.990000. */
      printf( "This %s costs $%.2f.\n", "jacket", 52.50 );       /* This jacket costs $52.50.*/

You use the control string to tell the C program where and how you want the data to be displayed. The conversion codes (such as %d, %f, %c, and %s) are used to tell the C program how to print this data.
Conversion codeData Type
      %d       Integer
      %f       Floating-point number                  
      %c       Character
      %s       String

White Space

A blank space, a tab, a newline, or a series of mixed blanks, tabs, or newlines are called white space. C program treats a sequence of white spaces as one blank space.

Programming Style

The correct C statements in a program provide the C compiler the exact steps to process. All the C compiler needs is a blank between two valid statments. Providing proper amount of white spaces in a program is primarily for the benefit of programmers. In order for programmers to understand the program quickly and accurately for debugging, it is important to insert white spaces. The following two programs are accepted by the C compiler as the same.

A. The standard version:

	#include <stdio.h>	/* for printf() */
	
	main()
	{
	   printf("This C stuff is easy! \n");
	   return 0;
	}
  
B. The squeezed version:
	#include <stdio.h> /* for printf() */
	main() { printf("This C stuff is easy! \n"); return 0; }
  
For the sake of clearity, the version with alot of white spaces is recommended. The squeezed version is acceptable to C compiler, but it is not suitable for human consumption. A squeezed version of C program will receive zero marks in this course.

Updated October 12, 2005 by Maitang Mark