Useful Free Resources

Wednesday, August 19, 2015

The C Language : History and Standards(till C99)

Dennis Ritchie designed the C language at Bell Laboratories  from 1969 to 1973, and its ancestry is traced from
  1. ALGOL 60 (1960)
  2. Cambridge's CPL (1963)
  3. Martin Richards's BCPL (1967)
  4. and Ken Thompson' s B language (1970) at Bell Labs 
 The American National Standards Institute (ANSI) ratified the ANSI C standard in 1989, and this standardization later became the responsibility of the International Standards Organization (ISO).
The standards define the C language and a set of library functions known as the C standard library.
Kernighan and Ritchie describe ANSI C in their classic book, which is known affectionately as “K&R”. In Ritchie’s words ,
C is “quirky, flawed, and an enormous success.” 
So why the success?

  • C was closely tied with the Unix operating system. C was developed from the beginning as the system programming language for Unix. Most of the Unix kernel, and all of its supporting tools and libraries, were written in C. As Unix became popular in universities in the late 1970s and early 1980s, many people were exposed to C and found that they liked it. Since Unix was written almost entirely in C, it could be easily ported to new machines, which created an even wider audience for both C and Unix
  • C is a small, simple language.The design was controlled by a single person, rather than a committee, and the result was a clean, consistent design with little baggage. The K&R book describes the complete language and standard library, with numerous examples and exercises, in only 261 pages.
    The simplicity of C made it relatively easy to learn and to port to different computers.
  • C was designed for a practical purpose. C was designed to implement the Unix operating system. Later, other people found that they could write the programs they wanted, without the language
    getting in the way.


Although C is a general-purpose programming language, it has traditionally been used for (operating) systems programming. In particular, the popular UNIX operating system was originally written in C.
C's popularity is due to many factors. It is a:
  • small
  • efficient, yet powerful programming language 
  • with a rich(in 70's) run-time library
  • It provides precise control over the computer without a lot of hidden mechanisms
  • Since it has been standardized for over 25 years, programmers are comfortable with it. It is generally easy to write C programs that will be portable across different computing systems in different countries with different languages
  • Finally, there is a lot of legacy C code out there that is being modified and extended
Starting in the late 1990s, C's popularity began to be eclipsed by its "big brother," C++. However. there is still a loyal following for the C language, and it continues to be popular where programmers do not need the features in C++ or where the overhead of C++ is not welcome.

C has withstood the test of time. It remains a language in which the experienced programmer can write quickly and well. Millions of lines of code testify to its usefulness.

C is the language of choice for system-level programming, and there is a huge installed base of application-level programs as well.

However, it is not perfect for all programmers and all situations.
  1. C pointers are a common source of confusion and programming errors. 
  2. C also lacks explicit support for useful abstractions such as classes, objects, and exceptions. Newer languages such as C++ and Java address these issues for application-level programs.


THE EVOLUTION OF C

In the mid of 80's , the C language was in widespread use, but there was no official standard or precise description of the language. The de facto standards were the C compilers being used.
C became an international standard in 1989, was revised in 1994, and underwent a major revision in 1999.
Unfortunately simply changing the definition of a language does not automatically alter the hundreds of millions of lines of C program code in the world.

Traditional C

The original C language description is the first edition of the book,
The C Programming Language, by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1978), usually referred to as "K&R." 
After the book was published, the language continued to evolve in small ways; some features were added and some were dropped.
We refer to the consensus definition of C in the early 1980's as traditional C, the dialect before the standardization process.
Of course, individual C vendors had their own extensions to traditional C, too.

Standard C (1989)

Realizing that standardization of the language would help C become more widespread in commercial programming, the American National Standards Institute (ANSI) formed a committee in 1982 to propose a standard for C and its run-time libraries. That committee X3J11 (now NCITS J11 ), was chaired by Jim Brodie and produced a standard formally adopted in 1989 as American National Standard X3.159-1989, or "ANSI C."

