Skip to main content

Introducing C

When someone says "I want a programming language in which I need only say what I wish done", give him a lolipop.

Section 1.1: History of C

  • So the way C was created was something like this. Algol 60 -> BCPL -> B -> NB (New B) -> C. C was originally NB but since it diverged from the B language, the name C was given to the new programming language to rewrite the UNIX system (PDP-11 and other devices).

  • Stable version of C was released in 1973 and it evolved during the late 1970s. It was mostly used by the users for UNIX but later in the 1980s, it gained traction from many different programmers as the compiler for C was there on most machines. The original book of K&R for the C programming language was fuzzy about some of the language's features. It failed to properly distinct the features of the C language and the UNIX system.

  • In 1983, the US standard for C was seen to be happening under the American National Standards Institue (ANSI). It was finished in 1988 and formally approved in December, 1989 as "ANSI standard X3.159-1989" which was approved by International Organization for Standardization (ISO) in 1990 (ISO/IEC 9899:1990). This version is referred to as C89 or C90.

  • It went through some changes in 1995 (described in the document called Amendment 1). Other changes occurred in ISO/IEC 9899:1999 which is commonly known as C99.

Some languages that were inspired from the C language are:

  1. C++
  2. Java
  3. C#
  4. Perl

Section 1.2: Strengths and Weaknesses of C

The philosophy of the C language can be described as:

  1. C is a low level language: Provides access to machine level concepts, provides operations that corresponds to computer's built-in instructions.
  2. C is a small language: C relies heavily on "library" of standard functions.
  3. C is a permissive language: C assumes what the programmer is doing, so it provides a wider degree of "latitude" than other languages

Strengths

These are some of the strengths of the C language:

  1. Efficiency: As C was made as a replacement for the assembly language, it was crucial for the language to run quickly and in limited memory.
  2. Portability: It wasn't planned to make C portable, but thanks to C's association with UNIX and ANSI/ISO, it hasn't diverged into incompatible dialects.
  3. Power: C language has a huge collection of data types and operators that make it a powerful language.
  4. Flexibility: Although it was initially used as a systems programming, it is not limited to only that area as it imposes few restrictions on the use of its features.
  5. Standard Library: It has a standard library for many common use cases like I/O, string handling, storage allocation and much more.
  6. Integration with UNIX: C is much more powerful with UNIX (or Linux) based systems.

Weaknesses

These are some of the weaknesses of the C language:

  1. C programs can be error prone: Due to C's flexibility, it is prone to errors. Like assembly language, the errors aren't detected until the program is run. C language contains pitfalls for the unwary.
  2. C programs can be difficult to understand: C has a number of features that are not available in other languages. C was intentionally made terse (brief/concise) to minimize the time required to enter and edit programs.
  3. C programs can be difficult to modify: Large programs written in C are hard to modify if they weren't designed for maintenance in mind. Modern languages provides classes and packages that support the division of a large problem into manageable pieces.
  • A contest called the "International Obfuscated C Code Contest" encourages contestants to write some of the most confusing C programs. An example is described below (1990s "Best Small Program"):

    v,i,j,k,l,s,a[99];main(){for(scanf("%d",&s);*a-s;v=a[j*=v]-a[i],k=i<s,j+=(v=j<s&&(!k&&!!printf(2+"\n\n%c"-(!l<<!j)," #Q"[l^v?(l^j)&1:2])&&++l||a[i]<s&&v&&v-i+j&&v+i-j))&&!(l%=s),v||(i==j?a[i+=k]=0:++a[i])>=s*k&&++a[--i]);}
  • The programs provides all solutions for the "Eight Queens Problem" (The problem of placing 8 queens on a chessboard such that no queen attacks any other queen). More about this contest on their official page.

Effective Use of C

Some of the effective usage of the C language are:

  1. Learn how to avoid C pitfalls:
  2. Use software tools to make programs more reliable:
  3. Take advantage of existing code libraries:
  4. Adopt a sensible set of coding conventions:
  5. Avoid "tricks" and overly complex code:
  6. Stick to the standard:
  • lint checks a C program for a host of potential errors, like suspicious combination of types, unused variables, nonportable code, etc. Lint can produce a number of messages, which might have sone errors described as well.

  • Compilers have different levels of producing the warning messages. Higher the warning level, the more problems it checks for.

  • Some other tools apart from linting is bound-checkers and leak-finders. C doesn't check for the array subscript (array index), which can be done by bound-checkers. A leak-finder helps locate the "memory leaks": blocks of memory that are dynamically allocated but never deallocated.