1. AN INTRODUCTION TO C

In previous lessons, you were introduced to the basic structures of programming such as conditional statements, variables, input, subroutines, and more. Now you're going to look at a group of languages that have been very important in the history of computing and are the dominant languages in much of computing today.

For simplicity, these are referred to as the C family of languages because they have the C language in their background. The language called C was developed in the 1970s as a powerful and portable language usable at both the systems and applications level. What is called systems-level programming deals with the nitty-gritty details of hardware and operating systems, while applications-level programs run on top of operating systems and deal with user's tasks. The languages in this family share the following characteristics:

  • The core of the language is compact and depends on libraries to achieve functionality. Programs must specifically declare the libraries to be used.
  • Variables and functions must be declared with type information, and the language (usually) pays attention to the declared type in all subsequent uses.
  • There is a simple way to create complex variables that represent a collection of simpler variables.
  • Programs are built up of short statements written in plain text using the equal sign (=) to assign values to variables and expressions that look similar to standard algebra. These statements are built up into modules that can be compiled separately.

History of the C Language

The C language was developed with the original Unix operating system and still has some elements (such as terse abbreviations for commands) in common with it. Because of C's power and flexibility, it has become one of the most widely used computer languages, and compilers are available for a huge number of different systems. Because C programs usually have platform specific details; however, moving a program written for a Mac, for example, to a PC may take a bit of work.

When talking about computer languages, platform generally refers to a particular combination of hardware and operating system. You might see a product for the Windows platform, the Mac platform or the Linux - x86 platform. Unfortunately, this distinction breaks down a bit when you get to the Java platform because it runs on so many combinations of hardware and operating system.

You can read a history of the development of C in a paper by Dennis Ritchie, one of the originators of C. The language has been standardized, both in terms of basic syntax and in terms of standard libraries. C compilers for a large number of operating system are supported by the Free Software Foundation.

The power and flexibility of C come at a price, however. Complex C programs can be really hard to debug, and it may be susceptible to viruses and hacker attacks. There are a number of errors commonly made by novice (and even experienced) programmers that can be hard to find. Basically, it's easy to shoot yourself in the foot with C.

History of the C++ Language

C++ (spoken as C plus plus) builds on the C language as the name indicates. Because C++ contains all of C, C++ compilers compile strictly C programs without difficulty; however, the opposite is not true. Bjarne Stroustrup, working at Bell labs, started development of C++ in the early 1980s as described in a short history.

C++ popularized the idea of OOP (object-oriented programming) in which the variables and functions with which the programmer deals are thought of as being packaged in objects . Thinking of programs this way helps to control the complexity of very large programming projects. As OOP is a vital part of programming these days, you will see that term frequently.

Unfortunately, C++ also inherits from C the many possibilities for hard to find bugs. For example, it's perfectly possible to write a program that writes data all over itself and dies. When you see news reports of newly found security problems in some operating system, the root cause may well be this sort of problem.

One of the great strengths of C and C++ is the enormous number of libraries of already debugged code that are available. Although these are called libraries, they are really more similar to single books in a huge library. In addition to the free open source libraries, there are also commercial libraries that can be licensed.

Similar to C, the syntax and standard libraries of C++ are under the control of a standards committee of the ANSI (American National Standards Institute). When you see references to ANSI C++, it means an implementation that adheres to the standard.

History of the Java Language

Researchers at Sun Microsystems were doing "blue sky" research in the early 1990s. They felt strongly that the future of computing would depend heavily on interconnected (networked) computers and small computing objects communicating with each other. At that time, there were no languages that made communication over networks easy and portable.