Recognizing that programming is an international activity, an international standardization group was created as ANSI C was being completed. 
ISO/IEC JTCl/SC22/WG14 under by P. J. Plauger turned the ANSI standard into an international standard, lSO/IEC 9899:1990, making only minor editorial changes. The ISO/IEC standard was thereafter adopted by ANSI, and people referred to this common standard as simply "Standard C" Since that standard would eventually be changed, we refer to it as Standard C (1989), or simply "C89".
Some of the changes from traditional C to C89 included:
• The addition of a truly standard library.
• New preprocessor commands and features.
• Function prototypes, which let you specify the argument types in a function declaration.
• Some new keywords, including const, volatile, and signed.
• Wide characters, wide strings, and multibyte characters.
• Many smaller changes and clarifications to conversion rules, declarations, and type checking.

Standard C (1995)

As a normal part of maintaining the C standard , WG14 produced two Technical Corrigenda (bug fixes) and an Amendment (extension) to C89. Taken together, these made a relatively modest change to the Standard mostly by adding new libraries
The result is what we call either "C89 with Amendment I" or "C95." 
The changes to C89 included:
• three new standard library headers: iso646.h, wctype.h, and wchar.h
• several new tokens and macros used as replacements for operators and punctuation characters not found in some countries's character sets
• some new formatting codes for the printf/scanf family of functions, and
• a large number of new functions, plus some types and constants, for multibyte and wide characters.

Standard C (1999)

ISO/IEC standards must be reviewed and updated on a regular basis. In 1995 WGI4 began work on a more extensive revision to the C standard, which was completed and approved in 1999. The new standard, ISO/IEC 9899:1999, or "C99," replaces the previous standard (and all corrigenda and amendments) and has now become the official Standard C.
Vendors are updating their C compilers and libraries to conform to the new standard.
C99 adds many new features to the C89/C95 language and libraries, including:
• complex arithmetic
• extensions to the integer types, including a longer standard type
• variable-length arrays
• a Boolean type
• better support for non-English character sets
• better support for floating-point types, including math functions for all types
• C++ style comments (/ /)
C99 is a much larger changeset than C95 since it includes changes to the language as well as extensions to the libraries. The C99 Standard document is significantly larger than the C89 document. However, the changes are "in the spirit of C," and they do not change the fundamental nature of the language.

Standard C++

C++, designed by Bjarne Stroustrup at AT&T Bell Labs in the early 1980s, has now largely supplanted C for mainstream programming. Most C implementations are actually C/C++ implementations, giving programmers a choice of which language to use.
C++ has itself been standardized, as ISO/lEC 14882:1998, or "Standard C++."
C++ includes many improvements over C that programmers need for large applications. including improved type checking and support for object-oriented programming. However, C++ is also one of the most complex programming languages, with many pitfalls for the unwary.
Standard C++ is nearly- but not exactly- a superset of Standard C. Since the C and C++ standards were developed on different schedules, they could not adapt to each other in a coordinated way.
Furthermore, C has kept itself distinct from C++. For example, there has been no attempt to adopt "simplified" versions of C++'s class types.
It is possible to write C code in the common subset of the Standard C and C++ languages -called Clean C by some- so that the code can be compiled either as a C program or a C++ program (i.e. write Standard C code so that it is acceptable to C++ compilers).
Since C++ generally has stricter rules than Standard C, Clean C tends to be a good, portable subset in which to write.
The major changes you must consider when writing Clean C are:
• Clean C programs must use function prototypes. Old-style declarations are not permitted in C++.
• Clean C programs must avoid using names that are reserved words in C++, like class and virtual.

WHICH DIALECT OF C SHOULD YOU USE?

Which dialect of C you use depends on what implementation(s) of C you have available and on how portable you want your code to be. Your choices are:
1. C99, the current version of Standard C. It has all the latest features, but some implementations may not yet support it. (That will change rapidly anyway.)
2. C89, the previous version of Standard C. Most  C programs and most C implementations are based on this version of C, usually with the Amendment I additions.
3. Traditional C, now encountered mostly when maintaining older C programs.
4. Clean C, compatible with C++.
C99 is generally upward compatible with C89, which is generally upward
compatible with traditional C.
Unfortunately, it is hard to write C code that is backward compatible. Consider function prototypes, for example. They are optional in Standard C, forbidden in traditional C, and required in C++. Fortunately, you can use the C preprocessor to alter your code depending on which implementation is being used-and even on whether your Standard C includes the Amendment 1 extensions-. Therefore, your C programs can remain compatible with all dialects.
If you are not limited by your compiler or an existing body of C code, you should definitely use Standard C as your base language. Standard C compilers are now almost universally available.
e.g. The Free Software Foundation's GNU C (gcc) is a free Standard C implementation (with many extensions).

