Monday, July 2, 2012

CPro1 Lecture: Getting Started in C Programming


1
Getting Started
OBJECTIVE
This chapter starts you toward becoming a proficient C programmer.  You will learn
·  Why C is the best choice among programming languages.
·  The steps in the program development cycle.
·  How to write, and run your first C program.
·  About error messages generated by the compiler and linker.

A Brief History of the C Language
You may be wondering about the origin of the C language, and where it got its elegant name.  C was created by Dennis Ritchie at the Bell Telephone Laboratories in 1972.  The language was not created for the fun of it, but for a specific purpose:  designing the UNIX operating system (which is used on many minicomputers).  From the beginning, C was intended to be useful:  to allow busy programmers to get things done.
Because C is such a powerful and flexible language, its use quickly spread beyond Bell Labs.  Programmers everywhere began using it to write all sorts of programs.  Soon, however, different organizations began utilizing their own versions of C, and subtle differences between implementations started to cause programmers headaches.  In response to this problem, the American National Standards Institute (ANSI) formed a committee in 1983 to establish a standard definition of C, which became known as ANSI Standard C.   With few exceptions, every modern C compiler adheres to this standard.
Now, what about the name?  The C language is so named because its predecessor was called B.   The B language was developed by Ken Thompson, who was also at Bell Labs.  You might guess easily why it was called B.
What about this new language called C++ (pronounced C plus plus)?  You may have heard already about C++ and a new programming technique called object-oriented programming.  C++ is a superset of C, which means that C++ contains everything C does, plus new additions for object-oriented programming.  If you do go on to learn C++, almost everything you learn about C will still apply to the C++ superset.
Why Use C?
In today’s world of computer programming, there are many high-level languages to choose from, such as C, Pascal, BASIC, and Modula.  These are all excellent languages suited for most programming tasks.  Even so, there are several reasons why many computer professionals feel that C is on top of the list:
·         C is a powerful and flexible language.  What you can accomplish with C is limited only by your imagination.  The language itself places no constraints on you.  C is used for projects as diverse as operating systems, word processors, graphics, spreadsheets, and even compiler for other languages.
·         C is a portable language.  Portable means that a C program written for one computer system (an IBM, for example) can be compiled and run on another system (a DEC VAX system perhaps) with little or no modification.  Portability is enhanced by the ANSI standard for C, the set of rules for C compilers discussed earlier.
·         C is a language of few words.  C contains only a handful of terms, called keywords, which serve as a base on which the language’s functionality is built.  You might think that a language with more keywords (sometimes called reserved words) would be more powerful.  This is not true.  As you program in C, you will find it can be programmed to do any task.
·         C is modular.  C code can (and should) be written in routines called functions.  These functions can be reused in other applications or programs.  By passing pieces of information to the functions, you can create useful, reusable code.
Preparation for Programming
When creating a program in C (or for that matter, a computer program in any language), the following sequence of steps should be followed.
1.  Determine the objective(s) of the program.
2.  Determine the methods you want to use in writing the program.
3.  Create the program to solve the problem.
4.  Run the program to see the results.
The Program Development Cycle
Step 1.  Use an editor to write your source code.
   Source Code is a series of statements, or commands, used to instruct the computer to perform your desired tasks.  For example, here is a line of C source code:
  printf(“Hello, Mom!”);
   This statement instructs the computer to display the message Hello, Mom! on the screen.  By tradition, C source code files have the extension .C (for example, MYPROG.C, DATABASE.C, and so on).   Editor is a program that you can use to enter your source code.
Step 2.  Compile the program using a compiler.
   Although you may be able to understand C source code, your computer cannot.  A computer requires digital, or binary, instructions in what is called machine language.  Before your C program can run on a computer, it must be translated from source code to machine language.  This translation is performed by a program called a compiler.  The compiler takes your source code file as input and produces a disk file containing the machine language instructions that corresponds to your source code statements.  The machine language instructions created by the compiler are called object code, and the disk file containing them is called an object file.
Step 3.  Link the program using a linker.
   Part of the C language is a function library that contains object code (i.e., code that has already been compiled) for predefined functions.  A predefined function contains C code that has already been written and is supplied in a ready-to-use form with your compiler package.  The printf() function used in the previous example is a library function.
   These library functions perform frequently needed tasks, such as displaying information on-screen and reading data from disk files.  If your program uses any of these functions (and hardly a program exists that doesn’t use at least one), the object file produced when your source was compiled must be combined with object code from the function library to create the final executable program.  (Executable means that the program can be run, or executed, on your computer.)  This process is called linking and is performed by a program called (you guessed it!) a linker.
Step 4:  Execute the program.
   You should test to determine whether it functions properly.  If not, start again with step one and make modifications to your source code.
                       
Your First C Program
Listing 1.1.  HELLO.C
1: include <stdio.h>
2:
3: main()
4: {
5:   printf(“Hello, World!”);
6: }

Entering and Compiling HELLO.C
1.  Run C’s IDE (Integrated Development Environment) from the DOS prompt.
2.  Use the keyboard to type HELLO.C source code as shown in Listing 1.1 (except the line numbers).  Press Enter at the end of each line. 
3.  Save the source code.  (In Borland C++, you would press F2). You should name the file HELLO.C.
4.  Compile and link HELLO.C.  (In Borland C++, this is done in a single step by pressing F9)
5.  If you made an error typing the program, the compiler catches it and displays an error message on your screen.  In that case,  go back to step two and make any necessary corrections.
6. Your first C program should now be compiled and ready to run.  If you display a directory listing of all files named HELLO, you should see the following.
a)  HELLO.C                (which is the source code you created with your editor)
b)  HELLO.OBJ            (which contains the object code for HELLO.C)
c)  HELLO.EXE            (which is the executable program created when you compiled and linked HELLO.C).
7.  To run, or execute, HELLO.EXE, simply type hello in the DOS prompt or in Borland C++ IDE, you would press Ctrl-F9.  The message Hello, World. is displayed on your screen.

Compilation Errors
A compilation error occurs when the compiler finds something in the source code that it can’t compile.  A misspelling, typographical error, or any of a dozen things can cause the compiler to choke.  Modern compilers such as Borland C++, tells you where in your source code the error has occurred.  This makes it easier to find and correct errors in your source code.


No comments:

Post a Comment