learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!
VB - soon here!

Search

 

 

 

 

Next Back


This lesson will show you how to:

    • Open C compiler (microsoft visual c++ 6.0)
    • Create source code
    • Edit prepared C source code
    • Compile and run source code
    • Save and retrieve source code

Q. What did the blonde's mum say to her before her date?
A. If you're not in bed by 12, come home.

Create and compile source code:

    All modern C compilers are also C++ compilers. This does not add to the complexity of creating the program, but you must be sure that you name your file with a .C extension, not the .CPP that the compiler may default to. The .C extension tells the compiler that the program is in C. If you leave the .CPP extension the compiler will assume that the program is written in C++, not all C programs are valid C++ programs. In some cases this will cause errors, in other cases this will allow incorrect code to be compiled.

This is not so important on a PC, but in the future you may be using C on a Unix machine when it will matter. The file that contains the C program is called the source file. The file that contains the compiled form of your program is called the executable file.

The Pre-Processor and standard libraries - Compiling and Linking
Compiling a C language is usually a four stage process. These processes are

  • Processing
  • Compilation to Assembly Language
  • Conversion of Assembly Language to Machine Code
  • Linking in Libraries

    The pre-processing phase is, essentially, a test substituting phase. The output of the pre-processing phase is still recognizable C language code, although parts can look very obscure. Preprocessing is controlled by various directives incorporated into the C language source code. A preprocessor directive is recognized by the fact that the first non-white space character on the source line is a "#" (hash);

The most widely used directives are "define", "include" and "if -- editif". There are commonly referred to with the preceding "#" as, for example, "#define". White space characters may appear between the "#" and the actual directive. A preprocessor directive is terminated by the end of the source line unless the last character on the source line is a backslash. A line consisting of just a "#" symbol has no effect, Unix programmers should not confuse this with the Bourne shell conventions for writing comments.

The Pre-Processor and standard libraries - The #include directive

    The include directive is used to switch compiler input into a named file. This is most commonly used to incorporate files containing prototypes for library functions, definitions of standard constants and definitions of data structures used by library functions.
The form of an include directive is either:
          #include          <file-name.h>                   used when the file is in the same folder
or
          #include           "file-name"                       used when the file is in another folder

An include directive is simply replaced by the entire contents of the named file. If the file-name is enclosed within angle brackets then the compiler looks in various standard places, if the file-name is enclosed in quotes then the compiler looks in the current directory before looking in the standard places. The actual set of places searched for files to include is implementation specific and the rules just quoted are also implementation specific.

Program Implementation
Phase 1: The creation of C program in text mode using editor.

Program keyed in     >     Editor    >     Program stored on
from document                                        disk in test mode.

Amendments can be made to the program during this phase

Phase 2: The translation of a grogram using a compiler

Programs stored on   >    Compiler  —>  program stored on disk in
disk in text mode                                      machine oriented language

If the compiler flags errors in the program then the programmer must return to phase 1 and amend the lines of text that are in error.

Phase 3: Link loading the compiler program

Program stored         >     Link/Loader    machine code program stored in memory
on disk in                   ^
machine oriented      ^
language                    <     C library

C library supplies pre-compiled routines to enable program to run. If an error occurs either there is an error in the program or the routine cannot be found in the library and progression to the next phase in not possible.

Phase 4: Program execution

data                            >     Machine code         >     results
                                          program running
                                          in memory

The program might contain run-time-errors, in which case the computer system will terminate the program per maturely. To modify the program it would be necessary to repeat the four phases again.

Example 1:

1. Since all C programs share common elements, understanding one program will help you to
understand many others. One of the simplest programs is:

#include <stdio.h>
#include <conio.h>

int main()
{

    printf("This is my first C program.");
    getch();

    return 0;

}

When this is compiled and executed, the program displays the message This is my first C program on the screen. Even though it is only six lines long it illustrates aspects common to all C programs.
The first line is:

#include <stdio.h>
This tells the compiler to read the file stdio.h and include it with the program. This file contains the information for the printf() statement.
The second line:

int main()
This begins the main() function. This is where the execution begins. All C programs are made up of one or more functions. You are looking at the simplest possible C program. There is no way to simplify this program or leave anything out. Unfortunately, the program doesn't do anything.

The word main is very important, it is the name of the first function in every C program, and must appear once, and only once. This is the point where execution is begun when the program is run .Following the main program name is a pair of parentheses '()' which are an indication to the compiler that this is a function. Before the word main is int, which tells the compiler that the function main
returns an integer value to the OS - this value is usually 0 by default. In this case it is 0 because the return 0 statement in the last line signals to return a value of 0. The two curly brackets '{}' are used to define the limits of the program itself. The actual program statements go between the two braces and in this case, there are no statements because the program does absolutely nothing.
The next line:

printf("This is my first C program.");
printf() is a standard library function which displays the string on the screen.

Finally, execution comes to the closing curly brace and stops.

Example 2:

#include <stdio.h>

int main(void)
{

    printf("This is ");
    printf("another C ");
    printf("program.");

    return 0;

}

The program also displays This is another C program on the screen. The point is that the statements are executed sequentially beginning with the opening brace and ending with the closing brace. Notice the keyword void between the brackets. This signals that the function is not initially being passed any values when execution of the function begins - include this keyword from now on, you'll understand this later on.

back to top                summary           exercises       lesson 2