In speculating on what kind of language would be required, they liked the syntax of C and the idea of OOP, but decided that many conventions in C++ would have to be removed or revised to make a more bug-resistant language. In particular, they wanted to make memory management , multithreading, and network connectivity easier. (Don't worry, these terms are all explained in later lessons.) This is where Java comes into play. Java incorporates many security features that make fatal bugs less likely.

Click this link to read about the exciting early history of Java as recalled by the originators on the third anniversary of its release. Java achieved more rapid adoption than any other language up to that time because it was perfectly positioned to catch the rising interest in the Internet, and it was designed with network communication in mind. Sun has always made Java implementations available for free, and this policy has stimulated a huge community of programmers to create their own freely available resources.

The Java language specification was developed by Sun Microsystems, and use of the name Java is still controlled by Sun. However, Sun has involved many individuals and industry partners in the subsequent development of the language and standardization proceeds through an organization called the Java Community Process rather than ANSI.

Even more than C or C++, Java comes close to the goal of running complex programs on many different kinds of hardware with no modification.

What's Next for the C Family

The success of Java has inspired Microsoft to create the latest member of the C family of languages -- C# (pronounced C sharp.) The creation of C# is a key factor in Microsoft's attempt to unify the programming environment for many different languages. It combines ideas that have been successful in Java with some additional features. At present, C# development appears to be confined to the Windows platform, but there's no reason it could not be extended to other platforms. C# is so new that a history article isn't readily available, but here is a site for beginners in the language.

2. CONTENTS OF A C PROGRAM

Here's the world's simplest C program that actually performs an action:

#include <stdio.h>
int main()
{
 printf("Hello, world!\n");
  return 0;
}

This code illustrates many important elements about the C family of languages. The following section looks at the C program in detail.

Programs that do nothing more than display the words "Hello, world" are famous exercises for learning any language. It, or something similar to it, is usually the first task most programmers try to do with a new language they learn.

Use a Library

Libraries are essential to getting anything done in C. At the start of a C program, you have to list the libraries that you want to use. The line #include <stdio.h> tells the compiler that you're going to use the standard IO library. Libraries in C and C++ are organized by means of header files that are typically named by some sort of acronym for what they do. In this case, stdio for stands for STandarD Input/Output. By long standing convention, header file names end in .h -- therefore, stdio.h .

A header file contains information on how the various functions in a library are named and the parameters they take. Header files are separate from the actual library code they describe, so the programmer must be careful to ensure that the header files match the library that is being used.

Program Conventions

By convention, C programs are written with a function named main that's executed when the program runs. Also by convention, this function must return an integer value used to indicate whether any errors are encountered. C uses int to indicate an integer number. The opening curly brace { starts the content of the main function, and the closing curly brace } shows the end of the function. The C family of languages uses pairs of curly braces to enclose logical blocks of code statements, just as you saw with the JavaScript function in the previous chapter.

Also by convention, each complete code statement is terminated by a semicolon. The statement printf("Hello, world!\n"); is a call to the printf function in the stdio library with a string constant. The return 0; statement declares that the value returned by the main function is zero, which is the convention for not having any error.

C Variable Types

In contrast with typical scripting languages, C family programming languages expect variables to be declared with a type that indicates the kind of data the variable holds. Having this knowledge in advance lets the compiler create more efficient code. Unfortunately, while Java has exact definitions about variables, some important points about type declaration in C and C++ vary between platforms.

One reason for the difference is that C has been used for many different sized computers and never got standardized in areas related to computer hardware limitations. Table 4-1 summarizes the points that cause the most trouble:

Type Abbreviation Size in C/C++ Size in Java
character char 8 bit or 16 bit always 16 bit
integer int 16 bit or 32 bit always 32 bit

Table 4-1: Some differences in variable types among C family languages.

Characters in Java always use 16 bits because Java attempts to provide for the character sets used in writing a large fraction of the languages in use in the world today. When C got started, practically all programming was done with the small character set used in English. Now programmers have to worry about I18N, so more bits are needed to represent more different characters.

C Arithmetic Expressions

If you remember your algebra, C family arithmetic expressions will look very familiar. C uses parentheses to enforce the order of evaluation as in the following:

a = ( b + c ) * ( d + e ) ;

In words, that statement says multiply the sum of b and c and by the sum of d and e , storing the result in a . C also assigns relative precedence to arithmetic operators so that if an expression is ambiguous, such as a + b * c , the multiplication is performed before the addition.

C also has some convenience notations that will not be familiar from algebra. For example, you can attach the symbols ++ or -- to a variable such as the following:

a = ++b ; // called pre-increment
a = b++ ; // called post-increment

In the first statement, the computer adds 1 to the current b variable; then puts that incremented value and in the a variable. In the second statement, the current value of b is taken and assigned to a ; then the computer adds 1 to b . There is a similar notation, called pre- and post-decrement, which, as its name indicates, subtracts values.

You'll frequently see the post-increment notation used in conjunction with arrays to automatically increment the array index. For example, suppose you want to fill an array called buffer with the successive values returned from a function called readData , and that readData is supposed to return a value of -1 to signal that the last data has been read. The following code looks plausible; the index n starts at zero and is incremented with every cycle:

int n = 0 ;
while( (ch = readData() != -1 ) {
 buffer[ n++ ] = ch ;
}

That would work as long as the buffer array was created large enough to hold the data. However, you have made no provision for detecting that n has reached the end of the array. In C, what happens then is that the data gets stuck in the middle of whatever happens to follow the array in memory. This is called buffer overflow . It's the cause of many mysterious bugs in C and C++ programs, and is exploited by virus writers.

Getting a C/C++ programming environment set up and compiling even simple programs is way beyond the scope of this course. Two widely recommended books for learning the basics of C or C++ are Ivor Horton's Beginning C++ by Ivan Horton and Schaum's Outline of Programming with C++ by John R. Hubbard.

3. A SIMPLE JAVA PROGRAM

An important driving force for the adoption of OO languages has been the rise of GUIs (graphic user interfaces) starting in the 1980s. To write the C code to handle even the simplest window is unbelievably complicated. Just as the use of subprograms allowed early C programmers to start with a library of reliable functions, using the correct OO language lets you start with some complicated capabilities, all reliably debugged and documented.

In Java, you write your entire program as a logical unit called a class . All of the code for a class is contained in a single text file and is compiled by the Java compiler into a single file of computer instructions. To create a running program, the Java runtime environment creates one or more objects using the compiled code as a template.

Here's the complete Hello World program in Java.

import java.awt.*;
public class MyFrame extends Frame {
 public static void main(String[] args){
  MyFrame mf = new MyFrame("MyFrame");
  mf.show();
 }
 public MyFrame(String title ){
  super( title );
  setSize( 200, 120 );
  add( new Label("Hello World", Label.CENTER ));
 } 
}

Similar to the other C family languages, the code starts with an import line that tells the computer you are going to be using a library called java.awt . The final * says you may use any class in that library. AWT stands for Abstract Windowing Toolkit -- or, if you're having a hard time with it, Annoying Windowing Toolkit.

Next you get to the most important line -- the class declaration . This says that your program is going to be in a class named MyFrame and that it's going to extend a class named Frame . The Frame class comes from the java.awt library and is the typical starting point for Java programs that need a GUI window.

Because MyFrame extends Frame , your class is able to use all of the cool capabilities that Frame provides, just like that. Here's a screen shot of a running MyFrame program on Windows.

The window created by a MyFrame object.
The window created by a MyFrame object.

You can drag that window around the screen, resize it, or minimize it because MyFrame inherits all of those capabilities from Frame .

Okay, back to the code. Look at the next chunk of code in MyFrame, the main function.

public static void main(String[] args){
  MyFrame mf = new MyFrame("MyFrame");
  mf.show();
}

Similar to C, the convention is that programs start by executing a function named main . If the Java runtime can't find a main function, it can't run a program. The other parts of that main function declaration are glossed over here to avoid getting bogged down in detail. You come back to this in later lessons.

The second line creates a variable of the MyFrame type that's named mf and says to create a new MyFrame object to store in mf . The keyword new that appears in this line is unique to OO languages. It says to use the MyFrame class as a template to create a new object. At this point the object has been created, but not yet shown onscreen.

The line mf.show() ; says to call the show function that belongs to the MyFrame class. You don't see a show function defined in the MyFrame class because you inherit that function from Frame , which in fact inherits from another class called Window . Your MyFrame class can use functions and variables from a complete hierarchy of classes, all the way back to the most primitive Java class called Object .

The MyFrame Constructor

The remaining code in MyFrame is a constructor . It has one parameter, the string representing the title that you want the graphic window to display:

public MyFrame(String title ){
  super( title );
  setSize( 200, 120 );
  add( new Label("Hello World", Label.CENTER ));
 }

Although it looks similar to a function declaration, a constructor is in a special category. Note that it doesn't declare a returned variable. Constructors always get invoked by the new keyword and carry out a special set of instructions. All of the classes from which the MyFrame inherits, all the way back up the hierarchy to Object , have to be called to build a MyFrame object.

This is accomplished by the super( title ) ; line that calls a constructor in the Frame class. In particular, it calls a version of the Frame constructor that takes a title string parameter. After that, the line setSize( 200, 120 ); calls a function (inherited from Frame ) to set the size in pixels of the space MyFrame takes up onscreen.

You don't inherit any method from Frame that shows a message because a Frame is intended to hold other GUI components that do handle messages. To get the words "Hello World" displayed, you create a Label object and then add it to the Frame . The Label class is part of the java.awt library you imported. Using the new keyword, you call the Label constructor with a message and a special constant that tells the new Label to show the message centered in the area it occupies in the Frame .

That's all it takes to create a custom version of a GUI window display. Of course, it doesn't do anything but show up on the desktop display, but you can add functionality by defining more functions in the MyFrame code.

4. MORE ABOUT THE EXAMPLE

By extending Frame , the MyFrame code gets access to a bunch of cool variables and functions related to its appearance and behavior. Here's a short list:

  • Background color
  • Foreground (text) color
  • Screen position and size
  • Default font used by the label
  • Whether or not the user can resize the window
  • How the mouse cursor appears when it is over the window
  • The icon that shows up on the tool bar when the window is minimized

You could build up a user interface by adding GUI elements created from classes in the java.awt library. Here are some of the elements you could create:

  • Labeled push-buttons
  • Checkboxes or radio buttons
  • Scrollbars
  • Text entry boxes or areas
  • Drop-down selection lists
  • Images
  • A menu bar with drop-down menus

Moving On

This lesson introduced you to a very important group of programming languages that got started with C. After a quick look at a program in the most primitive of this group, you made a start on the features of OOP with emphasis on programs for graphic user interfaces.

In the next lesson, you temporarily leave OOP to look at the markup languages HTML and XML. Don't worry; later lessons tie all these strands together. Before you move on, be sure to complete the assignment and quiz for this lesson. And don't forget to drop by the Message Board to see what your fellow students have to say.