AN OVERVIEW OF C PROGRAMMING

Let's do a quick overview of C programming.
A C program is composed of one or more source files, or translation units, each of which contains some part of the entire C program -typically some number of external functions-. 
Common declarations are often collected into header files and are included into the source files with a special #include directive/command. 
One external function must be named main(); this function is where your program starts(the entry point).
A C compiler independently processes each source file and translates the C program text into instructions understood by the computer(object code). 
The compiler "understands" the C program and analyses it for correctness. If the programmer has made an error the compiler can detect, then the compiler issues an error message. Otherwise, the output of the compiler is usually called object code or an object module.
When all source files are compiled, the object modules are given to a program called the linker. The linker resolves references between the modules, adds functions from the standard run-time library, and detects some programming errors such as the failure to define a needed function. 
The linker is typically not specific to C; each computer system has a standard linker that is used for programs written in many different (compiled) languages.
The linker produces a single executable program. which can then be invoked to run. Although most computer systems go through these steps, they may appear different to the programmer.
In integrated environments such as Microsoft's Visual Studio, they may be completely hidden.

Example

Suppose we have a program to be named aprogram consisting of the two C source files hello.c and startup.c
The file hello.c might contain these lines:
#include /* defines printf  */
void hello (void)
{
   printf("Hellol\n"};
}
Since hello.c contains facilities (the function void hello (void)) that will be used by other parts of our program, we create a header file hello.h to declare those facilities. It contains the line extern void hello{void};
File startup.c contains the main program, which simply calls function hello:
#include "hello.h"
int main (void)
{
  hello () ;
  return 0;
}
On a UNIX system, compiling, linking, and executing the program takes only two steps in a shell:
user@host:~$ gcc -o aprogram hello.c startup.c
user@host:~$ ./aprogram
The ftrst line(the part after the prompt -in italics- ) compiles and links the two source files, adds any standard library functions needed, and writes the executable program to file aprogram
The second line then executes the program, which prints: Hello!

Other non-UNIX implementations may use different commands. Increasingly, modern programming environments present an integrated, graphical interface to the programmer. Building a C application in such an environment requires only selecting commands from a menu or clicking a graphical button.


CONFORMANCE

Both C programs and C implementations can conform to Standard C. 
A C program is said to be strictly conforming to Standard C if that program uses only the features of the language and library described in the Standard. The program's operation must not depend on any aspect of the C language that the Standard characterizes as unspecified, undefined, or implementation-defined. There are Standard C test suites available from Perennial, Inc. and Plum Hall, Inc. that help establish conformance of implementations to the standard.
There are two kinds of conforming implementations-hosted and freestanding-. 
  • A C implementation is said to be a conforming hosted implementation if it accepts any strictly conforming program. 
  • A conforming freestanding implementation is one that accepts any strictly conforming program that uses no library facilities other than those provided in the header files float.h, iso646.h(C95), limits.h, stdarg.h, stdbool.h(C99), stddef.h , and stdint.h(C99).  
Freestanding conformance is meant to accommodate C implementations for embedded systems or other target environments with minimal run-time support. For example,such systems may have no file system.
A conforming program is one that is accepted by a conforming implementation.
Thus, a conforming program can depend on some non portable, implementation-defined features of a conforming implementation, whereas a strictly conforming program cannot depend on those features (and so is maximally portable).

Conforming implementations may provide extensions that do not change the meaning of any strictly conforming program. This allows implementations to add library routines and define their own #pragma directives, but not to introduce new reserved identifiers or change the operation of standard library functions.
Compiler vendors continue to provide non-conforming extensions to which their customers have grown accustomed. Compilers enable (or disable) these extensions with special switches.


Resources:

C A REFERENCE MANUAL : Samuel P. Harbison III, Guy L. Steele Jr.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.