Monday, November 22, 2010

Getting Started with C

A Brief History of the C Language

C was created by Dennis Ritchie at the Bell Telephone Laboratories in 1972. The language wasn't created for the fun of it, but for a specific purpose: to design the UNIX operating system (which is used on many computers). C is considered as middle-level programming language, because it combined the power of low-level language and the elegance of high-level language. This means that C language can directly manipulate the bits, bytes and even the systems programming.

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 has the ability to adhere 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 of Bell Labs.
Why Use C?
In today's world of computer programming, there are many languages to choose from, such as C, Pascal, BASIC, and Java. These are all excellent languages suited for most programming tasks. Even so, there are several reasons why many computer professionals feel that C is at the top of the list:
  • C is a popular language preferred by professional programmers. As a result, a wide variety of C compilers and helpful accessories are available.
  • C is a portable language. Portable means that a C program written for one computer system (an IBM PC, for example) can be compiled and run on another system (MAC system, perhaps) with little or no modification. Portability is enhanced by the ANSI standard for C, the set of rules for C compilers.
  • C is a language of few words, containing only a handful of terms, called keywords, which serve as the 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 isn't true. As you program with C, you will find that it can be programmed to do any task.
  • C is modular. C code can 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.
As these features show, C is an excellent choice for your first programming language. What about C++? You might have heard about C++ and the programming technique called object-oriented programming. Perhaps you're wondering what the differences are between C and C++ and whether you should be teaching yourself C++ instead of C.
Not to worry! 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. In learning C, you are not only learning one of today's most powerful and popular programming languages, but you are also preparing yourself for object-oriented programming.
Another language that has gotten lots of attention is Java. Java, like C++, is based on C. If later you decide to learn Java, you will find that almost everything you learned about C can be applied.
Preparing to Program
You should take certain steps when you're solving a problem. First, you must define the problem. If you don't know what the problem is, you can't find a solution! Once you know what the problem is, you can devise a plan to fix it. Once you have a plan, you can usually implement it. Once the plan is implemented, you must test the results to see whether the problem is solved. This same logic can be applied to many other areas, including programming.
When creating a program in C (or for that matter, a computer program in any language), you should follow a similar sequence of steps:
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.
As an example, assume that someone asks you to write a program to determine the area inside a circle. Step 1 is complete, because you know your objective: determine the area inside a circle. Step 2 is to determine what you need to know to ascertain the area. In this example, assume that the user of the program will provide the radius of the circle. Knowing this, you can apply the formula pr2 to obtain the answer. Now you have the pieces you need, so you can continue to steps 3 and 4, which are called the Program Development Cycle.

The Program Development Cycle

The Program Development Cycle has its own steps.
1.  In the first step, you use an editor to create your source code.
2.  In the second step, you compile the source code to create an object file.
3.  In the third step, you link the compiled code to create an executable file.
4.  The fourth step is to run the program to see whether it works as originally planned.
When you save a source file, you must give it a name. The name should describe what the program does. In addition, when you save C program source files, give the file a “.C extension”. Although you could give your source file any name and extension, .C is recognized as the appropriate extension to use.

Compiling the Source Code

Although you might be able to understand C source code, your computer can't. 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, the second step in program development, 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 correspond to your source code statements.

Linking to Create an Executable File

One more step is required before you can run your program. Part of the C language is a function library that contains object code (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.

Completing the Development Cycle

Once your program is compiled and linked to create an executable file, you can run it by entering its name at the system prompt or just like you would run any other program. If you run the program and receive results different from what you thought you would, you need to go back to the first step. You must identify what caused the problem and correct it in the source code. When you make a change to the source code, you need to recompile and relink the program to create a corrected version of the executable file. You keep following this cycle until you get the program to execute exactly as you intended.
The Components of a C Program

Your First C Program

This program displays the words Hello, World! on-screen.
#include <stdio.h>
main()
  {
     printf("Hello, World!\n");
  }

The Program's Components

#include

The #include directive instructs the C compiler to add the contents of an include file into your program during compilation. An include file is a separate disk file that contains information needed by your program or the compiler. Several of these files (sometimes called header files) are supplied with your compiler. You never need to modify the information in these files; that's why they're kept separate from your source code. Include files should all have an .H extension (for example, STDIO.H).

 

main()

The only component that is required in every C program is the main() function. In its simplest form, the main() function consists of the name main followed by a pair of empty parentheses (()) and a pair of braces ({}). Within the braces are statements that make up the main body of the program. Under normal circumstances, program execution starts at the first statement in main() and terminates at the last statement in main().

 

printf()

The printf() statement is a library function that displays information on-screen. The printf() statement can display a simple text message or a message and the value of one or more program variables.

 

Braces

You use braces ({}) to enclose the program lines that make up every C function--including the main() function. A group of one or more statements enclosed within braces is called a block. C has many uses for blocks.

 

A Short C Program

 

This is a very simple program. All it does is input two numbers from the keyboard and calculate their product.
1:  /* Program to calculate the product of two numbers. */
2:  #include <stdio.h>
3:
4:  int a,b,c;
5:
6:  main()            
7:  {              
8:     /* Input the first number */              
9:     printf("Enter a number between 1 and 100: ");              
10:     scanf("%i", &a);                 
11:
12:     /* Input the second number */                 
13:     printf("Enter another number between 1 and 100: ");              
14:     scanf("%i", &b);          
15:
16:     /* Calculate and display the product */                
17:     c = a*b;             
18:     printf ("%i times %i = %i\n", a, b, c);               
19:
20:     
21: }

Program Comments (Lines 1, 8, 12, and 16)

Any part of your program that starts with /* and ends with */ is called a comment. The compiler ignores all comments, so they have absolutely no effect on how a program works. You can put anything you want into a comment, and it won't modify the way your program operates. A comment can span part of a line, an entire line, or multiple lines. Here is an example:
/* A single-line comment */
int a,b,c; /* A partial-line comment */

The Variable Definition (Line 4)

A variable is a name assigned to a data storage location. Your program uses variables to store various kinds of data during program execution. In C, a variable must be defined before it can be used. A variable definition informs the compiler of the variable's name and the type of data it is to hold. In the sample program, the definition on line 4, int a,b,c;, defines three variables--named a, b, and c--that will each hold an integer value.

scanf()

The scanf() statement (lines 10 and 14) is another library function. It reads data from the keyboard and assigns that data to one or more program variables.

No comments:

Post a Comment